001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019package lucee.runtime.tag;
020
021import java.io.StringReader;
022
023import lucee.commons.lang.StringUtil;
024import lucee.runtime.exp.PageException;
025import lucee.runtime.ext.tag.BodyTagImpl;
026import lucee.runtime.op.Caster;
027import lucee.runtime.text.xml.XMLCaster;
028import lucee.runtime.text.xml.XMLUtil;
029
030import org.xml.sax.InputSource;
031
032/**
033* Creates a XML document object that contains the markup in the tag body. This tag can include XML and CFML tags. Lucee processes the CFML code in the tag body, then assigns the resulting text to an XML document object variable.
034*
035*
036*
037**/
038public final class Xml extends BodyTagImpl {
039
040        /** name of an xml variable */
041        private String variable;
042        private String validator;
043
044        /** yes: maintains the case of document elements and attributes */
045        private boolean casesensitive;
046
047        private String strXML;
048
049
050        @Override
051        public void release()   {
052                super.release();
053                variable=null;
054                casesensitive=false;
055                strXML=null;
056                validator=null;
057        }
058
059        /** set the value variable
060        *  name of an xml variable
061        * @param variable value to set
062        **/
063        public void setVariable(String variable)        {
064                this.variable=variable;
065        }
066
067        /** set the value casesensitive
068        *  yes: maintains the case of document elements and attributes
069        * @param casesensitive value to set
070        **/
071        public void setCasesensitive(boolean casesensitive)     {
072                this.casesensitive=casesensitive;
073        }
074
075
076        @Override
077        public int doStartTag() {
078                return EVAL_BODY_BUFFERED;
079        }
080
081        @Override
082        public int doEndTag() throws PageException      {
083                try {
084                        InputSource vis = StringUtil.isEmpty(validator)?null:XMLUtil.toInputSource(pageContext,validator);
085                        pageContext.setVariable(variable,XMLCaster.toXMLStruct(XMLUtil.parse(new InputSource(new StringReader(strXML)),vis,false),casesensitive));
086                } 
087                catch (Exception e) {
088                        throw Caster.toPageException(e);
089                }
090                
091                return EVAL_PAGE;
092        }
093
094        @Override
095        public void doInitBody()        {
096                
097        }
098
099        @Override
100        public int doAfterBody()        {
101                strXML=bodyContent.getString().trim();
102                return SKIP_BODY;
103        }
104
105        /**
106         * @param validator the validator to set
107         */
108        public void setValidator(String validator) {
109                this.validator = validator;
110        }
111}