001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.camel.model;
018
019 import java.io.StringReader;
020 import java.io.StringWriter;
021 import javax.xml.bind.JAXBContext;
022 import javax.xml.bind.JAXBException;
023 import javax.xml.bind.Marshaller;
024 import javax.xml.bind.Unmarshaller;
025
026 import org.apache.camel.CamelContext;
027 import org.apache.camel.NamedNode;
028
029 /**
030 * Helper for the Camel {@link org.apache.camel.model model} classes.
031 */
032 public final class ModelHelper {
033
034 private ModelHelper() {
035 // utility class
036 }
037
038 /**
039 * Dumps the definition as XML
040 *
041 * @param definition the definition, such as a {@link org.apache.camel.NamedNode}
042 * @return the output in XML (is formatted)
043 * @throws JAXBException is throw if error marshalling to XML
044 */
045 public static String dumpModelAsXml(NamedNode definition) throws JAXBException {
046 // create a new jaxb context
047 // must use classloader from CamelContext to have JAXB working
048 JAXBContext jaxbContext = JAXBContext.newInstance(Constants.JAXB_CONTEXT_PACKAGES, CamelContext.class.getClassLoader());
049
050 Marshaller marshaller = jaxbContext.createMarshaller();
051 marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
052 StringWriter buffer = new StringWriter();
053 marshaller.marshal(definition, buffer);
054
055 return buffer.toString();
056 }
057
058 /**
059 * Marshal the xml to the model definition
060 *
061 * @param xml the xml
062 * @param type the definition type to return, will throw a {@link ClassCastException} if not the expected type
063 * @return the model definition
064 * @throws javax.xml.bind.JAXBException is thrown if error unmarshalling from xml to model
065 */
066 public static <T extends NamedNode> T createModelFromXml(String xml, Class<T> type) throws JAXBException {
067 // create a new jaxb context
068 // must use classloader from CamelContext to have JAXB working
069 JAXBContext jaxbContext = JAXBContext.newInstance(Constants.JAXB_CONTEXT_PACKAGES, CamelContext.class.getClassLoader());
070
071 StringReader reader = new StringReader(xml);
072 Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
073 Object result = unmarshaller.unmarshal(reader);
074 reader.close();
075
076 if (result == null) {
077 throw new JAXBException("Cannot unmarshal to " + type + " using JAXB from XML: " + xml);
078 }
079
080 return type.cast(result);
081 }
082 }