1 16 package org.apache.commons.jelly.tags.core; 17 18 import java.io.StringReader ; 19 20 import javax.xml.parsers.ParserConfigurationException ; 21 import javax.xml.parsers.SAXParser ; 22 import javax.xml.parsers.SAXParserFactory ; 23 24 import org.apache.commons.jelly.JellyTagException; 25 import org.apache.commons.jelly.MissingAttributeException; 26 import org.apache.commons.jelly.Script; 27 import org.apache.commons.jelly.TagSupport; 28 import org.apache.commons.jelly.XMLOutput; 29 import org.apache.commons.jelly.parser.XMLParser; 30 import org.apache.commons.logging.Log; 31 import org.apache.commons.logging.LogFactory; 32 import org.xml.sax.ContentHandler ; 33 import org.xml.sax.InputSource ; 34 import org.xml.sax.SAXException ; 35 import org.xml.sax.XMLReader ; 36 37 44 public class ParseTag extends TagSupport { 45 46 47 private static final Log log = LogFactory.getLog(ParseTag.class); 48 49 50 private String var; 51 52 53 private String text; 54 55 56 private XMLReader xmlReader; 57 58 59 private XMLParser jellyParser; 60 61 public ParseTag() { 62 } 63 64 67 70 public void doTag(XMLOutput output) 71 throws MissingAttributeException, JellyTagException { 72 73 String text = getText(); 74 if (text != null) { 75 parseText(text); 76 } 77 else { 78 parseBody(output); 79 } 80 81 Script script = getJellyParser().getScript(); 82 if (var != null) { 83 context.setVariable(var, script); 84 } 85 else { 86 script.run(context, output); 88 } 89 } 90 91 95 public String getVar() { 96 return var; 97 } 98 99 101 public void setVar(String var) { 102 this.var = var; 103 } 104 105 109 public String getText() { 110 return text; 111 } 112 113 117 public void setText(String text) { 118 this.text = text; 119 } 120 121 122 123 public XMLReader getXMLReader() throws ParserConfigurationException , SAXException { 124 if (xmlReader == null) { 125 xmlReader = createXMLReader(); 126 } 127 return xmlReader; 128 } 129 130 131 public void setXMLReader(XMLReader xmlReader) { 132 this.xmlReader = xmlReader; 133 } 134 135 136 139 public XMLParser getJellyParser() { 140 if (jellyParser == null) { 141 jellyParser = createJellyParser(); 142 } 143 return jellyParser; 144 } 145 146 150 public void setJellyParser(XMLParser jellyParser) { 151 this.jellyParser = jellyParser; 152 } 153 154 155 158 161 protected XMLReader createXMLReader() throws ParserConfigurationException , SAXException { 162 SAXParserFactory factory = SAXParserFactory.newInstance(); 163 factory.setNamespaceAware(true); 164 SAXParser parser = factory.newSAXParser(); 165 return parser.getXMLReader(); 166 } 167 168 169 172 protected void parseBody(XMLOutput output) throws JellyTagException { 173 ContentHandler handler = getJellyParser(); 174 XMLOutput newOutput = new XMLOutput(handler); 175 176 try { 177 handler.startDocument(); 178 invokeBody(newOutput); 179 handler.endDocument(); 180 } 181 catch (SAXException e) { 182 throw new JellyTagException(e); 183 } 184 } 185 186 189 protected void parseText(String text) throws JellyTagException { 190 if ( log.isDebugEnabled() ) { 191 log.debug( "About to parse: " + text ); 192 } 193 194 try { 195 XMLReader xmlReader = getXMLReader(); 196 xmlReader.setContentHandler(getJellyParser()); 197 xmlReader.parse( new InputSource ( new StringReader ( text ) ) ); 198 } 199 catch (Exception e) { 200 throw new JellyTagException(e); 201 } 202 } 203 204 208 protected XMLParser createJellyParser() { 209 XMLParser answer = new XMLParser(); 210 answer.setContext(context); 211 return answer; 212 } 213 } 214 | Popular Tags |