001: /*
002: * The contents of this file are subject to the terms
003: * of the Common Development and Distribution License
004: * (the License). You may not use this file except in
005: * compliance with the License.
006: *
007: * You can obtain a copy of the license at
008: * https://glassfish.dev.java.net/public/CDDLv1.0.html.
009: * See the License for the specific language governing
010: * permissions and limitations under the License.
011: *
012: * When distributing Covered Code, include this CDDL
013: * Header Notice in each file and include the License file
014: * at https://glassfish.dev.java.net/public/CDDLv1.0.html.
015: * If applicable, add the following below the CDDL Header,
016: * with the fields enclosed by brackets [] replaced by
017: * you own identifying information:
018: * "Portions Copyrighted [year] [name of copyright owner]"
019: *
020: * Copyright 2006 Sun Microsystems Inc. All Rights Reserved
021: */
022:
023: /*
024: * StAXC14nCanonicalizerImpl.java
025: *
026: * Created on August 22, 2005, 4:07 AM
027: *
028: * To change this template, choose Tools | Options and locate the template under
029: * the Source Creation and Management node. Right-click the template and choose
030: * Open. You can then make changes to the template in the Source Editor.
031: */
032:
033: package com.sun.xml.wss.impl.c14n;
034:
035: import com.sun.xml.wss.impl.misc.UnsyncByteArrayOutputStream;
036: import java.io.IOException;
037: import java.util.ArrayList;
038: import java.util.List;
039:
040: import javax.xml.stream.XMLStreamException;
041: import javax.xml.stream.XMLStreamWriter;
042: import org.xml.sax.helpers.NamespaceSupport;
043:
044: /**
045: *
046: * @author K.Venugopal@sun.com
047: */
048:
049: /*
050: * Test,
051: * Improve performance
052: * handle PI
053: */
054: public class StAXC14nCanonicalizerImpl extends BaseCanonicalizer
055: implements XMLStreamWriter {
056:
057: boolean closeStartTag = false;
058: NamespaceSupport nsContext = new NamespaceSupport();
059: private javax.xml.namespace.NamespaceContext namespaceContext = null;
060: ElementName[] elementNames = new ElementName[10];
061:
062: protected UnsyncByteArrayOutputStream elemBuffer = null;
063:
064: /** Creates a new instance of StAXC14nCanonicalizerImpl */
065: public StAXC14nCanonicalizerImpl() {
066: //_attrResult = new TreeSet(new StAXAttrSorter(false));
067: _attrResult = new ArrayList();
068: for (int i = 0; i < 4; i++) {
069: _attrs.add(new StAXAttr());
070: }
071: for (int i = 0; i < 10; i++) {
072: elementNames[i] = new ElementName();
073: }
074: elemBuffer = new UnsyncByteArrayOutputStream();
075: }
076:
077: /**
078: * This method has not effect when called.
079: *
080: * @throws javax.xml._stream.XMLStreamException {@inheritDoc}
081: */
082: public void close() throws XMLStreamException {
083: //no-op
084: }
085:
086: public void flush() throws XMLStreamException {
087: //no-op
088: }
089:
090: public javax.xml.namespace.NamespaceContext getNamespaceContext() {
091: return namespaceContext;
092: }
093:
094: public NamespaceSupport getNSContext() {
095: return nsContext;
096: }
097:
098: public String getPrefix(String namespaceURI)
099: throws XMLStreamException {
100: return nsContext.getPrefix(namespaceURI);
101: }
102:
103: public Object getProperty(String str)
104: throws IllegalArgumentException {
105: //throw new UnsupportedOperationException();
106: return null;
107: }
108:
109: public void setDefaultNamespace(String str)
110: throws XMLStreamException {
111: nsContext.declarePrefix("", str);
112: }
113:
114: public void setNamespaceContext(
115: javax.xml.namespace.NamespaceContext namespaceContext)
116: throws XMLStreamException {
117: this .namespaceContext = namespaceContext;
118: }
119:
120: public void setPrefix(String str, String str1)
121: throws XMLStreamException {
122: nsContext.declarePrefix(str, str1);
123: }
124:
125: /**
126: * Creates a DOM Atrribute @see org.w3c.dom.Node and associates it with the current DOM element @see org.w3c.dom.Node.
127: *
128: * @param localName {@inheritDoc}
129: * @param value {@inheritDoc}
130: * @throws javax.xml._stream.XMLStreamException {@inheritDoc}
131: */
132: public void writeAttribute(String localName, String value)
133: throws XMLStreamException {
134: writeAttribute("", "", localName, value);
135: }
136:
137: public void writeAttribute(String namespaceURI, String localName,
138: String value) throws XMLStreamException {
139: String prefix = "";
140: prefix = nsContext.getPrefix(namespaceURI);
141: writeAttribute(prefix, "", localName, value);
142: }
143:
144: public void writeAttribute(String prefix, String namespaceURI,
145: String localName, String value) throws XMLStreamException {
146: StAXAttr attr = getAttribute();
147: attr.setLocalName(localName);
148: attr.setValue(value);
149: attr.setPrefix(prefix);
150: attr.setUri(namespaceURI);
151: _attrResult.add(attr);
152: }
153:
154: public void writeCData(String data) throws XMLStreamException {
155: try {
156: closeStartTag();
157: outputTextToWriter(data, _stream);
158: } catch (IOException ex) {
159: throw new RuntimeException(ex);
160: }
161: }
162:
163: public void writeCharacters(String charData)
164: throws XMLStreamException {
165: try {
166: closeStartTag();
167: outputTextToWriter(charData, _stream);
168: } catch (IOException ex) {
169: throw new RuntimeException(ex);
170: }
171: }
172:
173: public void writeCharacters(char[] values, int param, int param2)
174: throws XMLStreamException {
175: try {
176: closeStartTag();
177: outputTextToWriter(values, param, param2, _stream);
178: } catch (IOException ex) {
179: throw new RuntimeException(ex);
180: }
181: }
182:
183: public void writeComment(String str) throws XMLStreamException {
184:
185: }
186:
187: public void writeDTD(String str) throws XMLStreamException {
188: throw new UnsupportedOperationException();
189: }
190:
191: public void writeDefaultNamespace(String namespaceURI)
192: throws XMLStreamException {
193: _defURI = namespaceURI;
194: writeNamespace("", namespaceURI);
195:
196: }
197:
198: public void writeEmptyElement(String localName)
199: throws XMLStreamException {
200: writeEmptyElement("", localName, "");
201: }
202:
203: public void writeEmptyElement(String namespaceURI, String localName)
204: throws XMLStreamException {
205:
206: String prefix = nsContext.getPrefix(namespaceURI);
207: writeEmptyElement(prefix, localName, namespaceURI);
208: }
209:
210: public void writeEmptyElement(String prefix, String localName,
211: String namespaceURI) throws XMLStreamException {
212: closeStartTag();
213:
214: try {
215: _stream.write('<');
216: if ((prefix != null && prefix.length() == 0)
217: || prefix == null) {
218: writeStringToUtf8(localName, _stream);
219: } else {
220: writeStringToUtf8(prefix, _stream);
221: writeStringToUtf8(":", _stream);
222: writeStringToUtf8(localName, _stream);
223:
224: }
225:
226: _stream.write('>');
227: _stream.write(_END_TAG);
228: if ((prefix != null && prefix.length() == 0)
229: || prefix == null) {
230: writeStringToUtf8(localName, _stream);
231: } else {
232: writeStringToUtf8(prefix, _stream);
233: writeStringToUtf8(":", _stream);
234: writeStringToUtf8(localName, _stream);
235:
236: }
237: _stream.write('>');
238: } catch (IOException ex) {
239: throw new RuntimeException(ex);
240: }
241:
242: }
243:
244: public void writeEndDocument() throws XMLStreamException {
245: while (_depth > 0) {
246: writeEndElement();
247: }
248: }
249:
250: public void writeEndElement() throws XMLStreamException {
251: //ElementName qname = elementNames[--_depth];
252: closeStartTag();
253: if (_depth == 0) {
254: return;
255: }
256: if (_ncContextState[_depth]) {
257: nsContext.popContext();
258: }
259: try {
260: _stream.write(_END_TAG);
261: //writeStringToUtf8 (qname,_stream);
262: ElementName en = elementNames[--_depth];
263: _stream.write(en.getUtf8Data().getBytes(), 0, en
264: .getUtf8Data().getLength());
265: en.getUtf8Data().reset();
266: _stream.write('>');
267: } catch (IOException ex) {
268: throw new RuntimeException(ex);
269: }
270: }
271:
272: public void writeEntityRef(String str) throws XMLStreamException {
273: throw new UnsupportedOperationException();
274: }
275:
276: public void writeNamespace(String prefix, String namespaceURI)
277: throws XMLStreamException {
278: String duri = nsContext.getURI(prefix);
279: boolean add = false;
280: if (duri == null || !duri.equals(namespaceURI)) {
281: add = true;
282: }
283: if (add && !_ncContextState[_depth - 1]) {
284: nsContext.pushContext();
285: _ncContextState[_depth - 1] = true;
286: }
287: if (add) {
288: nsContext.declarePrefix(prefix, namespaceURI);
289: AttributeNS attrNS = getAttributeNS();
290: attrNS.setPrefix(prefix);
291: attrNS.setUri(namespaceURI);
292: _nsResult.add(attrNS);
293: }
294: }
295:
296: public void writeProcessingInstruction(String target)
297: throws XMLStreamException {
298: try {
299: outputPItoWriter(target, "", _stream);
300: } catch (IOException ex) {
301: throw new RuntimeException(ex);
302: }
303: }
304:
305: public void writeProcessingInstruction(String target, String data)
306: throws XMLStreamException {
307: try {
308: outputPItoWriter(target, data, _stream);
309: } catch (IOException ex) {
310: throw new RuntimeException(ex);
311: }
312: }
313:
314: public void writeStartDocument() throws XMLStreamException {
315: }
316:
317: public void writeStartDocument(String version)
318: throws XMLStreamException {
319: }
320:
321: public void writeStartDocument(String encoding, String version)
322: throws XMLStreamException {
323: }
324:
325: public void writeStartElement(String localName)
326: throws XMLStreamException {
327: writeStartElement("", localName, "");
328: }
329:
330: public void writeStartElement(String namespaceURI, String localName)
331: throws XMLStreamException {
332: String prefix = nsContext.getPrefix(namespaceURI);
333: writeStartElement(prefix, localName, namespaceURI);
334: }
335:
336: public void writeStartElement(String prefix, String localName,
337: String namespaceURI) throws XMLStreamException {
338:
339: closeStartTag();
340: elemBuffer.reset();
341: UnsyncByteArrayOutputStream buffWriter = null;
342: try {
343: if (prefix != null && prefix.length() > 0) {
344: buffWriter = elementNames[_depth].getUtf8Data();
345: writeStringToUtf8(prefix, buffWriter);
346: writeStringToUtf8(":", buffWriter);
347: writeStringToUtf8(localName, buffWriter);
348: _elementPrefix = prefix;
349: } else {
350: buffWriter = elementNames[_depth].getUtf8Data();
351: writeStringToUtf8(localName, buffWriter);
352:
353: }
354: } catch (Exception ex) {
355: throw new RuntimeException(ex);
356: }
357:
358: //byte [] data = elemBuffer.toByteArray();
359: //byte [] data = elemBuffer.getBytes();
360: _ncContextState[_depth] = false;
361:
362: _depth++;
363: resizeElementStack();
364: try {
365: _stream.write('<');
366:
367: _stream.write(buffWriter.getBytes(), 0, buffWriter
368: .getLength());
369: closeStartTag = true;
370: } catch (IOException ex) {
371: throw new RuntimeException(ex);
372: }
373: }
374:
375: protected void closeStartTag() throws XMLStreamException {
376: try {
377: if (closeStartTag) {
378: //Iterator itr = _nsResult.iterator();
379: if (_defURI != null) {
380: outputAttrToWriter("xmlns", _defURI, _stream);
381: }
382:
383: //writeAttributesNS(itr);
384:
385: if (_nsResult.size() > 0) {
386: writeAttributesNS(_nsResult.iterator());
387: }
388:
389: if (_attrResult.size() > 0) {
390: writeAttributes(_attrResult);
391: }
392:
393: _nsResult.clear();
394: _attrResult.clear();
395: _stream.write('>');
396: closeStartTag = false;
397: _defURI = null;
398: }
399: } catch (IOException ex) {
400: throw new RuntimeException(ex);
401: }
402: }
403:
404: protected StAXAttr getAttribute() {
405: if (_attrPos < _attrs.size()) {
406: return (StAXAttr) _attrs.get(_attrPos++);
407: } else {
408: for (int i = 0; i < initalCacheSize; i++) {
409: _attrs.add(new StAXAttr());
410: }
411: return (StAXAttr) _attrs.get(_attrPos++);
412: }
413: }
414:
415: protected void resizeElementStack() {
416: if (_depth >= elementNames.length) {
417: ElementName[] tmp = new ElementName[elementNames.length + 10];
418: System.arraycopy(elementNames, 0, tmp, 0,
419: elementNames.length);
420: elementNames = tmp;
421: for (int i = 10; i < elementNames.length; i++) {
422: elementNames[i] = new ElementName();
423: }
424: }
425: }
426:
427: protected void writeAttributes(List itr) throws IOException {
428: ///while(itr.hasNext()){
429: int size = itr.size();
430: for (int i = 0; i < size; i++) {
431: StAXAttr attr = (StAXAttr) itr.get(i);
432: String prefix = attr.getPrefix();
433: if (prefix != null && prefix.length() != 0) {
434: /// attrName.setLength(0);
435: /// attrName.append(prefix);
436: /// attrName.append(":");
437: /// attrName.append(attr.getLocalName());
438: /// prefix = attrName.toString();
439: outputAttrToWriter(prefix, attr.getLocalName(), attr
440: .getValue(), _stream);
441: } else {
442: prefix = attr.getLocalName();
443: outputAttrToWriter(prefix, attr.getValue(), _stream);
444: }
445:
446: //outputAttrToWriter(prefix,attr.getValue(),_stream);
447: }
448: }
449:
450: }
|