001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.transport.http_jetty.spring;
019:
020: import java.util.ArrayList;
021: import java.util.List;
022:
023: import javax.xml.bind.JAXBContext;
024: import javax.xml.bind.JAXBElement;
025: import javax.xml.bind.JAXBException;
026: import javax.xml.bind.Unmarshaller;
027: import javax.xml.namespace.QName;
028:
029: import org.w3c.dom.Element;
030: import org.w3c.dom.Node;
031: import org.w3c.dom.NodeList;
032: import org.springframework.beans.factory.support.BeanDefinitionBuilder;
033:
034: public final class JAXBHelper {
035: private JAXBHelper() {
036:
037: }
038:
039: @SuppressWarnings("unchecked")
040: public static <V> List<V> parseListElement(Element parent,
041: BeanDefinitionBuilder bean, QName name, Class<?> c)
042: throws JAXBException {
043: List<V> list = new ArrayList<V>();
044: NodeList nl = parent.getChildNodes();
045: Node data = null;
046:
047: JAXBContext context = null;
048: String pkg = "";
049: if (null != c) {
050: pkg = c.getPackage().getName();
051: context = JAXBContext.newInstance(pkg, c.getClassLoader());
052: } else {
053: context = JAXBContext.newInstance(pkg);
054: }
055:
056: for (int i = 0; i < nl.getLength(); i++) {
057: Node n = nl.item(i);
058: if (n.getNodeType() == Node.ELEMENT_NODE
059: && name.getLocalPart().equals(n.getLocalName())
060: && name.getNamespaceURI().equals(
061: n.getNamespaceURI())) {
062: data = n;
063: Object obj = unmarshal(context, data, c);
064: if (obj != null) {
065: list.add((V) obj);
066: }
067: }
068: }
069: return list;
070: }
071:
072: public static <T> T parseElement(Element element,
073: BeanDefinitionBuilder bean, Class<T> c)
074: throws JAXBException {
075: if (null == element) {
076: return null;
077: }
078: JAXBContext context = null;
079: String pkg = "";
080: if (null != c) {
081: pkg = c.getPackage().getName();
082: context = JAXBContext.newInstance(pkg, c.getClassLoader());
083: } else {
084: context = JAXBContext.newInstance(pkg);
085: }
086: Object obj = unmarshal(context, element, c);
087:
088: return c.cast(obj);
089: }
090:
091: private static Object unmarshal(JAXBContext context, Node data,
092: Class<?> c) {
093: if (context == null) {
094: return null;
095: }
096:
097: Object obj = null;
098:
099: try {
100:
101: Unmarshaller u = context.createUnmarshaller();
102: if (c != null) {
103: obj = u.unmarshal(data, c);
104: } else {
105: obj = u.unmarshal(data);
106: }
107:
108: if (obj instanceof JAXBElement<?>) {
109: JAXBElement<?> el = (JAXBElement<?>) obj;
110: obj = el.getValue();
111: }
112:
113: } catch (JAXBException e) {
114: throw new RuntimeException(
115: "Could not parse configuration.", e);
116: }
117:
118: return obj;
119:
120: }
121:
122: }
|