1:
37:
38: package ;
39:
40: import ;
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48: import ;
49: import ;
50: import ;
51: import ;
52: import ;
53:
54:
59: public class XMLEventAllocatorImpl
60: implements XMLEventAllocator
61: {
62:
63: protected Map entityDeclarations;
64:
65: protected XMLEventAllocatorImpl()
66: {
67: entityDeclarations = new HashMap();
68: }
69:
70: public XMLEvent allocate(XMLStreamReader reader)
71: throws XMLStreamException
72: {
73: String text;
74: boolean whitespace;
75: boolean ignorableWhitespace;
76: int len;
77: List namespaces;
78: int eventType = reader.getEventType();
79: Location location = reader.getLocation();
80: switch (eventType)
81: {
82: case XMLStreamConstants.CDATA:
83: text = reader.getText();
84: whitespace = isWhitespace(text);
85:
86: ignorableWhitespace = whitespace && false;
87: return new CharactersImpl(location, text,
88: whitespace, true, ignorableWhitespace);
89: case XMLStreamConstants.CHARACTERS:
90: text = reader.getText();
91: whitespace = false;
92:
93: ignorableWhitespace = whitespace && false;
94: return new CharactersImpl(location, text,
95: whitespace, false, ignorableWhitespace);
96: case XMLStreamConstants.COMMENT:
97: text = reader.getText();
98: return new CommentImpl(location, text);
99: case XMLStreamConstants.DTD:
100: text = reader.getText();
101: List notations = new LinkedList();
102: List entities = new LinkedList();
103:
104: return new DTDImpl(location, text, null, notations, entities);
105: case XMLStreamConstants.END_DOCUMENT:
106: return new EndDocumentImpl(location);
107: case XMLStreamConstants.END_ELEMENT:
108: len = reader.getNamespaceCount();
109: namespaces = new LinkedList();
110: for (int i = 0; i < len; i++)
111: namespaces.add(new NamespaceImpl(location,
112: reader.getNamespacePrefix(i),
113: reader.getNamespaceURI(i),
114: false));
115: return new EndElementImpl(location,
116: reader.getName(),
117: namespaces);
118: case XMLStreamConstants.ENTITY_REFERENCE:
119: String name = reader.getLocalName();
120: EntityDeclaration decl =
121: (EntityDeclaration) entityDeclarations.get(name);
122: return new EntityReferenceImpl(location, decl, name);
123: case XMLStreamConstants.PROCESSING_INSTRUCTION:
124: return new ProcessingInstructionImpl(location,
125: reader.getPITarget(),
126: reader.getPIData());
127: case XMLStreamConstants.SPACE:
128: text = reader.getText();
129: whitespace = true;
130:
131: ignorableWhitespace = whitespace && false;
132: return new CharactersImpl(location, text,
133: whitespace, false, ignorableWhitespace);
134: case XMLStreamConstants.START_DOCUMENT:
135: String systemId = location.getSystemId();
136: String encoding = reader.getCharacterEncodingScheme();
137: boolean encodingDeclared = encoding != null;
138: if (encoding == null)
139: {
140: encoding = reader.getEncoding();
141: if (encoding == null)
142: encoding = "UTF-8";
143: }
144: String xmlVersion = reader.getVersion();
145: if (xmlVersion == null)
146: xmlVersion = "1.0";
147: boolean xmlStandalone = reader.isStandalone();
148: boolean standaloneDeclared = reader.standaloneSet();
149: return new StartDocumentImpl(location,
150: systemId,
151: encoding,
152: xmlVersion,
153: xmlStandalone,
154: standaloneDeclared,
155: encodingDeclared);
156: case XMLStreamConstants.START_ELEMENT:
157: len = reader.getNamespaceCount();
158: namespaces = new LinkedList();
159: for (int i = 0; i < len; i++)
160: namespaces.add(new NamespaceImpl(location,
161: reader.getNamespacePrefix(i),
162: reader.getNamespaceURI(i),
163: false));
164: len = reader.getAttributeCount();
165: List attributes = new LinkedList();
166: for (int i = 0; i < len; i++)
167: attributes.add(new AttributeImpl(location,
168: reader.getAttributeName(i),
169: reader.getAttributeValue(i),
170: reader.getAttributeType(i),
171: reader.isAttributeSpecified(i)));
172: return new StartElementImpl(location,
173: reader.getName(),
174: attributes, namespaces,
175: reader.getNamespaceContext());
176: default:
177: throw new XMLStreamException("Unknown event type: " + eventType);
178: }
179: }
180:
181: public void allocate(XMLStreamReader reader, XMLEventConsumer consumer)
182: throws XMLStreamException
183: {
184: consumer.add(allocate(reader));
185: }
186:
187: public XMLEventAllocator newInstance()
188: {
189: return new XMLEventAllocatorImpl();
190: }
191:
192: protected boolean isWhitespace(String text)
193: {
194: int len = text.length();
195: for (int i = 0; i < len; i++)
196: {
197: char c = text.charAt(i);
198: if (c != 0x20 && c != 0x09 && c != 0x0a && c != 0x0d)
199: return false;
200: }
201: return true;
202: }
203:
204: }