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: * StAXEC14nCanonicalizerImpl.java
025: *
026: * Created on August 22, 2005, 7:14 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.HashSet;
038: import java.util.Iterator;
039: import java.util.List;
040: import javax.xml.namespace.NamespaceContext;
041: import javax.xml.stream.XMLStreamException;
042: import static javax.xml.stream.XMLStreamConstants.*;
043:
044: /**
045: *
046: * @author K.Venugopal@sun.com
047: */
048: public class StAXEXC14nCanonicalizerImpl extends
049: StAXC14nCanonicalizerImpl {
050:
051: private List inclusivePrefixList = null;
052: private HashSet visiblyUtilized = new HashSet();
053:
054: private UnsyncByteArrayOutputStream tmpBuffer = null;
055:
056: NamespaceContextImpl exC14NContext = new NamespaceContextImpl();
057:
058: /** Creates a new instance of StAXEC14nCanonicalizerImpl */
059: public StAXEXC14nCanonicalizerImpl() {
060: super ();
061: tmpBuffer = new UnsyncByteArrayOutputStream();
062:
063: }
064:
065: public void reset() {
066: super .reset();
067: exC14NContext.reset();
068:
069: }
070:
071: public void setInclusivePrefixList(List values) {
072: this .inclusivePrefixList = values;
073: }
074:
075: public void writeNamespace(String prefix, String namespaceURI)
076: throws XMLStreamException {
077: if (prefix == null || prefix.length() == 0) {
078: String defNS = exC14NContext.getNamespaceURI(prefix);
079: if ((defNS == null || defNS.length() == 0)
080: && (namespaceURI == null || namespaceURI.length() == 0)) {
081: return;
082: }
083: if (namespaceURI == null) {
084: namespaceURI = "";
085: }
086: _defURI = namespaceURI;
087: prefix = "";
088: }
089: exC14NContext.declareNamespace(prefix, namespaceURI);
090: }
091:
092: public void writeStartElement(String prefix, String localName,
093: String namespaceURI) throws XMLStreamException {
094: String pf = prefix;
095: if (prefix == null) {
096: pf = "";
097: }
098: super .writeStartElement(pf, localName, namespaceURI);
099: _elementPrefix = pf;
100: exC14NContext.push();
101:
102: }
103:
104: protected void closeStartTag() throws XMLStreamException {
105: try {
106: if (closeStartTag) {
107: if (_attrResult.size() > 0) {
108: collectVisiblePrefixes(_attrResult.iterator());
109: }
110: if (_elementPrefix != null)
111: visiblyUtilized.add(_elementPrefix);
112: AttributeNS nsDecl = null;
113: if (_elementPrefix != null
114: && _elementPrefix.length() > 0) {
115: AttributeNS eDecl = exC14NContext
116: .getNamespaceDeclaration(_elementPrefix);
117:
118: if (eDecl != null && !eDecl.isWritten()) {
119: eDecl.setWritten(true);
120: _nsResult.add(eDecl);
121: }
122:
123: }
124: if (visiblyUtilized.size() > 0) {
125: Iterator prefixItr = visiblyUtilized.iterator();
126: populateNamespaceDecl(prefixItr);
127: }
128: if (inclusivePrefixList != null) {
129: populateNamespaceDecl(inclusivePrefixList
130: .iterator());
131: }
132:
133: if (_nsResult.size() > 0) {
134: BaseCanonicalizer.sort(_nsResult);
135: writeAttributesNS(_nsResult);
136: }
137:
138: if (_attrResult.size() > 0) {
139: BaseCanonicalizer.sort(_attrResult);
140: writeAttributes(_attrResult);
141: }
142: visiblyUtilized.clear();
143: _nsResult.clear();
144: _attrResult.clear();
145: _stream.write('>');
146: closeStartTag = false;
147: _elementPrefix = null;
148: _defURI = null;
149: }
150: } catch (IOException ex) {
151: throw new RuntimeException(ex);
152: }
153: }
154:
155: public void writeEmptyElement(String namespaceURI, String localName)
156: throws XMLStreamException {
157: /* String prefix = nsContext.getPrefix (namespaceURI);
158: writeEmptyElement (prefix,localName,namespaceURI);*/
159: //TODO
160: throw new UnsupportedOperationException();
161: }
162:
163: public void writeEmptyElement(String prefix, String localName,
164: String namespaceURI) throws XMLStreamException {
165:
166: closeStartTag();
167: exC14NContext.push();
168:
169: try {
170: _stream.write('<');
171: elemBuffer.reset();
172: if (prefix.length() == 0) {
173: writeStringToUtf8(localName, elemBuffer);
174: } else {
175: writeStringToUtf8(prefix, elemBuffer);
176: writeStringToUtf8(":", elemBuffer);
177: writeStringToUtf8(localName, elemBuffer);
178:
179: }
180: byte[] endElem = elemBuffer.getBytes();
181: int len = elemBuffer.getLength();
182: visiblyUtilized.add(prefix);
183: AttributeNS nsDecl = null;
184: _stream.write(endElem, 0, len);
185:
186: if (visiblyUtilized.size() > 0) {
187: Iterator prefixItr = visiblyUtilized.iterator();
188: populateNamespaceDecl(prefixItr);
189: }
190: if (inclusivePrefixList != null) {
191: populateNamespaceDecl(inclusivePrefixList.iterator());
192: }
193:
194: if (_nsResult.size() > 0) {
195: BaseCanonicalizer.sort(_nsResult);
196: writeAttributesNS(_nsResult);
197: }
198:
199: if (_attrResult.size() > 0) {
200: BaseCanonicalizer.sort(_attrResult);
201: writeAttributes(_attrResult);
202: }
203:
204: visiblyUtilized.clear();
205: _nsResult.clear();
206: _attrResult.clear();
207: // _stream .write('>');
208: closeStartTag = false;
209: _elementPrefix = "";
210: _defURI = null;
211:
212: _stream.write('>');
213: _stream.write(_END_TAG);
214: //writeStringToUtf8(name,_stream);
215: _stream.write(endElem, 0, len);
216: _stream.write('>');
217: exC14NContext.pop();
218: } catch (IOException ex) {
219: throw new RuntimeException(ex);
220: }
221: }
222:
223: public void writeEndDocument() throws XMLStreamException {
224: while (_depth > 0) {
225: writeEndElement();
226: }
227: }
228:
229: public void writeEndElement() throws XMLStreamException {
230: closeStartTag();
231: if (_depth == 0) {
232: return;
233: }
234: ElementName qname = elementNames[--_depth];
235:
236: exC14NContext.pop();
237:
238: try {
239: _stream.write(_END_TAG);
240: //writeStringToUtf8(qname,_stream);
241: _stream.write(qname.getUtf8Data().getBytes(), 0, qname
242: .getUtf8Data().getLength());
243: qname.getUtf8Data().reset();
244: _stream.write('>');
245: } catch (IOException ex) {
246: throw new RuntimeException(ex);
247: }
248: }
249:
250: protected void collectVisiblePrefixes(Iterator itr)
251: throws IOException {
252: while (itr.hasNext()) {
253: StAXAttr attr = (StAXAttr) itr.next();
254: String prefix = attr.getPrefix();
255: if (prefix.length() > 0) {
256: visiblyUtilized.add(prefix);
257: }
258: }
259: }
260:
261: private void populateNamespaceDecl(Iterator prefixItr) {
262: AttributeNS nsDecl = null;
263: while (prefixItr.hasNext()) {
264: String prefix = (String) prefixItr.next();
265: nsDecl = exC14NContext.getNamespaceDeclaration(prefix);
266:
267: if (nsDecl != null && !nsDecl.isWritten()) {
268: nsDecl.setWritten(true);
269: _nsResult.add(nsDecl);
270: }
271: }
272: }
273:
274: protected void writeAttributesNS(List itr) throws IOException {
275:
276: AttributeNS attr = null;
277: int size = itr.size();
278: for (int i = 0; i < size; i++) {
279: attr = (AttributeNS) itr.get(i);
280: tmpBuffer.reset();
281: _stream.write(attr.getUTF8Data(tmpBuffer));
282: }
283:
284: }
285:
286: public NamespaceContext getNamespaceContext() {
287: return exC14NContext;
288: }
289: }
|