Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libGPE-KML / src / org / gvsig / gpe / kml / utils / KMLUtilsParser.java @ 37960

History | View | Annotate | Download (5.57 KB)

1
package org.gvsig.gpe.kml.utils;
2

    
3
import java.awt.Color;
4
import java.text.NumberFormat;
5
import java.util.Hashtable;
6

    
7
import javax.xml.namespace.QName;
8

    
9
import org.gvsig.gpe.GPEDefaults;
10
import org.gvsig.gpe.utils.StringUtils;
11
import org.gvsig.gpe.xml.XmlProperties;
12
import org.gvsig.gpe.xml.stream.IXmlStreamReader;
13
import org.gvsig.gpe.xml.stream.XmlStreamException;
14

    
15

    
16
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
17
 *
18
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
19
 *
20
 * This program is free software; you can redistribute it and/or
21
 * modify it under the terms of the GNU General Public License
22
 * as published by the Free Software Foundation; either version 2
23
 * of the License, or (at your option) any later version.
24
 *
25
 * This program is distributed in the hope that it will be useful,
26
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28
 * GNU General Public License for more details.
29
 *
30
 * You should have received a copy of the GNU General Public License
31
 * along with this program; if not, write to the Free Software
32
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
33
 *
34
 * For more information, contact:
35
 *
36
 *  Generalitat Valenciana
37
 *   Conselleria d'Infraestructures i Transport
38
 *   Av. Blasco Ib??ez, 50
39
 *   46010 VALENCIA
40
 *   SPAIN
41
 *
42
 *      +34 963862235
43
 *   gvsig@gva.es
44
 *      www.gvsig.gva.es
45
 *
46
 *    or
47
 *
48
 *   IVER T.I. S.A
49
 *   Salamanca 50
50
 *   46005 Valencia
51
 *   Spain
52
 *
53
 *   +34 963163400
54
 *   dac@iver.es
55
 */
56
/* CVS MESSAGES:
57
 *
58
 * $Id: GMLUtilsParser.java 189 2007-11-21 12:45:56Z csanchez $
59
 * $Log$
60
 * Revision 1.8  2007/06/28 13:05:09  jorpiell
61
 * The Qname has been updated to the 1.5 JVM machine. The schema validation is made in the GPEWriterHandlerImplementor class
62
 *
63
 * Revision 1.7  2007/05/18 10:41:01  csanchez
64
 * Actualizaci?n libGPE-GML eliminaci?n de clases inecesarias
65
 *
66
 * Revision 1.6  2007/05/16 13:00:48  csanchez
67
 * Actualizaci?n de libGPE-GML
68
 *
69
 * Revision 1.5  2007/05/16 09:29:12  jorpiell
70
 * The polygons has to be closed
71
 *
72
 * Revision 1.4  2007/05/15 10:14:45  jorpiell
73
 * The element and the feature is managed like a Stack
74
 *
75
 * Revision 1.3  2007/05/15 09:35:09  jorpiell
76
 * the tag names cant have blanc spaces
77
 *
78
 * Revision 1.2  2007/05/07 12:58:42  jorpiell
79
 * Add some methods to manage the multigeometries
80
 *
81
 * Revision 1.1  2007/02/28 11:48:31  csanchez
82
 * *** empty log message ***
83
 *
84
 * Revision 1.1  2007/02/20 10:53:20  jorpiell
85
 * A?adidos los proyectos de kml y gml antiguos
86
 *
87
 * Revision 1.3  2007/01/15 13:11:00  csanchez
88
 * Sistema de Warnings y Excepciones adaptado a BasicException
89
 *
90
 * Revision 1.2  2006/12/22 11:25:44  csanchez
91
 * Nuevo parser GML 2.x para gml's sin esquema
92
 *
93
 * Revision 1.1  2006/08/10 12:00:49  jorpiell
94
 * Primer commit del driver de Gml
95
 *
96
 *
97
 */
98
/**
99
 * @author Jorge Piera Llodr? (piera_jor@gva.es)
100
 * @author Carlos S?nchez Peri??n (sanchez_carper@gva.es)
101
 */
102
public class KMLUtilsParser {
103

    
104
        /**
105
         * It returns a HashTable with the XML attributes. It has been 
106
         * created because the parser doesn't has a getAttribiute(AttributeName)
107
         * method.
108

109
         * @param parser
110
         * @return
111
         * @throws XmlStreamException 
112
         */
113
        public static Hashtable getAttributes(IXmlStreamReader parser) throws XmlStreamException{
114
                Hashtable hash = new Hashtable();
115
                int num_atributos = parser.getAttributeCount();
116
                for (int i=0 ; i<parser.getAttributeCount() ; i++){
117
                        QName atributo = parser.getAttributeName(i);
118
                        String valor=parser.getAttributeValue(i);
119
                        if (valor!=null)
120
                                hash.put(atributo, valor);
121
                }
122
                return hash;
123
        }        
124
        
125
        /**
126
         * Remove the blanc symbol from a tag
127
         * @param tag
128
         * Tag name
129
         * @return
130
         * The tag without blancs
131
         */
132
        public static String removeBlancSymbol(QName tag){
133
                if (tag == null){
134
                        return null;
135
                }
136
                String blancSpace = GPEDefaults.getStringProperty(XmlProperties.DEFAULT_BLANC_SPACE);
137
                if (blancSpace == null){
138
                        blancSpace = Kml2_1_Tags.DEFAULT_BLANC_SPACE;
139
                }
140
                // PROBLEM WITH COMPATIBILITY OF "replaceAll()" WITH IBM J9 JAVA MICROEDITION
141
                return StringUtils.replaceAllString(tag.getLocalPart(), blancSpace, " ");
142
                // return tag.replaceAll(blancSpace," ");
143
        }
144
        
145
        /**
146
         * Replace the blancs of a tag with the
147
         * deafult blanc symbol
148
         * @param name
149
         * @return
150
         * A tag with blancs
151
         */
152
        public static String addBlancSymbol(QName name){
153
                if (name == null){
154
                        return null;
155
                }
156
                String blancSpace = GPEDefaults.getStringProperty(XmlProperties.DEFAULT_BLANC_SPACE);
157
                if (blancSpace == null){
158
                        blancSpace = Kml2_1_Tags.DEFAULT_BLANC_SPACE;
159
                }
160
                // PROBLEM WITH COMPATIBILITY OF "replaceAll()" WITH IBM J9 JAVA MICROEDITION
161
                return StringUtils.replaceAllString(name.getLocalPart()," ",blancSpace);
162
                //return tag.replaceAll(" ",blancSpace);
163
        }
164
        
165
        public static Color fromABGRtoColor(String abgr) {
166
                long in = Long.decode("#"+ abgr);
167
                int alpha = (int) (in >> 24) & 0xFF;
168
                int blue = (int) (in >> 16) & 0xFF;
169
                int green = (int) (in >> 8) & 0xFF;
170
                int red = (int) (in >> 0) & 0xFF;
171
                return new Color(red, green, blue, alpha);
172
        }
173
        
174
        public static String padWith0(String s) {
175
                if ( s.length() < 2 )
176
              { // pad on left with zeros
177
                        s = "00".substring( 0, 2 - s.length() ) + s;
178
              }
179
                return s;
180
        }
181

    
182
        public static String fromColorToABGR(Color color) {
183
                String abgr;
184
                String alpha = padWith0(Integer.toHexString(color.getAlpha()));
185
                String blue = padWith0(Integer.toHexString(color.getBlue()));
186
                String green = padWith0(Integer.toHexString(color.getGreen()));
187
                String red = padWith0(Integer.toHexString(color.getRed()));
188
                abgr = alpha + blue + green + red;
189
                return abgr;
190
        }
191

    
192
}
193