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 */ 017package org.apache.camel.view; 018 019import java.io.File; 020import java.io.FileNotFoundException; 021import java.io.FileOutputStream; 022import java.io.IOException; 023import java.io.OutputStream; 024import java.io.OutputStreamWriter; 025import java.util.List; 026import java.util.Properties; 027 028import javax.xml.bind.Binder; 029import javax.xml.bind.JAXBContext; 030import javax.xml.bind.JAXBException; 031import javax.xml.bind.annotation.XmlRootElement; 032import javax.xml.parsers.ParserConfigurationException; 033import javax.xml.transform.OutputKeys; 034import javax.xml.transform.Result; 035import javax.xml.transform.TransformerException; 036import javax.xml.transform.TransformerFactory; 037import javax.xml.transform.stream.StreamResult; 038 039import org.w3c.dom.Document; 040import org.w3c.dom.Element; 041import org.w3c.dom.Node; 042 043import org.apache.camel.RuntimeCamelException; 044import org.apache.camel.RuntimeTransformException; 045import org.apache.camel.builder.xml.Namespaces; 046import org.apache.camel.converter.jaxp.XmlConverter; 047import org.apache.camel.model.RouteDefinition; 048import org.apache.camel.model.RoutesDefinition; 049import org.apache.camel.util.ObjectHelper; 050 051@Deprecated 052public class ModelFileGenerator { 053 054 private static final String DEFAULT_ROOT_ELEMENT_NAME = "routes"; 055 private final JAXBContext jaxbContext; 056 private Binder<Node> binder; 057 058 public ModelFileGenerator(JAXBContext jaxbContext) { 059 this.jaxbContext = jaxbContext; 060 } 061 062 /** 063 * Write the specified 'routeTypes' to 'fileName' as XML using JAXB. 064 */ 065 public void marshalRoutesUsingJaxb(String fileName, List<RouteDefinition> routeTypes) throws IOException { 066 OutputStream outputStream = outputStream(fileName); 067 068 try { 069 XmlConverter converter = converter(); 070 Document doc = converter.createDocument(); 071 072 Element root = doc.createElement(rootElementName()); 073 root.setAttribute("xmlns", Namespaces.DEFAULT_NAMESPACE); 074 doc.appendChild(root); 075 076 for (RouteDefinition routeType : routeTypes) { 077 addJaxbElementToNode(root, routeType); 078 } 079 080 Result result = new StreamResult(new OutputStreamWriter(outputStream, XmlConverter.defaultCharset)); 081 082 copyToResult(converter, doc, result); 083 } catch (ParserConfigurationException e) { 084 throw new RuntimeTransformException(e); 085 } catch (TransformerException e) { 086 throw new RuntimeTransformException(e); 087 } finally { 088 outputStream.close(); 089 } 090 } 091 092 /** 093 * Returns a configured XmlConverter 094 */ 095 private XmlConverter converter() { 096 XmlConverter converter = new XmlConverter(); 097 TransformerFactory transformerFactory = converter.getTransformerFactory(); 098 transformerFactory.setAttribute("indent-number", 2); 099 return converter; 100 } 101 102 /** 103 * Copies the given input Document into the required result using the provided converter. 104 */ 105 private void copyToResult(XmlConverter converter, Document doc, Result result) throws TransformerException { 106 Properties outputProperties = converter.defaultOutputProperties(); 107 outputProperties.put(OutputKeys.OMIT_XML_DECLARATION, "no"); 108 outputProperties.put(OutputKeys.INDENT, "yes"); 109 110 converter.toResult(converter.toDOMSource(doc), result, outputProperties); 111 } 112 113 /** 114 * Convert the specified object into XML and add it as a child of 'node' using JAXB. 115 */ 116 private void addJaxbElementToNode(Node node, Object jaxbElement) { 117 try { 118 if (binder == null) { 119 binder = jaxbContext.createBinder(); 120 } 121 binder.marshal(jaxbElement, node); 122 } catch (JAXBException e) { 123 throw new RuntimeCamelException(e); 124 } 125 } 126 127 /** 128 * Return the root element name for the list of routes. 129 */ 130 private String rootElementName() { 131 XmlRootElement annotation = (RoutesDefinition.class).getAnnotation(XmlRootElement.class); 132 if (annotation != null) { 133 String elementName = annotation.name(); 134 if (ObjectHelper.isNotEmpty(elementName)) { 135 return elementName; 136 } 137 } 138 return DEFAULT_ROOT_ELEMENT_NAME; 139 } 140 141 /** 142 * returns an output stream for the filename specified. 143 */ 144 private OutputStream outputStream(String fileName) throws FileNotFoundException { 145 File file = new File(fileName); 146 if (!file.exists()) { 147 File parentFile = file.getParentFile(); 148 if (parentFile != null) { 149 parentFile.mkdirs(); 150 } 151 } 152 return new FileOutputStream(file); 153 } 154}