1 /*** 2 * Copyright 2004 The Apache Software Foundation. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package org.apache.commons.jelly.tags.jaxme; 17 18 import java.io.File; 19 import java.io.IOException; 20 import java.io.StringWriter; 21 22 import javax.xml.bind.JAXBContext; 23 import javax.xml.bind.JAXBException; 24 import javax.xml.bind.Marshaller; 25 26 import org.apache.commons.jelly.TagSupport; 27 import org.apache.commons.jelly.XMLOutput; 28 import org.apache.commons.jelly.MissingAttributeException; 29 import org.apache.commons.jelly.JellyTagException; 30 31 import org.xml.sax.SAXException; 32 33 /*** 34 * <p>Marshalls a generated object generated by a JAXB implementation into xml.</p> 35 * <p> 36 * The result is placed into the processed content for further processing by enclosing 37 * tags.</p> 38 * 39 * @author <a href="mailto:joe@ispsoft.de">Jochen Wiedmann</a> 40 * @author <a href="mailto:commons-dev at jakarta.apache.org">Apache Commons Development Team</a> 41 * @version $Revision: 155420 $ 42 */ 43 public class MarshallTag extends TagSupport { 44 45 private String packages; 46 private Object object; 47 48 public String getPackages() { 49 return packages; 50 } 51 52 /*** 53 * Defines the generated objects which will be marshalled by specifying the package name. 54 */ 55 public void setPackages(String packages) { 56 this.packages = packages; 57 } 58 59 public Object getObject() { 60 return object; 61 } 62 63 /*** 64 * Defines the object to be unmarshalled into xml by specifying a jelly variable name. 65 */ 66 public void setObject(Object object) { 67 this.object = object; 68 } 69 70 public void doTag(XMLOutput output) throws MissingAttributeException, JellyTagException { 71 if (packages == null) { 72 throw new MissingAttributeException( "packages" ); 73 } 74 if (object == null) { 75 throw new MissingAttributeException( "object" ); 76 } 77 try { 78 79 JAXBContext jaxbContext = JAXBContext.newInstance(packages); 80 Marshaller marshaller = jaxbContext.createMarshaller(); 81 marshaller.marshal(object, output); 82 83 } catch (JAXBException ex) { 84 throw new JellyTagException(ex); 85 } 86 } 87 }