001    package railo.runtime.tag;
002    
003    import java.io.StringReader;
004    
005    import org.xml.sax.InputSource;
006    
007    import railo.commons.lang.StringUtil;
008    import railo.runtime.exp.PageException;
009    import railo.runtime.ext.tag.BodyTagImpl;
010    import railo.runtime.op.Caster;
011    import railo.runtime.text.xml.XMLCaster;
012    import railo.runtime.text.xml.XMLUtil;
013    
014    /**
015    * Creates a XML document object that contains the markup in the tag body. This tag can include XML and CFML tags. Railo processes the CFML code in the tag body, then assigns the resulting text to an XML document object variable.
016    *
017    *
018    *
019    **/
020    public final class Xml extends BodyTagImpl {
021    
022            /** name of an xml variable */
023            private String variable;
024            private String validator;
025    
026            /** yes: maintains the case of document elements and attributes */
027            private boolean casesensitive;
028    
029            private String strXML;
030    
031    
032            @Override
033            public void release()   {
034                    super.release();
035                    variable=null;
036                    casesensitive=false;
037                    strXML=null;
038                    validator=null;
039            }
040    
041            /** set the value variable
042            *  name of an xml variable
043            * @param variable value to set
044            **/
045            public void setVariable(String variable)        {
046                    this.variable=variable;
047            }
048    
049            /** set the value casesensitive
050            *  yes: maintains the case of document elements and attributes
051            * @param casesensitive value to set
052            **/
053            public void setCasesensitive(boolean casesensitive)     {
054                    this.casesensitive=casesensitive;
055            }
056    
057    
058            @Override
059            public int doStartTag() {
060                    return EVAL_BODY_BUFFERED;
061            }
062    
063            @Override
064            public int doEndTag() throws PageException      {
065                    try {
066                            InputSource vis = StringUtil.isEmpty(validator)?null:XMLUtil.toInputSource(pageContext,validator);
067                            pageContext.setVariable(variable,XMLCaster.toXMLStruct(XMLUtil.parse(new InputSource(new StringReader(strXML)),vis,false),casesensitive));
068                    } 
069                    catch (Exception e) {
070                            throw Caster.toPageException(e);
071                    }
072                    
073                    return EVAL_PAGE;
074            }
075    
076            @Override
077            public void doInitBody()        {
078                    
079            }
080    
081            @Override
082            public int doAfterBody()        {
083                    strXML=bodyContent.getString().trim();
084                    return SKIP_BODY;
085            }
086    
087            /**
088             * @param validator the validator to set
089             */
090            public void setValidator(String validator) {
091                    this.validator = validator;
092            }
093    }