Revision 188

View differences:

org.gvsig.xmlpull/library/trunk/org.gvsig.xmlpull/org.gvsig.xmlpull.prov/org.gvsig.xmlpull.prov.kxml/src/main/java/org/gvsig/xml/prov/kxml/stream/KxmlXmlStreamReader.java
1
package org.gvsig.xml.prov.kxml.stream;
2

  
3
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
4
 *
5
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
6
 *
7
 * This program is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU General Public License
9
 * as published by the Free Software Foundation; either version 2
10
 * of the License, or (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program; if not, write to the Free Software
19
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
20
 *
21
 * For more information, contact:
22
 *
23
 *  Generalitat Valenciana
24
 *   Conselleria d'Infraestructures i Transport
25
 *   Av. Blasco Ib??ez, 50
26
 *   46010 VALENCIA
27
 *   SPAIN
28
 *
29
 *      +34 963862235
30
 *   gvsig@gva.es
31
 *      www.gvsig.gva.es
32
 *
33
 *    or
34
 *
35
 *   IVER T.I. S.A
36
 *   Salamanca 50
37
 *   46005 Valencia
38
 *   Spain
39
 *
40
 *   +34 963163400
41
 *   dac@iver.es
42
 */
43
/* CVS MESSAGES:
44
 *
45
 * $Id: XmlPullStreamReader.java 19593 2008-03-12 17:23:30Z groldan $
46
 * $Log$
47
 */
48

  
49
import java.io.IOException;
50
import java.io.InputStream;
51

  
52
import javax.xml.namespace.QName;
53
import javax.xml.stream.XMLStreamReader;
54

  
55
import org.gvsig.xml.lib.api.stream.IXmlStreamReader;
56
import org.gvsig.xml.lib.api.stream.XmlStreamException;
57
import org.kxml2.io.KXmlParser;
58
import org.xmlpull.v1.XmlPullParser;
59
import org.xmlpull.v1.XmlPullParserException;
60

  
61
/**
62
 * An {@link IXmlStreamReader} adapter for xml textual parsing using a
63
 * {@link XmlPullParser pull parser}.
64
 * 
65
 * @author Gabriel Roldan (TOPP)
66
 * @version $Id: XmlPullStreamReader.java 19593 2008-03-12 17:23:30Z groldan $
67
 */
68
public class KxmlXmlStreamReader implements IXmlStreamReader {
69

  
70
    private XmlPullParser parser;
71
	/**
72
     * Default constructor for this parser
73
     * @param in 
74
     * @throws XmlStreamException 
75
     */
76
    public KxmlXmlStreamReader(InputStream in) throws XmlStreamException {
77
			setInput(in);
78
    }
79

  
80
    /**
81
     * Sets the internal xmlpull parser input stream
82
     * 
83
     * @param inputStream
84
     * @throws XmlStreamException
85
     * @see org.gvsig.gpe.xml.stream.IXmlStreamReader#setInput(java.io.InputStream)
86
     */
87
    public void setInput(final InputStream inputStream) throws XmlStreamException {
88
        try {
89
            parser = new KXmlParser();
90
            parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
91
            // let the parser inferr the encoding
92
            parser.setInput(inputStream, null);
93
        } catch (XmlPullParserException e) {
94
            throw new XmlStreamException(e);
95
        }
96
    }
97

  
98
    public int getAttributeCount() throws XmlStreamException {
99
        return parser.getAttributeCount();
100
    }
101

  
102
    public QName getAttributeName(int i) throws XmlStreamException {
103
        return new QName(parser.getAttributeNamespace(i), parser.getAttributeName(i));
104
    }
105

  
106
    public String getAttributeValue(int i) throws XmlStreamException {
107
        return parser.getAttributeValue(i);
108
    }
109

  
110
    public int getEventType() throws XmlStreamException {
111
        // TODO: improve this mapping
112
        int xmlPullEventType;
113
        try {
114
            xmlPullEventType = parser.getEventType();
115
        } catch (XmlPullParserException e) {
116
            throw new XmlStreamException(e);
117
        }
118
        return pullEventToGpeEventType(xmlPullEventType);
119
    }
120

  
121
    /**
122
     * @return
123
     * @see org.gvsig.gpe.xml.stream.IXmlStreamReader#getName()
124
     */
125
    public QName getName() throws XmlStreamException {
126
    	int eventType;
127
		try {
128
			eventType = parser.getEventType();
129
			if (eventType != XMLStreamReader.START_ELEMENT
130
	                 && eventType  != XMLStreamReader.END_ELEMENT
131
	                 && eventType  != XMLStreamReader.PROCESSING_INSTRUCTION
132
	                 && eventType  != XMLStreamReader.CHARACTERS) {
133
	             return null;
134
	        }
135
			String name = parser.getName();
136
			if (name != null){
137
    			name = name.substring(name.indexOf(":") + 1, name.length());
138
    			parser.getNamespace();
139
    			return new QName(parser.getNamespace(), name);
140
    		}
141
		} catch (XmlPullParserException e) {
142
			return null;
143
		}
144
		return null;     	
145
    }
146

  
147
    public String getText() throws XmlStreamException {
148
        return parser.getText();
149
    }
150

  
151
    public boolean isWhitespace() throws XmlStreamException {
152
        try {
153
            return parser.isWhitespace();
154
        } catch (XmlPullParserException e) {
155
            throw new XmlStreamException(e);
156
        }
157
    }
158

  
159
    public int next() throws XmlStreamException {
160
        int xmlPullEventType;
161
        if (getEventType()==XMLStreamReader.END_DOCUMENT)
162
        	throw new XmlStreamException("End Document Exception! \n There aren't more items to get.");
163
        try {
164
            xmlPullEventType = parser.next();
165
        } catch (XmlPullParserException e) {
166
            throw new XmlStreamException(e);
167
        } catch (IOException e) {
168
            throw new XmlStreamException(e);
169
        }
170
        return pullEventToGpeEventType(xmlPullEventType);
171
    }
172

  
173
    public int nextTag() throws XmlStreamException {
174
        int xmlPullEventType;
175
        if (getEventType()==XMLStreamReader.END_DOCUMENT)
176
        	throw new XmlStreamException("End Document Exception! \n There aren't more tags to get.");
177
        try {
178
            xmlPullEventType = parser.nextTag();
179
        } catch (XmlPullParserException e) {
180
            throw new XmlStreamException(e);
181
        } catch (IOException e) {
182
            throw new XmlStreamException(e);
183
        }
184
        return pullEventToGpeEventType(xmlPullEventType);
185
    }
186

  
187
    private int pullEventToGpeEventType(int xmlPullEventType) {
188
        switch (xmlPullEventType) {
189
        case XmlPullParser.START_DOCUMENT:
190
            return IXmlStreamReader.START_DOCUMENT;
191
        case XmlPullParser.END_DOCUMENT:
192
            return IXmlStreamReader.END_DOCUMENT;
193
        case XmlPullParser.START_TAG:
194
            return IXmlStreamReader.START_ELEMENT;
195
        case XmlPullParser.END_TAG:
196
            return IXmlStreamReader.END_ELEMENT;
197
        case XmlPullParser.TEXT:
198
            return IXmlStreamReader.CHARACTERS;
199
        case XmlPullParser.CDSECT:
200
            return IXmlStreamReader.CDATA;
201
        case XmlPullParser.ENTITY_REF:
202
            return IXmlStreamReader.ENTITY_REFERENCE;
203
        case XmlPullParser.IGNORABLE_WHITESPACE:
204
            return IXmlStreamReader.SPACE;
205
        case XmlPullParser.PROCESSING_INSTRUCTION:
206
            return IXmlStreamReader.PROCESSING_INSTRUCTION;
207
        case XmlPullParser.COMMENT:
208
            return IXmlStreamReader.COMMENT;
209
        case XmlPullParser.DOCDECL:
210
            return IXmlStreamReader.DTD;
211
        default:
212
            throw new IllegalStateException("Unknown tag type, this should't happen!: "
213
                    + xmlPullEventType);
214
        }
215
    }
216

  
217
}
0 218

  
org.gvsig.xmlpull/library/trunk/org.gvsig.xmlpull/org.gvsig.xmlpull.prov/org.gvsig.xmlpull.prov.kxml/src/main/java/org/gvsig/xml/prov/kxml/stream/KxmlXmlParserFactory.java
1
package org.gvsig.xml.prov.kxml.stream;
2

  
3
import java.io.InputStream;
4

  
5
import org.gvsig.xml.lib.api.stream.IXmlStreamReader;
6
import org.gvsig.xml.lib.api.stream.IXmlStreamReaderFactory;
7
import org.gvsig.xml.lib.api.stream.XmlStreamException;
8

  
9
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
10
 *
11
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
12
 *
13
 * This program is free software; you can redistribute it and/or
14
 * modify it under the terms of the GNU General Public License
15
 * as published by the Free Software Foundation; either version 2
16
 * of the License, or (at your option) any later version.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
 * GNU General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU General Public License
24
 * along with this program; if not, write to the Free Software
25
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
26
 *
27
 * For more information, contact:
28
 *
29
 *  Generalitat Valenciana
30
 *   Conselleria d'Infraestructures i Transport
31
 *   Av. Blasco Ib??ez, 50
32
 *   46010 VALENCIA
33
 *   SPAIN
34
 *
35
 *      +34 963862235
36
 *   gvsig@gva.es
37
 *      www.gvsig.gva.es
38
 *
39
 *    or
40
 *
41
 *   IVER T.I. S.A
42
 *   Salamanca 50
43
 *   46005 Valencia
44
 *   Spain
45
 *
46
 *   +34 963163400
47
 *   dac@iver.es
48
 */
49
/* CVS MESSAGES:
50
 *
51
 * $Id$
52
 * $Log$
53
 *
54
 */
55
/**
56
 * @author Jorge Piera Llodr? (jorge.piera@iver.es)
57
 */
58
public class KxmlXmlParserFactory implements IXmlStreamReaderFactory {
59

  
60
    /**
61
     * @see org.gvsig.gpe.xml.stream.IXmlStreamReaderFactory#canParse(java.lang.String)
62
     */
63
    public boolean canParse(String mimeType) {
64
        return mimeType.startsWith("text/xml");
65
    }
66

  
67
    /**
68
     * @see org.gvsig.gpe.xml.stream.IXmlStreamReaderFactory#createParser(java.lang.String,
69
     *      java.io.InputStream)
70
     */
71
    public IXmlStreamReader createParser(String mimeType, final InputStream in)
72
            throws XmlStreamException, IllegalArgumentException {
73
        if (!canParse(mimeType)) {
74
            throw new IllegalArgumentException("Unsupported mime type for this reader factory: "
75
                    + mimeType);
76
        }
77
        KxmlXmlStreamReader reader = new KxmlXmlStreamReader(in);
78
        reader.setInput(in);
79
        return reader;
80
    }
81
}
82

  
0 83

  
org.gvsig.xmlpull/library/trunk/org.gvsig.xmlpull/org.gvsig.xmlpull.prov/org.gvsig.xmlpull.prov.kxml/src/main/java/org/gvsig/xml/prov/kxml/KxmlLibrary.java
1
/* gvSIG. Geographic Information System of the Valencian Government
2
*
3
* Copyright (C) 2007-2008 Infrastructures and Transports Department
4
* of the Valencian Government (CIT)
5
* 
6
* This program is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU General Public License
8
* as published by the Free Software Foundation; either version 2
9
* of the License, or (at your option) any later version.
10
* 
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
* GNU General Public License for more details.
15
* 
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
19
* MA  02110-1301, USA.
20
* 
21
*/
22

  
23
/*
24
* AUTHORS (In addition to CIT):
25
* 2010 {Iver T.I.}   {Task}
26
*/
27
 
28
package org.gvsig.xml.prov.kxml;
29

  
30
import org.gvsig.tools.library.AbstractLibrary;
31
import org.gvsig.tools.library.LibraryException;
32
import org.gvsig.xml.lib.spi.XMLProviderLocator;
33
import org.gvsig.xml.prov.kxml.stream.KxmlXmlParserFactory;
34

  
35
/**
36
 * @author <a href="mailto:jpiera@gvsig.org">Jorge Piera</a>
37
 */
38
public class KxmlLibrary extends AbstractLibrary  {
39

  
40
	protected void doInitialize() throws LibraryException {
41
	}
42

  
43
	protected void doPostInitialize() throws LibraryException {
44
		XMLProviderLocator.getXMLProviderManager().
45
			registerXMLStreamReaderFactory(new KxmlXmlParserFactory());
46
	}
47
}
48

  
0 49

  
org.gvsig.xmlpull/library/trunk/org.gvsig.xmlpull/org.gvsig.xmlpull.prov/org.gvsig.xmlpull.prov.kxml/pom.xml
1
<?xml version="1.0" encoding="ISO-8859-1"?>
2

  
3
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
5
                      http://maven.apache.org/xsd/maven-4.0.0.xsd">
6
	<modelVersion>4.0.0</modelVersion>
7
	<groupId>org.gvsig</groupId>
8
	<artifactId>org.gvsig.xml.prov.kxml</artifactId>
9
	<packaging>jar</packaging>
10
	<name>org.gvsig.xml.prov.kxml</name>
11
	<description>Kxml provider for XML</description>
12
	<parent>
13
		<groupId>org.gvsig</groupId>
14
		<artifactId>org.gvsig.xml.prov</artifactId>
15
		<version>2.0-SNAPSHOT</version>				
16
	</parent>
17
	<dependencies>
18
		<dependency>
19
			<groupId>org.gvsig</groupId>
20
			<artifactId>org.gvsig.tools</artifactId>
21
		</dependency>
22
		<dependency>
23
			<groupId>org.gvsig</groupId>
24
			<artifactId>org.gvsig.xml.lib.api</artifactId>			
25
		</dependency>
26
		<dependency>
27
			<groupId>org.gvsig</groupId>
28
			<artifactId>org.gvsig.xml.lib.spi</artifactId>			
29
		</dependency>
30
	</dependencies>
31
	<build>		
32
		<plugins>
33
			<plugin>
34
				<groupId>org.apache.maven.plugins</groupId>
35
				<artifactId>maven-compiler-plugin</artifactId>
36
				<configuration>
37
					<source>1.4</source>
38
					<target>1.4</target>
39
				</configuration>
40
			</plugin>
41
		</plugins>
42
	</build>
43
</project>
0 44

  

Also available in: Unified diff