001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements. See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership. The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the  "License");
007     * you may not use this file except in compliance with the License.
008     * You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    /*
019     * $Id: ElemMessage.java 468643 2006-10-28 06:56:03Z minchau $
020     */
021    package org.apache.xalan.templates;
022    
023    import javax.xml.transform.TransformerException;
024    
025    import org.apache.xalan.res.XSLMessages;
026    import org.apache.xalan.res.XSLTErrorResources;
027    import org.apache.xalan.transformer.TransformerImpl;
028    
029    /**
030     * Implement xsl:message.
031     * <pre>
032     * <!ELEMENT xsl:message %template;>
033     * <!ATTLIST xsl:message
034     *   %space-att;
035     *   terminate (yes|no) "no"
036     * >
037     * </pre>
038     * @see <a href="http://www.w3.org/TR/xslt#message">message in XSLT Specification</a>
039     * @xsl.usage advanced
040     */
041    public class ElemMessage extends ElemTemplateElement
042    {
043        static final long serialVersionUID = 1530472462155060023L;
044    
045      /**
046       * If the terminate attribute has the value yes, then the
047       * XSLT transformer should terminate processing after sending
048       * the message. The default value is no.
049       * @serial
050       */
051      private boolean m_terminate = Constants.ATTRVAL_NO;  // default value 
052    
053      /**
054       * Set the "terminate" attribute.
055       * If the terminate attribute has the value yes, then the
056       * XSLT transformer should terminate processing after sending
057       * the message. The default value is no.
058       *
059       * @param v Value to set for "terminate" attribute. 
060       */
061      public void setTerminate(boolean v)
062      {
063        m_terminate = v;
064      }
065    
066      /**
067       * Get the "terminate" attribute.
068       * If the terminate attribute has the value yes, then the
069       * XSLT transformer should terminate processing after sending
070       * the message. The default value is no.
071       *
072       * @return value of "terminate" attribute.
073       */
074      public boolean getTerminate()
075      {
076        return m_terminate;
077      }
078    
079      /**
080       * Get an int constant identifying the type of element.
081       * @see org.apache.xalan.templates.Constants
082       *
083       * @return The token ID for this element
084       */
085      public int getXSLToken()
086      {
087        return Constants.ELEMNAME_MESSAGE;
088      }
089    
090      /**
091       * Return the node name.
092       *
093       * @return name of the element 
094       */
095      public String getNodeName()
096      {
097        return Constants.ELEMNAME_MESSAGE_STRING;
098      }
099    
100      /**
101       * Send a message to diagnostics.
102       * The xsl:message instruction sends a message in a way that
103       * is dependent on the XSLT transformer. The content of the xsl:message
104       * instruction is a template. The xsl:message is instantiated by
105       * instantiating the content to create an XML fragment. This XML
106       * fragment is the content of the message.
107       *
108       * @param transformer non-null reference to the the current transform-time state.
109       *
110       * @throws TransformerException
111       */
112      public void execute(
113              TransformerImpl transformer)
114                throws TransformerException
115      {
116    
117        if (transformer.getDebug())
118          transformer.getTraceManager().fireTraceEvent(this);
119    
120        String data = transformer.transformToString(this);
121    
122        transformer.getMsgMgr().message(this, data, m_terminate);
123        
124        if(m_terminate)
125          transformer.getErrorListener().fatalError(new TransformerException(XSLMessages.createMessage(XSLTErrorResources.ER_STYLESHEET_DIRECTED_TERMINATION, null))); //"Stylesheet directed termination"));
126        
127        if (transformer.getDebug())
128              transformer.getTraceManager().fireTraceEndEvent(this); 
129      }
130    }