001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.axis2.databinding.utils.reader;
020:
021: import org.apache.axis2.databinding.utils.ConverterUtil;
022:
023: import javax.activation.DataHandler;
024: import javax.xml.namespace.NamespaceContext;
025: import javax.xml.namespace.QName;
026: import javax.xml.stream.Location;
027: import javax.xml.stream.XMLStreamException;
028:
029: public class ADBDataHandlerStreamReader implements ADBXMLStreamReader {
030: private static final int START_ELEMENT_STATE = 0;
031: private static final int TEXT_STATE = 1;
032: private static final int END_ELEMENT_STATE = 2;
033:
034: private ADBNamespaceContext namespaceContext = new ADBNamespaceContext();
035:
036: private QName name;
037: private DataHandler value;
038:
039: private int state = START_ELEMENT_STATE;
040: //initiate at the start element state
041:
042: //keeps track whether the namespace is declared
043: //false by default
044: private boolean nsDeclared = false;
045:
046: public ADBDataHandlerStreamReader(QName name, DataHandler value) {
047: this .name = name;
048: this .value = value;
049: }
050:
051: private String convertedText = null;
052:
053: /**
054: * Return the right properties for the optimization
055: *
056: * @param propKey
057: * @throws IllegalArgumentException
058: */
059: public Object getProperty(String propKey)
060: throws IllegalArgumentException {
061: if (OPTIMIZATION_ENABLED.equals(propKey)) {
062: return Boolean.TRUE;
063: }
064: if (state == TEXT_STATE) {
065: if (IS_BINARY.equals(propKey)) {
066: return Boolean.TRUE;
067: } else if (DATA_HANDLER.equals(propKey)) {
068: return value;
069: }
070: }
071: return null;
072:
073: }
074:
075: public int next() throws XMLStreamException {
076: //no need to handle null here. it should have been handled
077: //already
078: switch (state) {
079: case START_ELEMENT_STATE:
080: state = TEXT_STATE;
081: return CHARACTERS;
082: case END_ELEMENT_STATE:
083: //oops, not supposed to happen!
084: throw new XMLStreamException("end already reached!");
085: case TEXT_STATE:
086: state = END_ELEMENT_STATE;
087: return END_ELEMENT;
088: default:
089: throw new XMLStreamException("unknown event type!");
090: }
091: }
092:
093: public void require(int i, String string, String string1)
094: throws XMLStreamException {
095: //not implemented
096: }
097:
098: public String getElementText() throws XMLStreamException {
099: if (state == START_ELEMENT) {
100: //move to the end state and return the value
101: state = END_ELEMENT_STATE;
102: if (convertedText == null) {
103: convertedText = ConverterUtil
104: .getStringFromDatahandler(value);
105: }
106: return convertedText;
107: } else {
108: throw new XMLStreamException();
109: }
110:
111: }
112:
113: public int nextTag() throws XMLStreamException {
114: return 0;//todo
115: }
116:
117: public boolean hasNext() throws XMLStreamException {
118: return (state != END_ELEMENT_STATE);
119: }
120:
121: public void close() throws XMLStreamException {
122: //Do nothing - we've nothing to free here
123: }
124:
125: public String getNamespaceURI(String prefix) {
126: return namespaceContext.getNamespaceURI(prefix);
127: }
128:
129: public boolean isStartElement() {
130: return (state == START_ELEMENT_STATE);
131: }
132:
133: public boolean isEndElement() {
134: return (state == END_ELEMENT_STATE);
135: }
136:
137: public boolean isCharacters() {
138: return (state == TEXT_STATE);
139: }
140:
141: public boolean isWhiteSpace() {
142: return false; //no whitespaces here
143: }
144:
145: public String getAttributeValue(String string, String string1) {
146: return null;
147: }
148:
149: public int getAttributeCount() {
150: return 0;
151: }
152:
153: public QName getAttributeName(int i) {
154: return null;
155: }
156:
157: public String getAttributeNamespace(int i) {
158: return null;
159: }
160:
161: public String getAttributeLocalName(int i) {
162: return null;
163: }
164:
165: public String getAttributePrefix(int i) {
166: return null;
167: }
168:
169: public String getAttributeType(int i) {
170: return null;
171: }
172:
173: public String getAttributeValue(int i) {
174: return null;
175: }
176:
177: public boolean isAttributeSpecified(int i) {
178: return false; //no attribs here
179: }
180:
181: public int getNamespaceCount() {
182: return (nsDeclared) ? 1 : 0;
183: }
184:
185: public String getNamespacePrefix(int i) {
186: return (nsDeclared && i == 0) ? name.getPrefix() : null;
187: }
188:
189: public String getNamespaceURI(int i) {
190: return (nsDeclared && i == 0) ? name.getNamespaceURI() : null;
191: }
192:
193: public NamespaceContext getNamespaceContext() {
194: return this .namespaceContext;
195: }
196:
197: public int getEventType() {
198: switch (state) {
199: case START_ELEMENT_STATE:
200: return START_ELEMENT;
201: case END_ELEMENT_STATE:
202: return END_ELEMENT;
203: case TEXT_STATE:
204: return CHARACTERS;
205: default:
206: throw new UnsupportedOperationException();
207: //we've no idea what this is!!!!!
208: }
209:
210: }
211:
212: public String getText() {
213: if (state == TEXT_STATE) {
214: if (convertedText == null) {
215: convertedText = ConverterUtil
216: .getStringFromDatahandler(value);
217: }
218: return convertedText;
219: } else {
220: throw new IllegalStateException();
221: }
222: }
223:
224: public char[] getTextCharacters() {
225: if (state == TEXT_STATE) {
226: if (convertedText == null) {
227: convertedText = ConverterUtil
228: .getStringFromDatahandler(value);
229: }
230: return convertedText.toCharArray();
231: } else {
232: throw new IllegalStateException();
233: }
234: }
235:
236: public int getTextCharacters(int i, char[] chars, int i1, int i2)
237: throws XMLStreamException {
238: //not implemented
239: throw new UnsupportedOperationException();
240: }
241:
242: public int getTextStart() {
243: if (state == TEXT_STATE) {
244: return 0;
245: } else {
246: throw new IllegalStateException();
247: }
248: }
249:
250: public int getTextLength() {
251: if (state == TEXT_STATE) {
252: if (convertedText == null) {
253: convertedText = ConverterUtil
254: .getStringFromDatahandler(value);
255: }
256: return convertedText.length();
257: } else {
258: throw new IllegalStateException();
259: }
260:
261: }
262:
263: public String getEncoding() {
264: return null;
265: }
266:
267: public boolean hasText() {
268: return (state == TEXT_STATE);
269: }
270:
271: public Location getLocation() {
272: return new Location() {
273: public int getLineNumber() {
274: return 0;
275: }
276:
277: public int getColumnNumber() {
278: return 0;
279: }
280:
281: public int getCharacterOffset() {
282: return 0;
283: }
284:
285: public String getPublicId() {
286: return null;
287: }
288:
289: public String getSystemId() {
290: return null;
291: }
292: };
293: }
294:
295: public QName getName() {
296: if (state != TEXT_STATE) {
297: return name;
298: } else {
299: return null;
300: }
301: }
302:
303: public String getLocalName() {
304: if (state != TEXT_STATE) {
305: return name.getLocalPart();
306: } else {
307: return null;
308: }
309: }
310:
311: public boolean hasName() {
312: return (state != TEXT_STATE);
313:
314: }
315:
316: public String getNamespaceURI() {
317: if (state != TEXT_STATE) {
318: return name.getNamespaceURI();
319: } else {
320: return null;
321: }
322:
323: }
324:
325: public String getPrefix() {
326: if (state != TEXT_STATE) {
327: return name.getPrefix();
328: } else {
329: return null;
330: }
331: }
332:
333: public String getVersion() {
334: return null; //todo 1.0 ?
335: }
336:
337: public boolean isStandalone() {
338: return false;
339: }
340:
341: public boolean standaloneSet() {
342: return false;
343: }
344:
345: public String getCharacterEncodingScheme() {
346: return null;
347: }
348:
349: public String getPITarget() {
350: return null;
351: }
352:
353: public String getPIData() {
354: return null;
355: }
356:
357: public boolean isDone() {
358: return (state == END_ELEMENT_STATE);
359: }
360:
361: public void addNamespaceContext(NamespaceContext nsContext) {
362: this .namespaceContext.setParentNsContext(nsContext);
363: }
364:
365: public void init() {
366: //just add the current elements namespace and prefix to the this
367: //elements nscontext
368: addToNsMap(name.getPrefix(), name.getNamespaceURI());
369:
370: }
371:
372: /**
373: * @param prefix
374: * @param uri
375: */
376: private void addToNsMap(String prefix, String uri) {
377: //todo - need to fix this up to cater for cases where
378: //namespaces are having no prefixes
379: if (!uri.equals(namespaceContext.getNamespaceURI(prefix))) {
380: //this namespace is not there. Need to declare it
381: namespaceContext.pushNamespace(prefix, uri);
382: nsDeclared = true;
383: }
384: }
385:
386: }
|