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: package com.sun.xml.ws.security.opt.impl.util;
024:
025: import com.sun.xml.wss.impl.c14n.BaseCanonicalizer;
026: import javax.xml.stream.XMLStreamWriter;
027: import javax.xml.stream.XMLStreamException;
028:
029: import com.sun.xml.wss.impl.MessageConstants;
030:
031: /**
032: *
033: * @author Ashutosh.Shahi@sun.com
034: */
035: public class XMLStreamFilterWithId extends XMLStreamFilter {
036:
037: String id = null;
038: boolean wroteId = false;
039:
040: /** Creates a new instance of XMLStreamWriterWithId */
041: public XMLStreamFilterWithId(XMLStreamWriter writer,
042: NamespaceContextEx nce, String id)
043: throws XMLStreamException {
044: super (writer, nce);
045: this .id = id;
046: }
047:
048: public void setDefaultNamespace(String string)
049: throws XMLStreamException {
050: writer.setDefaultNamespace(string);
051: }
052:
053: public void writeEndElement() throws XMLStreamException {
054: if (!wroteId && count == 1) {
055: writer.writeAttribute(MessageConstants.WSU_PREFIX,
056: MessageConstants.WSU_NS, "Id", id);
057: if (writer instanceof BaseCanonicalizer) {
058: writer.setNamespaceContext(nsContext);
059: }
060: wroteId = true;
061: }
062: writer.writeEndElement();
063: }
064:
065: public void writeStartElement(String string)
066: throws XMLStreamException {
067: if (!wroteId && count == 1) {
068: writer.writeAttribute(MessageConstants.WSU_PREFIX,
069: MessageConstants.WSU_NS, "Id", id);
070: if (writer instanceof BaseCanonicalizer) {
071: writer.setNamespaceContext(nsContext);
072: }
073: wroteId = true;
074: }
075: if (!seenFirstElement) {
076: seenFirstElement = true;
077: }
078: writer.writeStartElement(string);
079: /*if(count == 0){
080: writer.writeAttribute(MessageConstants.WSU_PREFIX, MessageConstants.WSU_NS,
081: "Id", id);
082: if(writer instanceof BaseCanonicalizer){
083: writer.setNamespaceContext(nsContext);
084: }
085: }*/
086: count++;
087: }
088:
089: public void writeAttribute(String string, String string0)
090: throws XMLStreamException {
091: writer.writeAttribute(string, string0);
092: }
093:
094: public void writeNamespace(String string, String string0)
095: throws XMLStreamException {
096: writer.writeNamespace(string, string0);
097: }
098:
099: public void writeStartElement(String string, String string0)
100: throws XMLStreamException {
101: if (!wroteId && count == 1) {
102: writer.writeAttribute(MessageConstants.WSU_PREFIX,
103: MessageConstants.WSU_NS, "Id", id);
104: if (writer instanceof BaseCanonicalizer) {
105: writer.setNamespaceContext(nsContext);
106: }
107: wroteId = true;
108: }
109: if (!seenFirstElement) {
110: seenFirstElement = true;
111: }
112: writer.writeStartElement(string, string0);
113: /*if(count == 0){
114: writer.writeAttribute(MessageConstants.WSU_PREFIX, MessageConstants.WSU_NS,
115: "Id", id);
116: if(writer instanceof BaseCanonicalizer){
117: writer.setNamespaceContext(nsContext);
118: }
119: }*/
120: count++;
121:
122: }
123:
124: public void writeAttribute(String string, String string0,
125: String string1) throws XMLStreamException {
126: writer.writeAttribute(string, string0, string1);
127: }
128:
129: public void writeStartElement(String string, String string0,
130: String string1) throws XMLStreamException {
131: if (!wroteId && count == 1) {
132: writer.writeAttribute(MessageConstants.WSU_PREFIX,
133: MessageConstants.WSU_NS, "Id", id);
134: if (writer instanceof BaseCanonicalizer) {
135: writer.setNamespaceContext(nsContext);
136: }
137: wroteId = true;
138: }
139: if (!seenFirstElement) {
140: seenFirstElement = true;
141: }
142:
143: writer.writeStartElement(string, string0, string1);
144: /*if(count == 0){
145: writer.writeAttribute(MessageConstants.WSU_PREFIX, MessageConstants.WSU_NS,
146: "Id", id);
147: if(writer instanceof BaseCanonicalizer){
148: writer.setNamespaceContext(nsContext);
149: }
150: }*/
151: count++;
152:
153: }
154:
155: public void writeAttribute(String string, String string0,
156: String string1, String string2) throws XMLStreamException {
157: writer.writeAttribute(string, string0, string1, string2);
158: }
159:
160: public void writeCharacters(char[] c, int index, int len)
161: throws XMLStreamException {
162: if (!wroteId && count == 1) {
163: writer.writeAttribute(MessageConstants.WSU_PREFIX,
164: MessageConstants.WSU_NS, "Id", id);
165: if (writer instanceof BaseCanonicalizer) {
166: writer.setNamespaceContext(nsContext);
167: }
168: wroteId = true;
169: }
170: writer.writeCharacters(c, index, len);
171: }
172:
173: public void writeCharacters(String string)
174: throws XMLStreamException {
175: if (!wroteId && count == 1) {
176: writer.writeAttribute(MessageConstants.WSU_PREFIX,
177: MessageConstants.WSU_NS, "Id", id);
178: if (writer instanceof BaseCanonicalizer) {
179: writer.setNamespaceContext(nsContext);
180: }
181: wroteId = true;
182: }
183: writer.writeCharacters(string);
184: }
185:
186: public void writeEmptyElement(String string)
187: throws XMLStreamException {
188: if (count == 0) {
189: writer.setNamespaceContext(nsContext);
190: }
191: if (!wroteId && count == 1) {
192: writer.writeAttribute(MessageConstants.WSU_PREFIX,
193: MessageConstants.WSU_NS, "Id", id);
194: if (writer instanceof BaseCanonicalizer) {
195: writer.setNamespaceContext(nsContext);
196: }
197: wroteId = true;
198: }
199: writer.writeEmptyElement(string);
200:
201: }
202:
203: public void writeEmptyElement(String string, String string0,
204: String string1) throws XMLStreamException {
205: if (count == 0) {
206: writer.setNamespaceContext(nsContext);
207: }
208: if (!wroteId && count == 1) {
209: writer.writeAttribute(MessageConstants.WSU_PREFIX,
210: MessageConstants.WSU_NS, "Id", id);
211: if (writer instanceof BaseCanonicalizer) {
212: writer.setNamespaceContext(nsContext);
213: }
214: wroteId = true;
215: }
216: writer.writeEmptyElement(string, string0, string1);
217: }
218:
219: public void writeEmptyElement(String string, String string0)
220: throws XMLStreamException {
221: if (count == 0) {
222: writer.setNamespaceContext(nsContext);
223: }
224: if (!wroteId && count == 1) {
225: writer.writeAttribute(MessageConstants.WSU_PREFIX,
226: MessageConstants.WSU_NS, "Id", id);
227: if (writer instanceof BaseCanonicalizer) {
228: writer.setNamespaceContext(nsContext);
229: }
230: wroteId = true;
231: }
232: writer.writeEmptyElement(string, string0);
233: }
234:
235: public void writeProcessingInstruction(String string, String string0)
236: throws XMLStreamException {
237: if (!wroteId && count == 1) {
238: writer.writeAttribute(MessageConstants.WSU_PREFIX,
239: MessageConstants.WSU_NS, "Id", id);
240: if (writer instanceof BaseCanonicalizer) {
241: writer.setNamespaceContext(nsContext);
242: }
243: wroteId = true;
244: }
245: writer.writeProcessingInstruction(string, string0);
246: }
247:
248: public void writeProcessingInstruction(String string)
249: throws XMLStreamException {
250: if (!wroteId && count == 1) {
251: writer.writeAttribute(MessageConstants.WSU_PREFIX,
252: MessageConstants.WSU_NS, "Id", id);
253: if (writer instanceof BaseCanonicalizer) {
254: writer.setNamespaceContext(nsContext);
255: }
256: wroteId = true;
257: }
258: writer.writeProcessingInstruction(string);
259: }
260:
261: public void writeCData(String string) throws XMLStreamException {
262: if (!wroteId && count == 1) {
263: writer.writeAttribute(MessageConstants.WSU_PREFIX,
264: MessageConstants.WSU_NS, "Id", id);
265: if (writer instanceof BaseCanonicalizer) {
266: writer.setNamespaceContext(nsContext);
267: }
268: wroteId = true;
269: }
270: writer.writeCData(string);
271: }
272: }
|