001: /*
002: * $Id: XMLEventAllocatorImpl.java,v 1.3 2007/03/02 21:29:48 spericas Exp $
003: */
004:
005: /*
006: * The contents of this file are subject to the terms
007: * of the Common Development and Distribution License
008: * (the License). You may not use this file except in
009: * compliance with the License.
010: *
011: * You can obtain a copy of the license at
012: * https://glassfish.dev.java.net/public/CDDLv1.0.html.
013: * See the License for the specific language governing
014: * permissions and limitations under the License.
015: *
016: * When distributing Covered Code, include this CDDL
017: * Header Notice in each file and include the License file
018: * at https://glassfish.dev.java.net/public/CDDLv1.0.html.
019: * If applicable, add the following below the CDDL Header,
020: * with the fields enclosed by brackets [] replaced by
021: * you own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * [Name of File] [ver.__] [Date]
025: *
026: * Copyright 2006 Sun Microsystems Inc. All Rights Reserved
027: */
028:
029: package com.sun.xml.stream.events;
030:
031: import javax.xml.stream.util.XMLEventAllocator;
032: import javax.xml.stream.*;
033: import javax.xml.stream.events.*;
034: import javax.xml.XMLConstants;
035: import javax.xml.namespace.QName;
036: import com.sun.xml.stream.xerces.util.NamespaceContextWrapper;
037: import com.sun.xml.stream.xerces.util.NamespaceSupport;
038:
039: /**
040: * Implementation of XMLEvent Allocator.
041: * @author Neeraj.bajaj@sun.com, k.venugopal@sun.com
042: */
043: public class XMLEventAllocatorImpl implements XMLEventAllocator {
044:
045: /** Creates a new instance of XMLEventAllocator */
046: public XMLEventAllocatorImpl() {
047: }
048:
049: public javax.xml.stream.events.XMLEvent allocate(
050: javax.xml.stream.XMLStreamReader xMLStreamReader)
051: throws javax.xml.stream.XMLStreamException {
052: if (xMLStreamReader == null)
053: throw new XMLStreamException("Reader cannot be null");
054: // allocate is not supposed to change the state of the reader so we shouldn't be calling next.
055: // return getNextEvent(xMLStreamReader);
056: return getXMLEvent(xMLStreamReader);
057: }
058:
059: public void allocate(
060: javax.xml.stream.XMLStreamReader xMLStreamReader,
061: javax.xml.stream.util.XMLEventConsumer xMLEventConsumer)
062: throws javax.xml.stream.XMLStreamException {
063: XMLEvent currentEvent = getXMLEvent(xMLStreamReader);
064: if (currentEvent != null)
065: xMLEventConsumer.add(currentEvent);
066:
067: return;
068: }
069:
070: public javax.xml.stream.util.XMLEventAllocator newInstance() {
071: return new XMLEventAllocatorImpl();
072: }
073:
074: //REVISIT: shouldn't we be using XMLEventFactory to create events.
075: XMLEvent getXMLEvent(XMLStreamReader streamReader) {
076: XMLEvent event = null;
077: //returns the current event
078: int eventType = streamReader.getEventType();
079: switch (eventType) {
080:
081: case XMLEvent.START_ELEMENT: {
082: StartElementEvent startElementEvent = new StartElementEvent(
083: getQName(streamReader));
084: fillAttributes(startElementEvent, streamReader);
085: //we might have different XMLStreamReader so check every time for the namespace aware property
086: //we should be setting namespace related values only when isNamespaceAware is 'true'
087: if (((Boolean) streamReader
088: .getProperty(XMLInputFactory.IS_NAMESPACE_AWARE))
089: .booleanValue()) {
090: fillNamespaceAttributes(startElementEvent, streamReader);
091: setNamespaceContext(startElementEvent, streamReader);
092: }
093:
094: startElementEvent.setLocation(streamReader.getLocation());
095: event = startElementEvent;
096: break;
097: }
098: case XMLEvent.END_ELEMENT: {
099: EndElementEvent endElementEvent = new EndElementEvent(
100: getQName(streamReader));
101: endElementEvent.setLocation(streamReader.getLocation());
102:
103: if (((Boolean) streamReader
104: .getProperty(XMLInputFactory.IS_NAMESPACE_AWARE))
105: .booleanValue()) {
106: fillNamespaceAttributes(endElementEvent, streamReader);
107: }
108: event = endElementEvent;
109: break;
110: }
111: case XMLEvent.PROCESSING_INSTRUCTION: {
112: ProcessingInstructionEvent piEvent = new ProcessingInstructionEvent(
113: streamReader.getPITarget(), streamReader
114: .getPIData());
115: piEvent.setLocation(streamReader.getLocation());
116: event = piEvent;
117: break;
118: }
119: case XMLEvent.CHARACTERS: {
120: CharacterEvent cDataEvent = new CharacterEvent(streamReader
121: .getText());
122: cDataEvent.setLocation(streamReader.getLocation());
123: event = cDataEvent;
124: break;
125: }
126: case XMLEvent.COMMENT: {
127: CommentEvent commentEvent = new CommentEvent(streamReader
128: .getText());
129: commentEvent.setLocation(streamReader.getLocation());
130: event = commentEvent;
131: break;
132: }
133: case XMLEvent.START_DOCUMENT: {
134: StartDocumentEvent sdEvent = new StartDocumentEvent();
135: sdEvent.setVersion(streamReader.getVersion());
136: sdEvent.setEncoding(streamReader.getEncoding());
137: if (streamReader.getCharacterEncodingScheme() != null) {
138: sdEvent.setDeclaredEncoding(true);
139: } else {
140: sdEvent.setDeclaredEncoding(false);
141: }
142: sdEvent.setStandalone(streamReader.isStandalone());
143: sdEvent.setLocation(streamReader.getLocation());
144: event = sdEvent;
145: break;
146: }
147: case XMLEvent.END_DOCUMENT: {
148: EndDocumentEvent endDocumentEvent = new EndDocumentEvent();
149: endDocumentEvent.setLocation(streamReader.getLocation());
150: event = endDocumentEvent;
151: break;
152: }
153: case XMLEvent.ENTITY_REFERENCE: {
154: EntityReferenceEvent entityEvent = new EntityReferenceEvent(
155: streamReader.getLocalName(),
156: new EntityDeclarationImpl(streamReader
157: .getLocalName(), streamReader.getText()));
158: entityEvent.setLocation(streamReader.getLocation());
159: event = entityEvent;
160: break;
161:
162: }
163: case XMLEvent.ATTRIBUTE: {
164: event = null;
165: break;
166: }
167: case XMLEvent.DTD: {
168: event = new DTDEvent(streamReader.getText());
169: break;
170: }
171: case XMLEvent.CDATA: {
172: CharacterEvent cDataEvent = new CharacterEvent(streamReader
173: .getText(), true);
174: cDataEvent.setLocation(streamReader.getLocation());
175: event = cDataEvent;
176: break;
177: }
178: case XMLEvent.SPACE: {
179: CharacterEvent spaceEvent = new CharacterEvent(streamReader
180: .getText(), false, true);
181: spaceEvent.setLocation(streamReader.getLocation());
182: event = spaceEvent;
183: break;
184: }
185: }
186: return event;
187: }
188:
189: //this function is not used..
190: protected XMLEvent getNextEvent(XMLStreamReader streamReader)
191: throws XMLStreamException {
192: //advance the reader to next event.
193: streamReader.next();
194: return getXMLEvent(streamReader);
195: }
196:
197: protected void fillAttributes(StartElementEvent event,
198: XMLStreamReader xmlr) {
199:
200: int len = xmlr.getAttributeCount();
201: QName qname = null;
202: String prefix = null;
203: String localpart = null;
204: AttributeImpl attr = null;
205: NamespaceImpl nattr = null;
206: for (int i = 0; i < len; i++) {
207: qname = xmlr.getAttributeName(i);
208: prefix = qname.getPrefix();
209: localpart = qname.getLocalPart();
210: //this method doesn't include namespace declarations
211: //so we can be sure that there wont be any namespace declaration as part of this function call
212: //we can avoid this check - nb.
213: /**
214: * if (prefix.equals(XMLConstants.XMLNS_ATTRIBUTE) ) {
215: * attr = new NamespaceImpl(localpart,xmlr.getAttributeValue(i));
216: * }else if (prefix.equals(XMLConstants.DEFAULT_NS_PREFIX)){
217: * attr = new NamespaceImpl(xmlr.getAttributeValue(i));
218: * }else{
219: * attr = new AttributeImpl();
220: * attr.setName(qname);
221: * }
222: **/
223: attr = new AttributeImpl();
224: attr.setName(qname);
225: attr.setAttributeType(xmlr.getAttributeType(i));
226: attr.setSpecified(xmlr.isAttributeSpecified(i));
227: attr.setValue(xmlr.getAttributeValue(i));
228: event.addAttribute(attr);
229: }
230: }
231:
232: protected void fillNamespaceAttributes(StartElementEvent event,
233: XMLStreamReader xmlr) {
234: int count = xmlr.getNamespaceCount();
235: String uri = null;
236: String prefix = null;
237: NamespaceImpl attr = null;
238: for (int i = 0; i < count; i++) {
239: uri = xmlr.getNamespaceURI(i);
240: prefix = xmlr.getNamespacePrefix(i);
241: if (prefix == null) {
242: prefix = XMLConstants.DEFAULT_NS_PREFIX;
243: }
244: attr = new NamespaceImpl(prefix, uri);
245: event.addNamespaceAttribute(attr);
246: }
247: }
248:
249: protected void fillNamespaceAttributes(EndElementEvent event,
250: XMLStreamReader xmlr) {
251: int count = xmlr.getNamespaceCount();
252: String uri = null;
253: String prefix = null;
254: NamespaceImpl attr = null;
255: for (int i = 0; i < count; i++) {
256: uri = xmlr.getNamespaceURI(i);
257: prefix = xmlr.getNamespacePrefix(i);
258: if (prefix == null) {
259: prefix = XMLConstants.DEFAULT_NS_PREFIX;
260: }
261: attr = new NamespaceImpl(prefix, uri);
262: event.addNamespace(attr);
263: }
264: }
265:
266: //Revisit : Creating a new Namespacecontext for now.
267: //see if we can do better job.
268: private void setNamespaceContext(StartElementEvent event,
269: XMLStreamReader xmlr) {
270: NamespaceContextWrapper contextWrapper = (NamespaceContextWrapper) xmlr
271: .getNamespaceContext();
272: NamespaceSupport ns = new NamespaceSupport(contextWrapper
273: .getNamespaceContext());
274: event.setNamespaceContext(new NamespaceContextWrapper(ns));
275: }
276:
277: private QName getQName(XMLStreamReader xmlr) {
278: return new QName(xmlr.getNamespaceURI(), xmlr.getLocalName(),
279: xmlr.getPrefix());
280: }
281: }
|