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