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.MessageConstants;
026: import javax.xml.stream.XMLStreamException;
027: import javax.xml.stream.XMLStreamReader;
028: import javax.xml.stream.XMLStreamWriter;
029: import org.jvnet.staxex.XMLStreamReaderEx;
030: import org.jvnet.staxex.XMLStreamWriterEx;
031:
032: /**
033: *
034: * @author K.Venugopal@sun.com
035: */
036: public class StreamUtil {
037:
038: /** Creates a new instance of StreamUtil */
039: public StreamUtil() {
040: }
041:
042: public static boolean moveToNextElement(XMLStreamReader reader)
043: throws XMLStreamException {
044: if (reader.hasNext()) {
045: reader.next();
046: while (reader.getEventType() != XMLStreamReader.START_ELEMENT) {
047: if (reader.hasNext()) {
048: reader.next();
049: } else {
050: return false;
051: }
052: }
053: return true;
054: } else {
055: return false;
056: }
057: }
058:
059: public static boolean moveToNextStartOREndElement(
060: XMLStreamReader reader) throws XMLStreamException {
061: if (reader.hasNext()) {
062: reader.next();
063: while (move(reader)) {
064: if (reader.hasNext()) {
065: reader.next();
066: } else {
067: return false;
068: }
069: }
070: return true;
071: } else {
072: return false;
073: }
074: }
075:
076: public static boolean moveToNextStartOREndElement(
077: XMLStreamReader reader, XMLStreamWriter writer)
078: throws XMLStreamException {
079: if (reader.hasNext()) {
080: reader.next();
081: writeCurrentEvent(reader, writer);
082: while (move(reader)) {
083: if (reader.hasNext()) {
084: reader.next();
085: writeCurrentEvent(reader, writer);
086: } else {
087: return false;
088: }
089: }
090: return true;
091: } else {
092: return false;
093: }
094: }
095:
096: public static boolean isStartElement(XMLStreamReader reader) {
097: if (reader.getEventType() == XMLStreamReader.START_ELEMENT) {
098: return true;
099: }
100: return false;
101: }
102:
103: public static boolean _break(XMLStreamReader reader,
104: String localName, String uri) throws XMLStreamException {
105: if (reader.getEventType() == XMLStreamReader.END_ELEMENT) {
106: if (reader.getLocalName() == localName
107: && reader.getNamespaceURI() == uri) {
108: return true;
109: }
110: }
111: return false;
112: }
113:
114: private static boolean move(XMLStreamReader reader) {
115: if (reader.getEventType() == XMLStreamReader.START_ELEMENT
116: || reader.getEventType() == XMLStreamReader.END_ELEMENT) {
117: return false;
118: }
119: return true;
120: }
121:
122: public static void writeStartElement(XMLStreamReader reader,
123: XMLStreamWriter writer) throws XMLStreamException {
124: writer.writeStartElement(reader.getPrefix(), reader
125: .getLocalName(), reader.getNamespaceURI());
126: int nsCount = reader.getNamespaceCount();
127:
128: for (int i = 0; i < nsCount; i++) {
129: String prefix = reader.getNamespacePrefix(i);
130: if (prefix == null)
131: prefix = "";
132: writer.writeNamespace(prefix, reader.getNamespaceURI(i));
133: }
134: int atCount = reader.getAttributeCount();
135: for (int i = 0; i < atCount; i++) {
136: if (reader.getAttributePrefix(i) == ""
137: || reader.getAttributePrefix(i) == null) {
138: writer.writeAttribute(reader.getAttributeLocalName(i),
139: reader.getAttributeValue(i));
140: } else {
141: writer.writeAttribute(reader.getAttributePrefix(i),
142: reader.getAttributeNamespace(i), reader
143: .getAttributeLocalName(i), reader
144: .getAttributeValue(i));
145: }
146: }
147:
148: }
149:
150: public static void writeCurrentEvent(XMLStreamReader reader,
151: XMLStreamWriter writer) throws XMLStreamException {
152: int event = reader.getEventType();
153: switch (event) {
154:
155: case XMLStreamReader.CDATA: {
156: writer.writeCData(reader.getText());
157: break;
158: }
159: case XMLStreamReader.CHARACTERS: {
160: writer.writeCharacters(reader.getTextCharacters(), reader
161: .getTextStart(), reader.getTextLength());
162: break;
163: }
164: case XMLStreamReader.COMMENT: {
165: writer.writeComment(reader.getText());
166: break;
167: }
168: case XMLStreamReader.DTD: {
169: break;
170: }
171: case XMLStreamReader.END_DOCUMENT: {
172: break;
173: }
174: case XMLStreamReader.END_ELEMENT: {
175: writer.writeEndElement();
176: break;
177: }
178: case XMLStreamReader.ENTITY_DECLARATION: {
179: break;
180: }
181: case XMLStreamReader.ENTITY_REFERENCE: {
182: break;
183: }
184: case XMLStreamReader.NAMESPACE: {
185: break;
186: }
187: case XMLStreamReader.NOTATION_DECLARATION: {
188: break;
189: }
190: case XMLStreamReader.PROCESSING_INSTRUCTION: {
191: break;
192: }
193: case XMLStreamReader.SPACE: {
194: writer.writeCharacters(reader.getText());
195: break;
196: }
197: case XMLStreamReader.START_DOCUMENT: {
198:
199: break;
200: }
201: case XMLStreamReader.START_ELEMENT: {
202: writeStartElement(reader, writer);
203: break;
204: }
205: }
206: }
207:
208: public static void writeCurrentEvent(XMLStreamReaderEx reader,
209: XMLStreamWriterEx writer) throws XMLStreamException {
210: int event = reader.getEventType();
211: switch (event) {
212:
213: case XMLStreamReader.CDATA: {
214: writer.writeCData(reader.getText());
215: break;
216: }
217: case XMLStreamReader.CHARACTERS: {
218: writer.writeCharacters(reader.getTextCharacters(), reader
219: .getTextStart(), reader.getTextLength());
220: break;
221: }
222: case XMLStreamReader.COMMENT: {
223: writer.writeComment(reader.getText());
224: break;
225: }
226: case XMLStreamReader.DTD: {
227: break;
228: }
229: case XMLStreamReader.END_DOCUMENT: {
230: break;
231: }
232: case XMLStreamReader.END_ELEMENT: {
233: writer.writeEndElement();
234: break;
235: }
236: case XMLStreamReader.ENTITY_DECLARATION: {
237: break;
238: }
239: case XMLStreamReader.ENTITY_REFERENCE: {
240: break;
241: }
242: case XMLStreamReader.NAMESPACE: {
243: break;
244: }
245: case XMLStreamReader.NOTATION_DECLARATION: {
246: break;
247: }
248: case XMLStreamReader.PROCESSING_INSTRUCTION: {
249: break;
250: }
251: case XMLStreamReader.SPACE: {
252: writer.writeCharacters(reader.getText());
253: break;
254: }
255: case XMLStreamReader.START_DOCUMENT: {
256:
257: break;
258: }
259: case XMLStreamReader.START_ELEMENT: {
260: writeStartElement(reader, writer);
261: break;
262: }
263: }
264: }
265:
266: public static String getWsuId(XMLStreamReader reader) {
267: return reader.getAttributeValue(MessageConstants.WSU_NS, "Id");
268: }
269:
270: public static String getId(XMLStreamReader reader) {
271: return reader.getAttributeValue(null, "Id");
272: }
273:
274: public static String getCV(XMLStreamReader reader)
275: throws XMLStreamException {
276: StringBuffer content = new StringBuffer();
277: int eventType = reader.getEventType();
278: while (eventType != XMLStreamReader.END_ELEMENT) {
279: if (eventType == XMLStreamReader.CHARACTERS
280: || eventType == XMLStreamReader.CDATA
281: || eventType == XMLStreamReader.SPACE
282: || eventType == XMLStreamReader.ENTITY_REFERENCE) {
283: content.append(reader.getText());
284: } else if (eventType == XMLStreamReader.PROCESSING_INSTRUCTION
285: || eventType == XMLStreamReader.COMMENT) {
286: // skipping
287: }
288: eventType = reader.next();
289: }
290: return content.toString();
291: }
292:
293: public static String getCV(XMLStreamReaderEx reader)
294: throws XMLStreamException {
295: StringBuffer sb = new StringBuffer();
296: while (reader.getEventType() == reader.CHARACTERS
297: && reader.getEventType() != reader.END_ELEMENT) {
298: CharSequence charSeq = ((XMLStreamReaderEx) reader)
299: .getPCDATA();
300: for (int i = 0; i < charSeq.length(); i++) {
301: sb.append(charSeq.charAt(i));
302: }
303: reader.next();
304: }
305: return sb.toString();
306: }
307:
308: public static String convertDigestAlgorithm(String algo) {
309: if (MessageConstants.SHA1_DIGEST.equals(algo)) {
310: return MessageConstants.SHA_1;
311: }
312: if (MessageConstants.SHA256.equals(algo)) {
313: return MessageConstants.SHA_256;
314: }
315:
316: if (MessageConstants.SHA512.equals(algo)) {
317: return MessageConstants.SHA_512;
318: }
319:
320: return MessageConstants.SHA_1;
321: }
322:
323: }
|