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.bus.extension;
19:
20: import java.io.IOException;
21: import java.io.InputStream;
22: import java.util.ArrayList;
23: import java.util.List;
24:
25: import javax.xml.parsers.DocumentBuilder;
26: import javax.xml.parsers.DocumentBuilderFactory;
27: import javax.xml.parsers.ParserConfigurationException;
28:
29: import org.w3c.dom.Document;
30: import org.w3c.dom.Element;
31: import org.w3c.dom.Node;
32:
33: import org.xml.sax.SAXException;
34:
35: public class ExtensionFragmentParser {
36:
37: private static final String EXTENSION_ELEM_NAME = "extension";
38: private static final String NAMESPACE_ELEM_NAME = "namespace";
39: private static final String CLASS_ATTR_NAME = "class";
40: private static final String INTERFACE_ATTR_NAME = "interface";
41: private static final String DEFERRED_ATTR_NAME = "deferred";
42:
43: List<Extension> getExtensions(InputStream is) {
44: Document document = null;
45: try {
46: DocumentBuilderFactory factory = DocumentBuilderFactory
47: .newInstance();
48: factory.setNamespaceAware(true);
49: DocumentBuilder parser = factory.newDocumentBuilder();
50: document = parser.parse(is);
51: } catch (ParserConfigurationException ex) {
52: throw new ExtensionException(ex);
53: } catch (SAXException ex) {
54: throw new ExtensionException(ex);
55: } catch (IOException ex) {
56: throw new ExtensionException(ex);
57: }
58:
59: return deserialiseExtensions(document);
60: }
61:
62: List<Extension> deserialiseExtensions(Document document) {
63: List<Extension> extensions = new ArrayList<Extension>();
64:
65: Element root = document.getDocumentElement();
66: for (Node nd = root.getFirstChild(); nd != null; nd = nd
67: .getNextSibling()) {
68: if (Node.ELEMENT_NODE == nd.getNodeType()
69: && EXTENSION_ELEM_NAME.equals(nd.getLocalName())) {
70: Extension e = new Extension();
71: Element elem = (Element) nd;
72: e.setClassname(elem.getAttribute(CLASS_ATTR_NAME));
73: e.setInterfaceName(elem
74: .getAttribute(INTERFACE_ATTR_NAME));
75: String bval = elem.getAttribute(DEFERRED_ATTR_NAME)
76: .trim();
77: e.setDeferred("1".equals(bval) || "true".equals(bval));
78:
79: deserialiseNamespaces(elem, e);
80:
81: extensions.add(e);
82: }
83: }
84: return extensions;
85: }
86:
87: void deserialiseNamespaces(Element extensionElem, Extension e) {
88: for (Node nd = extensionElem.getFirstChild(); nd != null; nd = nd
89: .getNextSibling()) {
90: if (Node.ELEMENT_NODE == nd.getNodeType()
91: && NAMESPACE_ELEM_NAME.equals(nd.getLocalName())) {
92: e.getNamespaces().add(nd.getTextContent());
93: }
94: }
95: }
96: }
|