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: return new EndElementImpl(location,
115: reader.getName(),
116: namespaces);
117: case XMLStreamConstants.ENTITY_REFERENCE:
118: String name = reader.getLocalName();
119: EntityDeclaration decl =
120: (EntityDeclaration) entityDeclarations.get(name);
121: return new EntityReferenceImpl(location, decl, name);
122: case XMLStreamConstants.PROCESSING_INSTRUCTION:
123: return new ProcessingInstructionImpl(location,
124: reader.getPITarget(),
125: reader.getPIData());
126: case XMLStreamConstants.SPACE:
127: text = reader.getText();
128: whitespace = true;
129:
130: ignorableWhitespace = whitespace && false;
131: return new CharactersImpl(location, text,
132: whitespace, false, ignorableWhitespace);
133: case XMLStreamConstants.START_DOCUMENT:
134: String systemId = location.getSystemId();
135: String encoding = reader.getCharacterEncodingScheme();
136: boolean encodingDeclared = encoding != null;
137: if (encoding == null)
138: {
139: encoding = reader.getEncoding();
140: if (encoding == null)
141: encoding = "UTF-8";
142: }
143: String xmlVersion = reader.getVersion();
144: if (xmlVersion == null)
145: xmlVersion = "1.0";
146: boolean xmlStandalone = reader.isStandalone();
147: boolean standaloneDeclared = reader.standaloneSet();
148: return new StartDocumentImpl(location,
149: systemId,
150: encoding,
151: xmlVersion,
152: xmlStandalone,
153: standaloneDeclared,
154: encodingDeclared);
155: case XMLStreamConstants.START_ELEMENT:
156: len = reader.getNamespaceCount();
157: namespaces = new LinkedList();
158: for (int i = 0; i < len; i++)
159: namespaces.add(new NamespaceImpl(location,
160: reader.getNamespacePrefix(i),
161: reader.getNamespaceURI(i)));
162: len = reader.getAttributeCount();
163: List attributes = new LinkedList();
164: for (int i = 0; i < len; i++)
165: attributes.add(new AttributeImpl(location,
166: reader.getAttributeName(i),
167: reader.getAttributeValue(i),
168: QName.valueOf(reader.getAttributeType(i)),
169: reader.isAttributeSpecified(i)));
170: return new StartElementImpl(location,
171: reader.getName(),
172: attributes, namespaces,
173: reader.getNamespaceContext());
174: default:
175: throw new XMLStreamException("Unknown event type: " + eventType);
176: }
177: }
178:
179: public void allocate(XMLStreamReader reader, XMLEventConsumer consumer)
180: throws XMLStreamException
181: {
182: consumer.add(allocate(reader));
183: }
184:
185: public XMLEventAllocator newInstance()
186: {
187: return new XMLEventAllocatorImpl();
188: }
189:
190: protected boolean isWhitespace(String text)
191: {
192: int len = text.length();
193: for (int i = 0; i < len; i++)
194: {
195: char c = text.charAt(i);
196: if (c != 0x20 && c != 0x09 && c != 0x0a && c != 0x0d)
197: return false;
198: }
199: return true;
200: }
201:
202: }