01: /**
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing,
13: * software distributed under the License is distributed on an
14: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15: * KIND, either express or implied. See the License for the
16: * specific language governing permissions and limitations
17: * under the License.
18: */package org.apache.cxf.tools.util;
19:
20: import java.net.URL;
21: import java.util.logging.Logger;
22:
23: import javax.xml.stream.XMLInputFactory;
24: import javax.xml.stream.XMLStreamException;
25: import javax.xml.stream.XMLStreamReader;
26:
27: import org.xml.sax.InputSource;
28:
29: import org.apache.cxf.common.i18n.Message;
30: import org.apache.cxf.common.logging.LogUtils;
31: import org.apache.cxf.tools.common.ToolException;
32:
33: public final class StAXUtil {
34: private static final Logger LOG = LogUtils
35: .getL7dLogger(StAXUtil.class);
36: private static final XMLInputFactory XML_INPUT_FACTORY;
37: static {
38: XML_INPUT_FACTORY = XMLInputFactory.newInstance();
39: XML_INPUT_FACTORY.setProperty(
40: XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.TRUE);
41: }
42:
43: private StAXUtil() {
44: }
45:
46: public static void toStartTag(XMLStreamReader r)
47: throws XMLStreamException {
48: while (!r.isStartElement() && r.hasNext()) {
49: r.next();
50: }
51: }
52:
53: public static XMLStreamReader createFreshXMLStreamReader(
54: InputSource source) {
55: try {
56: if (source.getCharacterStream() != null) {
57: return XML_INPUT_FACTORY.createXMLStreamReader(source
58: .getSystemId(), source.getCharacterStream());
59: }
60: if (source.getByteStream() != null) {
61: return XML_INPUT_FACTORY.createXMLStreamReader(source
62: .getSystemId(), source.getByteStream());
63: }
64: return XML_INPUT_FACTORY.createXMLStreamReader(source
65: .getSystemId(), new URL(source.getSystemId())
66: .openStream());
67: } catch (Exception e) {
68: Message msg = new Message("FAIL_TO_CREATE_STAX", LOG);
69: throw new ToolException(msg, e);
70: }
71: }
72: }
|