Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libGPE-KML / src / org / gvsig / gpe / kml / parser / v21 / features / StyleMapBinding.java @ 38048

History | View | Annotate | Download (4.87 KB)

1
package org.gvsig.gpe.kml.parser.v21.features;
2

    
3
import java.io.IOException;
4

    
5
import javax.xml.namespace.QName;
6

    
7
import org.gvsig.gpe.kml.parser.GPEDefaultKmlParser;
8
import org.gvsig.gpe.kml.utils.Kml2_1_Tags;
9
import org.gvsig.gpe.xml.stream.IXmlStreamReader;
10
import org.gvsig.gpe.xml.stream.XmlStreamException;
11
import org.gvsig.gpe.xml.utils.CompareUtils;
12
import org.gvsig.gpe.xml.utils.XMLAttributesIterator;
13

    
14
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
15
 *
16
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
17
 *
18
 * This program is free software; you can redistribute it and/or
19
 * modify it under the terms of the GNU General Public License
20
 * as published by the Free Software Foundation; either version 2
21
 * of the License, or (at your option) any later version.
22
 *
23
 * This program is distributed in the hope that it will be useful,
24
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26
 * GNU General Public License for more details.
27
 *
28
 * You should have received a copy of the GNU General Public License
29
 * along with this program; if not, write to the Free Software
30
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
31
 *
32
 * For more information, contact:
33
 *
34
 *  Generalitat Valenciana
35
 *   Conselleria d'Infraestructures i Transport
36
 *   Av. Blasco Ib??ez, 50
37
 *   46010 VALENCIA
38
 *   SPAIN
39
 *
40
 *      +34 963862235
41
 *   gvsig@gva.es
42
 *      www.gvsig.gva.es
43
 *
44
 *    or
45
 *
46
 *   IVER T.I. S.A
47
 *   Salamanca 50
48
 *   46005 Valencia
49
 *   Spain
50
 *
51
 *   +34 963163400
52
 *   dac@iver.es
53
 */
54
/* CVS MESSAGES:
55
 *
56
 * $Id: StyleBinding.java 357 2008-01-09 17:50:08Z jpiera $
57
 * $Log$
58
 * Revision 1.2  2007/06/07 14:53:59  jorpiell
59
 * Add the schema support
60
 *
61
 * Revision 1.1  2007/05/11 07:06:29  jorpiell
62
 * Refactoring of some package names
63
 *
64
 * Revision 1.2  2007/05/08 08:22:37  jorpiell
65
 * Add comments to create javadocs
66
 *
67
 * Revision 1.1  2007/04/13 13:16:21  jorpiell
68
 * Add KML reading support
69
 *
70
 *
71
 */
72
/**
73
 * This class parses a StyleMap tag. Example:
74
 * <p>
75
 * <pre>
76
 * <code>
77
 *         <StyleMap id="msn_ylw-pushpin">
78
                <Pair>
79
                        <key>normal</key>
80
                        <styleUrl>#sn_ylw-pushpin</styleUrl>
81
                </Pair>
82
                <Pair>
83
                        <key>highlight</key>
84
                        <styleUrl>#sh_ylw-pushpin</styleUrl>
85
                </Pair>
86
        </StyleMap>
87
 * </code>
88
 * </pre>
89
 * </p>
90
 * @see http://code.google.com/apis/kml/documentation/kml_tags_21.html#stylemap
91
 */
92
public class StyleMapBinding {
93

    
94
        /**
95
         * It parses the StyleMap tag
96
         * @param parser
97
         * The XML parser
98
         * @param handler
99
         * The GPE parser that contains the content handler and
100
         * the error handler
101
         * @throws IOException 
102
         * @throws XmlStreamException 
103
         * @throws XmlStreamException
104
         * @throws IOException
105
         */
106
        public static Object parse(IXmlStreamReader parser,GPEDefaultKmlParser handler) throws XmlStreamException, IOException{
107
                boolean endFeature = false;
108
                boolean endPair = false;
109
                int currentTag;                                
110

    
111
                QName tag = parser.getName();
112
                currentTag = parser.getEventType();
113
                
114
                XMLAttributesIterator attributesIterator = new XMLAttributesIterator(parser);
115
                
116
                String styleId = parser.getAttributeValue(0);
117
                
118
//                System.out.println("Leyendo STYLEMAP " + styleId);
119
                
120
                Object metadata = handler.getContentHandler().startMetadata("STYLEMAP", styleId, attributesIterator);
121
        
122
                XMLAttributesIterator attIterator = new XMLAttributesIterator(parser); 
123
                Object subData = null;
124
                
125
                while (!endFeature){
126
                        switch(currentTag){
127
                        case IXmlStreamReader.START_ELEMENT:
128
                                if (CompareUtils.compareWithOutNamespace(tag,Kml2_1_Tags.PAIR)){
129
                                        parser.next();
130
                                        subData = handler.getContentHandler().startMetadata(tag.getLocalPart(), styleId, attIterator);                                                                                
131
                                }                                                                
132
                                else if (CompareUtils.compareWithOutNamespace(tag,Kml2_1_Tags.KEY)){
133
                                        parser.next();
134
                                        String key = parser.getText();
135
                                        Object keyData = handler.getContentHandler().startMetadata(tag.getLocalPart(), key, attIterator);
136
                                        handler.getContentHandler().addMetadataToMetadata(keyData, subData);
137

    
138
                                                                                
139
                                }                                                                
140
                                else if (CompareUtils.compareWithOutNamespace(tag,Kml2_1_Tags.STYLEURL)){
141
                                        parser.next();
142
                                        String styleUrl = parser.getText();
143
                                        Object styleUrlData = handler.getContentHandler().startMetadata(tag.getLocalPart(), styleUrl, attIterator);
144
                                        handler.getContentHandler().addMetadataToMetadata(styleUrlData, subData);
145
                                        
146
                                }
147
                                break;
148
                        case IXmlStreamReader.END_ELEMENT:
149
                                if (CompareUtils.compareWithOutNamespace(tag,Kml2_1_Tags.STYLEMAP)){
150
                                        endFeature = true;
151
                                }
152
                                else if (CompareUtils.compareWithOutNamespace(tag,Kml2_1_Tags.PAIR)){
153
                                        handler.getContentHandler().addMetadataToMetadata(subData, metadata);
154
                                        endPair = true;
155
                                }
156
                                
157
                                break;
158
                        case IXmlStreamReader.CHARACTERS:        
159
                                break;
160
                        }
161
                        if (!endFeature){                                        
162
                                currentTag = parser.next();
163
                                tag = parser.getName();
164
//                                System.out.println("tag = " + tag);
165
                        }
166
                }                        
167
                return metadata;
168
        }
169
}