Revision 169
org.gvsig.xml/library/trunk/org.gvsig.xml/org.gvsig.xml.prov/org.gvsig.xml.prov.stax/src/main/java/org/gvsig/xml/prov/stax/stream/StaxXmlStreamReader.java | ||
---|---|---|
1 |
package org.gvsig.xml.prov.stax.stream; |
|
2 |
|
|
3 |
import java.io.InputStream; |
|
4 |
|
|
5 |
import javax.xml.namespace.QName; |
|
6 |
import javax.xml.stream.XMLInputFactory; |
|
7 |
import javax.xml.stream.XMLStreamConstants; |
|
8 |
import javax.xml.stream.XMLStreamException; |
|
9 |
import javax.xml.stream.XMLStreamReader; |
|
10 |
|
|
11 |
import org.gvsig.xml.lib.api.stream.IXmlStreamReader; |
|
12 |
import org.gvsig.xml.lib.api.stream.XmlStreamException; |
|
13 |
|
|
14 |
|
|
15 |
/** |
|
16 |
* An {@link IXmlStreamReader} adapter for xml textual parsing using a |
|
17 |
* {@link XMLStreamReader standard StAX} API. |
|
18 |
* |
|
19 |
* @author Gabriel Roldan (TOPP) |
|
20 |
* @version $Id: StaxXmlStreamReader.java 21945 2008-06-30 14:04:53Z jpiera $ |
|
21 |
*/ |
|
22 |
public class StaxXmlStreamReader implements IXmlStreamReader { |
|
23 |
|
|
24 |
private XMLStreamReader parser; |
|
25 |
|
|
26 |
/** |
|
27 |
* @param in |
|
28 |
* @throws XmlStreamException |
|
29 |
*/ |
|
30 |
public StaxXmlStreamReader(InputStream in) throws XmlStreamException { |
|
31 |
setInput(in); |
|
32 |
} |
|
33 |
|
|
34 |
// public StaxXmlStreamReader(final XMLStreamReader reader) { |
|
35 |
// |
|
36 |
// } |
|
37 |
|
|
38 |
/** |
|
39 |
* @param inputStream |
|
40 |
* @throws XmlStreamException |
|
41 |
*/ |
|
42 |
private void setInput(InputStream inputStream) throws XmlStreamException { |
|
43 |
final XMLInputFactory factory = XMLInputFactory.newInstance(); |
|
44 |
// tell the factory to return characters content in a single spot |
|
45 |
// factory.setProperty("javax.xml.stream.isCoalescing", "true"); |
|
46 |
try { |
|
47 |
parser = factory.createXMLStreamReader(inputStream, null); |
|
48 |
} catch (XMLStreamException e) { |
|
49 |
Throwable nestedException = e.getNestedException(); |
|
50 |
String msg = "Error creating StAX parser: " + e.getMessage(); |
|
51 |
if (nestedException != null) { |
|
52 |
msg += "(" + nestedException.getMessage() + ")"; |
|
53 |
} |
|
54 |
throw new XmlStreamException(msg, e); |
|
55 |
} |
|
56 |
} |
|
57 |
|
|
58 |
/** |
|
59 |
* @return |
|
60 |
* @see org.gvsig.gpe.xml.stream.IXmlStreamReader#getAttributeCount() |
|
61 |
*/ |
|
62 |
public int getAttributeCount() throws XmlStreamException { |
|
63 |
return parser.getAttributeCount(); |
|
64 |
} |
|
65 |
|
|
66 |
public QName getAttributeName(int i) throws XmlStreamException { |
|
67 |
return parser.getAttributeName(i); |
|
68 |
} |
|
69 |
|
|
70 |
public String getAttributeValue(int i) throws XmlStreamException { |
|
71 |
return parser.getAttributeValue(i); |
|
72 |
} |
|
73 |
|
|
74 |
public int getEventType() throws XmlStreamException { |
|
75 |
int xmlPullEventType; |
|
76 |
xmlPullEventType = parser.getEventType(); |
|
77 |
return pullEventToGpeEventType(xmlPullEventType); |
|
78 |
} |
|
79 |
|
|
80 |
/** |
|
81 |
* Returns the (local) name of the current tag (the current event shall be START_ELEMENT or |
|
82 |
* END_ELEMENT), or null if the current event is not a tag event. |
|
83 |
* |
|
84 |
* @return |
|
85 |
* @see org.gvsig.gpe.xml.stream.IXmlStreamReader#getName() |
|
86 |
*/ |
|
87 |
public QName getName() throws XmlStreamException { |
|
88 |
final int eventType = parser.getEventType(); |
|
89 |
if (eventType != XMLStreamConstants.START_ELEMENT |
|
90 |
&& eventType != XMLStreamConstants.END_ELEMENT) { |
|
91 |
return null; |
|
92 |
} |
|
93 |
// QName name = parser.getName(); |
|
94 |
// String qname = null; |
|
95 |
// if(name != null){ |
|
96 |
// String prefix = name.getPrefix(); |
|
97 |
// String localName = name.getLocalPart(); |
|
98 |
// if(XMLConstants.DEFAULT_NS_PREFIX.equals(prefix)){ |
|
99 |
// qname = localName; |
|
100 |
// }else{ |
|
101 |
// qname = prefix + ":" + localName; |
|
102 |
// } |
|
103 |
// } |
|
104 |
return parser.getName(); |
|
105 |
} |
|
106 |
|
|
107 |
public String getText() throws XmlStreamException { |
|
108 |
return parser.getText(); |
|
109 |
} |
|
110 |
|
|
111 |
public boolean isWhitespace() throws XmlStreamException { |
|
112 |
return parser.isWhiteSpace(); |
|
113 |
} |
|
114 |
|
|
115 |
public int next() throws XmlStreamException { |
|
116 |
int xmlPullEventType; |
|
117 |
try { |
|
118 |
xmlPullEventType = parser.next(); |
|
119 |
} catch (XMLStreamException e) { |
|
120 |
String msg = "Failed to call XmlStreamReader.next()"; |
|
121 |
if (e.getNestedException() != null) { |
|
122 |
msg += ": " + e.getNestedException().getMessage(); |
|
123 |
} |
|
124 |
throw new XmlStreamException(msg, e.getNestedException() == null ? e : e |
|
125 |
.getNestedException()); |
|
126 |
} |
|
127 |
int pullEventToGpeEventType = pullEventToGpeEventType(xmlPullEventType); |
|
128 |
return pullEventToGpeEventType; |
|
129 |
} |
|
130 |
|
|
131 |
public int nextTag() throws XmlStreamException { |
|
132 |
int xmlPullEventType; |
|
133 |
try { |
|
134 |
xmlPullEventType = parser.nextTag(); |
|
135 |
} catch (XMLStreamException e) { |
|
136 |
e.printStackTrace(); |
|
137 |
throw new XmlStreamException(e); |
|
138 |
} |
|
139 |
int pullEventToGpeEventType = pullEventToGpeEventType(xmlPullEventType); |
|
140 |
return pullEventToGpeEventType; |
|
141 |
} |
|
142 |
|
|
143 |
private int pullEventToGpeEventType(int xmlPullEventType) { |
|
144 |
switch (xmlPullEventType) { |
|
145 |
case XMLStreamConstants.START_DOCUMENT: |
|
146 |
return IXmlStreamReader.START_DOCUMENT; |
|
147 |
case XMLStreamConstants.END_DOCUMENT: |
|
148 |
return IXmlStreamReader.END_DOCUMENT; |
|
149 |
case XMLStreamConstants.START_ELEMENT: |
|
150 |
return IXmlStreamReader.START_ELEMENT; |
|
151 |
case XMLStreamConstants.END_ELEMENT: |
|
152 |
return IXmlStreamReader.END_ELEMENT; |
|
153 |
case XMLStreamConstants.CHARACTERS: |
|
154 |
return IXmlStreamReader.CHARACTERS; |
|
155 |
case XMLStreamConstants.CDATA: |
|
156 |
return IXmlStreamReader.CDATA; |
|
157 |
case XMLStreamConstants.ENTITY_REFERENCE: |
|
158 |
return IXmlStreamReader.ENTITY_REFERENCE; |
|
159 |
case XMLStreamConstants.SPACE: |
|
160 |
return IXmlStreamReader.SPACE; |
|
161 |
case XMLStreamConstants.PROCESSING_INSTRUCTION: |
|
162 |
return IXmlStreamReader.PROCESSING_INSTRUCTION; |
|
163 |
case XMLStreamConstants.COMMENT: |
|
164 |
return IXmlStreamReader.COMMENT; |
|
165 |
case XMLStreamConstants.DTD: |
|
166 |
return IXmlStreamReader.DTD; |
|
167 |
default: |
|
168 |
throw new IllegalStateException("Unknown tag type, this should't happen!: " |
|
169 |
+ xmlPullEventType); |
|
170 |
} |
|
171 |
} |
|
172 |
} |
|
0 | 173 |
org.gvsig.xml/library/trunk/org.gvsig.xml/org.gvsig.xml.prov/org.gvsig.xml.prov.stax/src/main/java/org/gvsig/xml/prov/stax/stream/StaxXmlParserFactory.java | ||
---|---|---|
1 |
package org.gvsig.xml.prov.stax.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 |
|
|
10 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana |
|
11 |
* |
|
12 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana. |
|
13 |
* |
|
14 |
* This program is free software; you can redistribute it and/or |
|
15 |
* modify it under the terms of the GNU General Public License |
|
16 |
* as published by the Free Software Foundation; either version 2 |
|
17 |
* of the License, or (at your option) any later version. |
|
18 |
* |
|
19 |
* This program is distributed in the hope that it will be useful, |
|
20 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
21 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
22 |
* GNU General Public License for more details. |
|
23 |
* |
|
24 |
* You should have received a copy of the GNU General Public License |
|
25 |
* along with this program; if not, write to the Free Software |
|
26 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA. |
|
27 |
* |
|
28 |
* For more information, contact: |
|
29 |
* |
|
30 |
* Generalitat Valenciana |
|
31 |
* Conselleria d'Infraestructures i Transport |
|
32 |
* Av. Blasco Ib??ez, 50 |
|
33 |
* 46010 VALENCIA |
|
34 |
* SPAIN |
|
35 |
* |
|
36 |
* +34 963862235 |
|
37 |
* gvsig@gva.es |
|
38 |
* www.gvsig.gva.es |
|
39 |
* |
|
40 |
* or |
|
41 |
* |
|
42 |
* IVER T.I. S.A |
|
43 |
* Salamanca 50 |
|
44 |
* 46005 Valencia |
|
45 |
* Spain |
|
46 |
* |
|
47 |
* +34 963163400 |
|
48 |
* dac@iver.es |
|
49 |
*/ |
|
50 |
|
|
51 |
/** |
|
52 |
* |
|
53 |
*/ |
|
54 |
public class StaxXmlParserFactory implements IXmlStreamReaderFactory { |
|
55 |
|
|
56 |
/** |
|
57 |
* @see org.gvsig.gpe.xml.stream.IXmlStreamReaderFactory#canParse(java.lang.String) |
|
58 |
*/ |
|
59 |
public boolean canParse(String mimeType) { |
|
60 |
return mimeType.startsWith("text/xml"); |
|
61 |
} |
|
62 |
|
|
63 |
/** |
|
64 |
* @see org.gvsig.gpe.xml.stream.IXmlStreamReaderFactory#createParser(java.lang.String, |
|
65 |
* java.io.InputStream) |
|
66 |
*/ |
|
67 |
public IXmlStreamReader createParser(String mimeType, final InputStream in) |
|
68 |
throws XmlStreamException, IllegalArgumentException { |
|
69 |
if (!canParse(mimeType)) { |
|
70 |
throw new IllegalArgumentException("Unsupported mime type for this writer factory: " |
|
71 |
+ mimeType); |
|
72 |
} |
|
73 |
return new StaxXmlStreamReader(in); |
|
74 |
} |
|
75 |
} |
|
0 | 76 |
org.gvsig.xml/library/trunk/org.gvsig.xml/org.gvsig.xml.prov/org.gvsig.xml.prov.stax/src/main/java/org/gvsig/xml/prov/stax/stream/StaxXmlWriterFactory.java | ||
---|---|---|
1 |
package org.gvsig.xml.prov.stax.stream; |
|
2 |
|
|
3 |
import java.io.OutputStream; |
|
4 |
|
|
5 |
import org.gvsig.xml.lib.api.stream.IXmlStreamWriter; |
|
6 |
import org.gvsig.xml.lib.api.stream.IXmlStreamWriterFactory; |
|
7 |
import org.gvsig.xml.lib.api.stream.XmlStreamException; |
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana |
|
12 |
* |
|
13 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana. |
|
14 |
* |
|
15 |
* This program is free software; you can redistribute it and/or |
|
16 |
* modify it under the terms of the GNU General Public License |
|
17 |
* as published by the Free Software Foundation; either version 2 |
|
18 |
* of the License, or (at your option) any later version. |
|
19 |
* |
|
20 |
* This program is distributed in the hope that it will be useful, |
|
21 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
22 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
23 |
* GNU General Public License for more details. |
|
24 |
* |
|
25 |
* You should have received a copy of the GNU General Public License |
|
26 |
* along with this program; if not, write to the Free Software |
|
27 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA. |
|
28 |
* |
|
29 |
* For more information, contact: |
|
30 |
* |
|
31 |
* Generalitat Valenciana |
|
32 |
* Conselleria d'Infraestructures i Transport |
|
33 |
* Av. Blasco Ib??ez, 50 |
|
34 |
* 46010 VALENCIA |
|
35 |
* SPAIN |
|
36 |
* |
|
37 |
* +34 963862235 |
|
38 |
* gvsig@gva.es |
|
39 |
* www.gvsig.gva.es |
|
40 |
* |
|
41 |
* or |
|
42 |
* |
|
43 |
* IVER T.I. S.A |
|
44 |
* Salamanca 50 |
|
45 |
* 46005 Valencia |
|
46 |
* Spain |
|
47 |
* |
|
48 |
* +34 963163400 |
|
49 |
* dac@iver.es |
|
50 |
*/ |
|
51 |
|
|
52 |
/** |
|
53 |
* A factory for streamed XML writers. |
|
54 |
* |
|
55 |
*/ |
|
56 |
public class StaxXmlWriterFactory implements IXmlStreamWriterFactory { |
|
57 |
|
|
58 |
/** |
|
59 |
* @see org.gvsig.gpe.xml.stream.IXmlStreamWriterFactory#canWrite(java.lang.String) |
|
60 |
*/ |
|
61 |
public boolean canWrite(final String mimeType) { |
|
62 |
return mimeType.startsWith("text/xml"); |
|
63 |
} |
|
64 |
|
|
65 |
/** |
|
66 |
* @see org.gvsig.gpe.xml.stream.IXmlStreamWriterFactory#createWriter(java.lang.String, |
|
67 |
* java.io.OutputStream) |
|
68 |
*/ |
|
69 |
public IXmlStreamWriter createWriter(String mimeType, OutputStream out) |
|
70 |
throws XmlStreamException, IllegalArgumentException { |
|
71 |
if (!canWrite(mimeType)) { |
|
72 |
throw new IllegalArgumentException("Unsupported mime type for this writer factory: " |
|
73 |
+ mimeType); |
|
74 |
} |
|
75 |
return new StaxXmlStreamWriter(out); |
|
76 |
} |
|
77 |
} |
|
0 | 78 |
org.gvsig.xml/library/trunk/org.gvsig.xml/org.gvsig.xml.prov/org.gvsig.xml.prov.stax/src/main/java/org/gvsig/xml/prov/stax/stream/StaxXmlStreamWriter.java | ||
---|---|---|
1 |
package org.gvsig.xml.prov.stax.stream; |
|
2 |
|
|
3 |
import java.io.OutputStream; |
|
4 |
import java.io.OutputStreamWriter; |
|
5 |
|
|
6 |
import javax.xml.namespace.QName; |
|
7 |
import javax.xml.stream.XMLOutputFactory; |
|
8 |
import javax.xml.stream.XMLStreamException; |
|
9 |
import javax.xml.stream.XMLStreamWriter; |
|
10 |
|
|
11 |
import org.gvsig.xml.lib.api.stream.EventType; |
|
12 |
import org.gvsig.xml.lib.api.stream.IXmlStreamWriter; |
|
13 |
import org.gvsig.xml.lib.api.stream.XmlStreamException; |
|
14 |
|
|
15 |
import com.bea.xml.stream.XMLWriterBase; |
|
16 |
|
|
17 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana |
|
18 |
* |
|
19 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana. |
|
20 |
* |
|
21 |
* This program is free software; you can redistribute it and/or |
|
22 |
* modify it under the terms of the GNU General Public License |
|
23 |
* as published by the Free Software Foundation; either version 2 |
|
24 |
* of the License, or (at your option) any later version. |
|
25 |
* |
|
26 |
* This program is distributed in the hope that it will be useful, |
|
27 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
28 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
29 |
* GNU General Public License for more details. |
|
30 |
* |
|
31 |
* You should have received a copy of the GNU General Public License |
|
32 |
* along with this program; if not, write to the Free Software |
|
33 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA. |
|
34 |
* |
|
35 |
* For more information, contact: |
|
36 |
* |
|
37 |
* Generalitat Valenciana |
|
38 |
* Conselleria d'Infraestructures i Transport |
|
39 |
* Av. Blasco Ib??ez, 50 |
|
40 |
* 46010 VALENCIA |
|
41 |
* SPAIN |
|
42 |
* |
|
43 |
* +34 963862235 |
|
44 |
* gvsig@gva.es |
|
45 |
* www.gvsig.gva.es |
|
46 |
* |
|
47 |
* or |
|
48 |
* |
|
49 |
* IVER T.I. S.A |
|
50 |
* Salamanca 50 |
|
51 |
* 46005 Valencia |
|
52 |
* Spain |
|
53 |
* |
|
54 |
* +34 963163400 |
|
55 |
* dac@iver.es |
|
56 |
*/ |
|
57 |
|
|
58 |
/** |
|
59 |
* An XML stream writer that uses a StAX writer under the hood. |
|
60 |
*/ |
|
61 |
public class StaxXmlStreamWriter implements IXmlStreamWriter { |
|
62 |
|
|
63 |
private XMLStreamWriter writer; |
|
64 |
|
|
65 |
private EventType lastTagEvent; |
|
66 |
private EventType lastEvent; |
|
67 |
|
|
68 |
private long valueLength; |
|
69 |
|
|
70 |
private long writtenValueLength; |
|
71 |
|
|
72 |
/** |
|
73 |
* nsuri for the attribute currently being processed. null if an attribute is not being |
|
74 |
* processed |
|
75 |
*/ |
|
76 |
private String attributeNamespace; |
|
77 |
|
|
78 |
/** |
|
79 |
* local name for the attribute currently being processed. null if an attribute is not being |
|
80 |
* processed |
|
81 |
*/ |
|
82 |
private String attributeLocalName; |
|
83 |
|
|
84 |
private StringBuffer attributeValue = new StringBuffer(); |
|
85 |
|
|
86 |
private StringBuffer valueBuff = new StringBuffer(); |
|
87 |
|
|
88 |
public StaxXmlStreamWriter(final OutputStream out) throws XmlStreamException { |
|
89 |
final XMLOutputFactory staxFactory = XMLOutputFactory.newInstance(); |
|
90 |
//TODO: Change the classloader of gvSIG to use SPI |
|
91 |
//try { |
|
92 |
this.writer = new XMLWriterBase(new OutputStreamWriter(out)); |
|
93 |
//} catch (XMLStreamException e) { |
|
94 |
// throw new XmlStreamException(e); |
|
95 |
//} |
|
96 |
lastEvent = EventType.NONE; |
|
97 |
} |
|
98 |
|
|
99 |
/** |
|
100 |
* @see IXmlStreamWriter#close() |
|
101 |
*/ |
|
102 |
public void close() throws XmlStreamException { |
|
103 |
if (writer != null) { |
|
104 |
try { |
|
105 |
writer.close(); |
|
106 |
} catch (XMLStreamException e) { |
|
107 |
throw new XmlStreamException(e); |
|
108 |
} |
|
109 |
writer = null; |
|
110 |
} |
|
111 |
} |
|
112 |
|
|
113 |
/** |
|
114 |
* @see IXmlStreamWriter#isOpen() |
|
115 |
*/ |
|
116 |
public boolean isOpen() { |
|
117 |
return writer != null; |
|
118 |
} |
|
119 |
|
|
120 |
/** |
|
121 |
* @throws XmlStreamException |
|
122 |
* @see IXmlStreamWriter#setDefaultNamespace(java.lang.String) |
|
123 |
*/ |
|
124 |
public void setDefaultNamespace(String defaultNamespaceUri) throws XmlStreamException { |
|
125 |
try { |
|
126 |
writer.setDefaultNamespace(defaultNamespaceUri); |
|
127 |
} catch (XMLStreamException e) { |
|
128 |
throw new XmlStreamException(e); |
|
129 |
} |
|
130 |
} |
|
131 |
|
|
132 |
/** |
|
133 |
* @throws XmlStreamException |
|
134 |
* @see IXmlStreamWriter#setPrefix(java.lang.String, java.lang.String) |
|
135 |
*/ |
|
136 |
public void setPrefix(String prefix, String namespaceUri) throws XmlStreamException { |
|
137 |
try { |
|
138 |
writer.setPrefix(prefix, namespaceUri); |
|
139 |
} catch (XMLStreamException e) { |
|
140 |
throw new XmlStreamException(e); |
|
141 |
} |
|
142 |
} |
|
143 |
|
|
144 |
/** |
|
145 |
* @see IXmlStreamWriter#setSchemaLocation(java.lang.String, java.lang.String) |
|
146 |
*/ |
|
147 |
public void setSchemaLocation(String namespaceUri, String schemaLocationUri) { |
|
148 |
throw new UnsupportedOperationException("Not yet implemented"); |
|
149 |
} |
|
150 |
|
|
151 |
/** |
|
152 |
* @see IXmlStreamWriter#flush() |
|
153 |
*/ |
|
154 |
public void flush() throws XmlStreamException { |
|
155 |
try { |
|
156 |
writer.flush(); |
|
157 |
} catch (XMLStreamException e) { |
|
158 |
throw new XmlStreamException(e); |
|
159 |
} |
|
160 |
} |
|
161 |
|
|
162 |
/** |
|
163 |
* @see IXmlStreamWriter#getLastEvent() |
|
164 |
*/ |
|
165 |
public EventType getLastEvent() { |
|
166 |
return lastEvent; |
|
167 |
} |
|
168 |
|
|
169 |
/** |
|
170 |
* @see IXmlStreamWriter#getLastTagEvent() |
|
171 |
*/ |
|
172 |
public EventType getLastTagEvent() { |
|
173 |
return lastTagEvent; |
|
174 |
} |
|
175 |
|
|
176 |
/** |
|
177 |
* @see IXmlStreamWriter#getTagDeep() |
|
178 |
*/ |
|
179 |
public int getTagDeep() { |
|
180 |
throw new UnsupportedOperationException("Not yet implemented"); |
|
181 |
} |
|
182 |
|
|
183 |
/** |
|
184 |
* @see IXmlStreamWriter#getValueLength() |
|
185 |
*/ |
|
186 |
public long getValueLength() { |
|
187 |
return valueLength; |
|
188 |
} |
|
189 |
|
|
190 |
/** |
|
191 |
* @see IXmlStreamWriter#getWrittenValueCount() |
|
192 |
*/ |
|
193 |
public long getWrittenValueCount() { |
|
194 |
return writtenValueLength; |
|
195 |
} |
|
196 |
|
|
197 |
/** |
|
198 |
* @see IXmlStreamWriter#startArray(org.gvsig.gpe.xml.stream.EventType, int) |
|
199 |
*/ |
|
200 |
public void startArray(EventType valueType, int arrayLength) throws XmlStreamException { |
|
201 |
valueLength = arrayLength; |
|
202 |
lastEvent = valueType; |
|
203 |
writtenValueLength = 0; |
|
204 |
} |
|
205 |
|
|
206 |
/** |
|
207 |
* @see IXmlStreamWriter#endArray() |
|
208 |
*/ |
|
209 |
public void endArray() throws XmlStreamException { |
|
210 |
// nothing to do |
|
211 |
} |
|
212 |
|
|
213 |
/** |
|
214 |
* @see IXmlStreamWriter#writeComment(java.lang.String) |
|
215 |
*/ |
|
216 |
public void writeComment(String commentContent) throws XmlStreamException { |
|
217 |
try { |
|
218 |
writer.writeComment(commentContent); |
|
219 |
} catch (XMLStreamException e) { |
|
220 |
throw new XmlStreamException(e); |
|
221 |
} |
|
222 |
lastEvent = EventType.COMMENT; |
|
223 |
} |
|
224 |
|
|
225 |
/** |
|
226 |
* @see IXmlStreamWriter#writeEndAttributes() |
|
227 |
*/ |
|
228 |
public void writeEndAttributes() throws XmlStreamException { |
|
229 |
flushAttribute(); |
|
230 |
lastEvent = EventType.ATTRIBUTES_END; |
|
231 |
} |
|
232 |
|
|
233 |
/** |
|
234 |
* @see IXmlStreamWriter#writeEndDocument() |
|
235 |
*/ |
|
236 |
public void writeEndDocument() throws XmlStreamException { |
|
237 |
try { |
|
238 |
writer.writeEndDocument(); |
|
239 |
} catch (XMLStreamException e) { |
|
240 |
throw new XmlStreamException(e); |
|
241 |
} |
|
242 |
lastEvent = EventType.END_DOCUMENT; |
|
243 |
} |
|
244 |
|
|
245 |
/** |
|
246 |
* @see IXmlStreamWriter#writeEndElement() |
|
247 |
*/ |
|
248 |
public void writeEndElement() throws XmlStreamException { |
|
249 |
try { |
|
250 |
writer.writeEndElement(); |
|
251 |
} catch (XMLStreamException e) { |
|
252 |
throw new XmlStreamException(e); |
|
253 |
} |
|
254 |
lastEvent = lastTagEvent = EventType.END_ELEMENT; |
|
255 |
} |
|
256 |
|
|
257 |
/** |
|
258 |
* @see IXmlStreamWriter#writeStartAttribute(java.lang.String, java.lang.String) |
|
259 |
*/ |
|
260 |
public void writeStartAttribute(String namespaceUri, String localName) |
|
261 |
throws XmlStreamException { |
|
262 |
// do we already have an attribute in course? |
|
263 |
flushAttribute(); |
|
264 |
|
|
265 |
attributeNamespace = namespaceUri; |
|
266 |
attributeLocalName = localName; |
|
267 |
attributeValue.setLength(0); |
|
268 |
lastEvent = EventType.ATTRIBUTE; |
|
269 |
} |
|
270 |
|
|
271 |
private void flushAttribute() throws XmlStreamException { |
|
272 |
if (attributeLocalName != null) { |
|
273 |
String value = attributeValue.toString(); |
|
274 |
try { |
|
275 |
writer.writeAttribute(attributeNamespace, attributeLocalName, value); |
|
276 |
} catch (XMLStreamException e) { |
|
277 |
throw new XmlStreamException(e); |
|
278 |
} |
|
279 |
attributeNamespace = null; |
|
280 |
attributeLocalName = null; |
|
281 |
attributeValue.setLength(0); |
|
282 |
} |
|
283 |
} |
|
284 |
|
|
285 |
/** |
|
286 |
* @see IXmlStreamWriter#writeStartAttribute(javax.xml.namespace.QName) |
|
287 |
*/ |
|
288 |
public void writeStartAttribute(QName qname) throws XmlStreamException { |
|
289 |
writeStartAttribute(qname.getNamespaceURI(), qname.getLocalPart()); |
|
290 |
} |
|
291 |
|
|
292 |
/** |
|
293 |
* @see IXmlStreamWriter#writeStartDocument() |
|
294 |
*/ |
|
295 |
public void writeStartDocument() throws XmlStreamException { |
|
296 |
try { |
|
297 |
writer.writeStartDocument(); |
|
298 |
} catch (XMLStreamException e) { |
|
299 |
throw new XmlStreamException(e); |
|
300 |
} |
|
301 |
lastEvent = EventType.START_DOCUMENT; |
|
302 |
} |
|
303 |
|
|
304 |
/** |
|
305 |
* @see IXmlStreamWriter#writeStartElement(java.lang.String, java.lang.String) |
|
306 |
*/ |
|
307 |
public void writeStartElement(String namespaceUri, String localName) throws XmlStreamException { |
|
308 |
try { |
|
309 |
writer.writeStartElement(namespaceUri, localName); |
|
310 |
} catch (XMLStreamException e) { |
|
311 |
throw new XmlStreamException(e); |
|
312 |
} |
|
313 |
attributeNamespace = attributeLocalName = null; |
|
314 |
lastEvent = lastTagEvent = EventType.START_ELEMENT; |
|
315 |
} |
|
316 |
|
|
317 |
/** |
|
318 |
* @see IXmlStreamWriter#writeStartElement(javax.xml.namespace.QName) |
|
319 |
*/ |
|
320 |
public void writeStartElement(QName qname) throws XmlStreamException { |
|
321 |
writeStartElement(qname.getNamespaceURI(), qname.getLocalPart()); |
|
322 |
} |
|
323 |
|
|
324 |
/** |
|
325 |
* All writeValue methods delegate to this one, which will call a writeCharacters if its an |
|
326 |
* element's value or an attribute if its an attribute value. |
|
327 |
* <p> |
|
328 |
* Whether the value corresponds to an attribute or an element's one is determined by the |
|
329 |
* nullity of the attributeLocalName field. |
|
330 |
* </p> |
|
331 |
* |
|
332 |
* @param value |
|
333 |
* @throws XmlStreamException |
|
334 |
*/ |
|
335 |
private void characters(String value) throws XmlStreamException { |
|
336 |
if (attributeLocalName == null) { |
|
337 |
// an element's value |
|
338 |
try { |
|
339 |
writer.writeCharacters(value); |
|
340 |
} catch (XMLStreamException e) { |
|
341 |
throw new XmlStreamException(e); |
|
342 |
} |
|
343 |
} else { |
|
344 |
// got a component of the current attribute value |
|
345 |
attributeValue.append(value); |
|
346 |
} |
|
347 |
} |
|
348 |
|
|
349 |
/** |
|
350 |
* @see IXmlStreamWriter#writeValue(java.lang.String) |
|
351 |
*/ |
|
352 |
public void writeValue(String value) throws XmlStreamException { |
|
353 |
characters(value); |
|
354 |
writtenValueLength++; |
|
355 |
lastEvent = EventType.VALUE_STRING; |
|
356 |
} |
|
357 |
|
|
358 |
/** |
|
359 |
* @see IXmlStreamWriter#writeValue(char[], int, int) |
|
360 |
*/ |
|
361 |
public void writeValue(char[] chars, int offset, int length) throws XmlStreamException { |
|
362 |
characters(new String(chars, offset, length)); |
|
363 |
writtenValueLength++; |
|
364 |
lastEvent = EventType.VALUE_STRING; |
|
365 |
} |
|
366 |
|
|
367 |
/** |
|
368 |
* @see IXmlStreamWriter#writeValue(int) |
|
369 |
*/ |
|
370 |
public void writeValue(int value) throws XmlStreamException { |
|
371 |
characters(String.valueOf(value)); |
|
372 |
writtenValueLength++; |
|
373 |
lastEvent = EventType.VALUE_INT; |
|
374 |
} |
|
375 |
|
|
376 |
/** |
|
377 |
* @see IXmlStreamWriter#writeValue(long) |
|
378 |
*/ |
|
379 |
public void writeValue(long value) throws XmlStreamException { |
|
380 |
characters(String.valueOf(value)); |
|
381 |
writtenValueLength++; |
|
382 |
lastEvent = EventType.VALUE_LONG; |
|
383 |
} |
|
384 |
|
|
385 |
/** |
|
386 |
* @see IXmlStreamWriter#writeValue(float) |
|
387 |
*/ |
|
388 |
public void writeValue(float value) throws XmlStreamException { |
|
389 |
characters(String.valueOf(value)); |
|
390 |
writtenValueLength++; |
|
391 |
lastEvent = EventType.VALUE_FLOAT; |
|
392 |
} |
|
393 |
|
|
394 |
/** |
|
395 |
* @see IXmlStreamWriter#writeValue(double) |
|
396 |
*/ |
|
397 |
public void writeValue(double value) throws XmlStreamException { |
|
398 |
characters(String.valueOf(value)); |
|
399 |
writtenValueLength++; |
|
400 |
lastEvent = EventType.VALUE_DOUBLE; |
|
401 |
} |
|
402 |
|
|
403 |
/** |
|
404 |
* @see IXmlStreamWriter#writeValue(boolean) |
|
405 |
*/ |
|
406 |
public void writeValue(boolean value) throws XmlStreamException { |
|
407 |
characters(String.valueOf(value)); |
|
408 |
writtenValueLength++; |
|
409 |
lastEvent = EventType.VALUE_BOOL; |
|
410 |
} |
|
411 |
|
|
412 |
/** |
|
413 |
* @see IXmlStreamWriter#writeValue(boolean[], int, int) |
|
414 |
*/ |
|
415 |
public void writeValue(boolean[] value, int offset, int length) throws XmlStreamException { |
|
416 |
valueBuff.setLength(0); |
|
417 |
for (int i = 0; i < length; i++) { |
|
418 |
valueBuff.append(value[offset + i]); |
|
419 |
if (i < length - 1) { |
|
420 |
valueBuff.append(' '); |
|
421 |
} |
|
422 |
} |
|
423 |
characters(valueBuff.toString()); |
|
424 |
writtenValueLength += length; |
|
425 |
lastEvent = EventType.VALUE_BOOL; |
|
426 |
} |
|
427 |
|
|
428 |
/** |
|
429 |
* @see IXmlStreamWriter#writeValue(int[], int, int) |
|
430 |
*/ |
|
431 |
public void writeValue(int[] value, int offset, int length) throws XmlStreamException { |
|
432 |
valueBuff.setLength(0); |
|
433 |
for (int i = 0; i < length; i++) { |
|
434 |
valueBuff.append(value[offset + i]); |
|
435 |
if (i < length - 1) { |
|
436 |
valueBuff.append(' '); |
|
437 |
} |
|
438 |
} |
|
439 |
characters(valueBuff.toString()); |
|
440 |
writtenValueLength += length; |
|
441 |
lastEvent = EventType.VALUE_INT; |
|
442 |
} |
|
443 |
|
|
444 |
/** |
|
445 |
* @see IXmlStreamWriter#writeValue(long[], int, int) |
|
446 |
*/ |
|
447 |
public void writeValue(long[] value, int offset, int length) throws XmlStreamException { |
|
448 |
valueBuff.setLength(0); |
|
449 |
for (int i = 0; i < length; i++) { |
|
450 |
valueBuff.append(value[offset + i]); |
|
451 |
if (i < length - 1) { |
|
452 |
valueBuff.append(' '); |
|
453 |
} |
|
454 |
} |
|
455 |
characters(valueBuff.toString()); |
|
456 |
writtenValueLength += length; |
|
457 |
lastEvent = EventType.VALUE_LONG; |
|
458 |
} |
|
459 |
|
|
460 |
/** |
|
461 |
* @see IXmlStreamWriter#writeValue(float[], int, int) |
|
462 |
*/ |
|
463 |
public void writeValue(float[] value, int offset, int length) throws XmlStreamException { |
|
464 |
valueBuff.setLength(0); |
|
465 |
for (int i = 0; i < length; i++) { |
|
466 |
valueBuff.append(value[offset + i]); |
|
467 |
if (i < length - 1) { |
|
468 |
valueBuff.append(' '); |
|
469 |
} |
|
470 |
} |
|
471 |
characters(valueBuff.toString()); |
|
472 |
writtenValueLength += length; |
|
473 |
lastEvent = EventType.VALUE_FLOAT; |
|
474 |
} |
|
475 |
|
|
476 |
/** |
|
477 |
* @see IXmlStreamWriter#writeValue(double[], int, int) |
|
478 |
*/ |
|
479 |
public void writeValue(double[] value, int offset, int length) throws XmlStreamException { |
|
480 |
valueBuff.setLength(0); |
|
481 |
for (int i = 0; i < length; i++) { |
|
482 |
valueBuff.append(value[offset + i]); |
|
483 |
if (i < length - 1) { |
|
484 |
valueBuff.append(','); |
|
485 |
} |
|
486 |
} |
|
487 |
valueBuff.append(' '); |
|
488 |
characters(valueBuff.toString()); |
|
489 |
writtenValueLength += length; |
|
490 |
lastEvent = EventType.VALUE_DOUBLE; |
|
491 |
} |
|
492 |
|
|
493 |
} |
|
0 | 494 |
org.gvsig.xml/library/trunk/org.gvsig.xml/org.gvsig.xml.prov/org.gvsig.xml.prov.stax/src/main/java/org/gvsig/xml/prov/stax/StaxLibrary.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.stax; |
|
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.stax.stream.StaxXmlParserFactory; |
|
34 |
|
|
35 |
/** |
|
36 |
* @author <a href="mailto:jpiera@gvsig.org">Jorge Piera</a> |
|
37 |
*/ |
|
38 |
public class StaxLibrary extends AbstractLibrary { |
|
39 |
|
|
40 |
protected void doInitialize() throws LibraryException { |
|
41 |
} |
|
42 |
|
|
43 |
protected void doPostInitialize() throws LibraryException { |
|
44 |
XMLProviderLocator.getXMLProviderManager(). |
|
45 |
registerXMLStreamReaderFactory(new StaxXmlParserFactory()); |
|
46 |
} |
|
47 |
} |
|
0 | 48 |
Also available in: Unified diff