View Javadoc
1 /* 2 * Copyright (c) 2003 Scott Howlett & Paul Libbrecht. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in 14 * the documentation and/or other materials provided with the 15 * distribution. 16 * 17 * 3. The end-user documentation included with the redistribution, 18 * if any, must include the following acknowledgment: 19 * "This product includes software developed by the Xwing 20 * Project ( http://xwing.sourceforge.net/ )." 21 * Alternately, this acknowledgment may appear in the software itself, 22 * if and wherever such third-party acknowledgments normally appear. 23 * 24 * 4. The name "Xwing" must not be used to endorse or promote products 25 * derived from this software without prior written permission. For 26 * written permission, please contact the project authors via 27 * the Xwing project site, http://xwing.sourceforge.net/ . 28 * 29 * 5. Products derived from this software may not be called "Xwing", 30 * nor may "Xwing" appear in their name, without prior written 31 * permission of the Xwing project. 32 * 33 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 34 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 35 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 36 * DISCLAIMED. IN NO EVENT SHALL THE XWING AUTHORS OR THE PROJECT 37 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 38 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 39 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 40 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 41 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 42 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 43 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 44 * SUCH DAMAGE. 45 * 46 * For more information on Xwing, please see 47 * < http://xwing.sourceforge.net/ >. 48 */ 49 package net.sourceforge.xwing.jelly.tags.dom; 50 51 import java.io.File; 52 import java.io.OutputStream; 53 import java.io.StringWriter; 54 import java.io.Writer; 55 import java.net.URL; 56 57 import javax.xml.transform.Result; 58 import javax.xml.transform.Source; 59 import javax.xml.transform.Transformer; 60 import javax.xml.transform.TransformerFactory; 61 import javax.xml.transform.dom.DOMSource; 62 import javax.xml.transform.sax.SAXResult; 63 import javax.xml.transform.stream.StreamResult; 64 65 import org.apache.commons.beanutils.BasicDynaBean; 66 import org.apache.commons.beanutils.BasicDynaClass; 67 import org.apache.commons.beanutils.DynaClass; 68 import org.apache.commons.beanutils.DynaProperty; 69 import org.apache.commons.jelly.DynaBeanTagSupport; 70 import org.apache.commons.jelly.JellyTagException; 71 import org.apache.commons.jelly.MissingAttributeException; 72 import org.apache.commons.jelly.XMLOutput; 73 import org.w3c.dom.Node; 74 75 // DynaBeanTagSupport is the only working way to get 76 // the "xml" attribute without going through unnecessary 77 // and destructive BeanUtils conversions. 78 79 public class OutTag extends DynaBeanTagSupport { 80 81 private static DynaClass attribClass = 82 new BasicDynaClass( 83 "Attributes", 84 null, 85 new DynaProperty[] { 86 new DynaProperty("xml", Object.class), 87 new DynaProperty("var", String.class), 88 new DynaProperty("node", Node.class)}); 89 90 public OutTag() { 91 super(new BasicDynaBean(attribClass)); 92 } 93 94 public void doTag(XMLOutput output) 95 throws MissingAttributeException, JellyTagException { 96 97 Object xml = getDynaBean().get("xml"); 98 String var = (String) getDynaBean().get("var"); 99 Node node = (Node) getDynaBean().get("node"); 100 101 if (node == null) { 102 throw new JellyTagException("node"); 103 } 104 105 try { 106 107 Transformer xform = 108 TransformerFactory.newInstance().newTransformer(); 109 110 Source src = new DOMSource(node); 111 Result dest = null; 112 113 StringWriter sw = null; 114 115 if (xml == null && var == null) { 116 dest = new SAXResult(output); 117 } else { 118 if (xml == null) { 119 sw = new StringWriter(); 120 xml = sw; 121 } 122 if (xml instanceof String) { 123 // xml = context.getResource((String) xml); 124 xml = new File(xml.toString()); 125 } 126 127 if (xml instanceof URL) { 128 129 URL u = (URL) xml; 130 xml = new File(((URL) xml).getFile()); 131 } 132 133 if (xml instanceof File) { 134 File xmlFile = (File) xml; 135 xmlFile.createNewFile(); 136 dest = new StreamResult((File) xml); 137 } else if (xml instanceof Writer) { 138 dest = new StreamResult((Writer) xml); 139 } else if (xml instanceof OutputStream) { 140 dest = new StreamResult((OutputStream) xml); 141 } else { 142 throw new IllegalArgumentException( 143 "Invalid xml argument. Must be a File," 144 + " Writer, OutputStream, or String." 145 + " Was type; " 146 + xml.getClass().getName() 147 + " with value: " 148 + xml); 149 } 150 151 } 152 xform.transform(src, dest); 153 154 if (var != null && sw != null) { 155 context.setVariable(var, sw.toString()); 156 } 157 158 } catch (Exception exc) { 159 throw new JellyTagException(exc); 160 } 161 162 } 163 }

This page was automatically generated by Maven