All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.caucho.jsp.java.JspRoot Maven / Gradle / Ivy

/*
 * Copyright (c) 1998-2018 Caucho Technology -- all rights reserved
 *
 * This file is part of Resin(R) Open Source
 *
 * Each copy or derived work must preserve the copyright notice and this
 * notice unmodified.
 *
 * Resin Open Source is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Resin Open Source is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
 * of NON-INFRINGEMENT.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Resin Open Source; if not, write to the
 *   Free SoftwareFoundation, Inc.
 *   59 Temple Place, Suite 330
 *   Boston, MA 02111-1307  USA
 *
 * @author Scott Ferguson
 */

package com.caucho.jsp.java;

import java.io.IOException;
import java.util.Map;
import java.util.TreeMap;

import com.caucho.jsp.JspParseException;
import com.caucho.util.L10N;
import com.caucho.vfs.WriteStream;
import com.caucho.xml.QName;
import com.caucho.xml.XmlChar;

/**
 * Represents the root node.
 */
public class JspRoot extends JspContainerNode {
  static final L10N L = new L10N(JspRoot.class);

  static final private QName VERSION = new QName("version");

  private String _version;

  private TreeMap _namespaceMap
    = new TreeMap();

  private TreeMap _revNamespaceMap
    = new TreeMap();

  /**
   * Sets the versino.
   */
  public void setVersion(String version)
  {
    _version = version;
  }
  
  /**
   * Adds an attribute.
   *
   * @param name the attribute name
   * @param value the attribute value
   */
  public void addAttribute(QName name, String value)
    throws JspParseException
  {
    if (VERSION.equals(name)) {
      if (! value.equals("2.1")
          && ! value.equals("2.0")
          && ! value.equals("1.2"))
        throw error(L.l("'{0}' is an unsupported jsp:root version.",
                        value));

      _version = value;
    }
    else {
      throw error(L.l("'{0}' is an unknown jsp:root attribute.  'version' is the only allowed JSP root value.",
                      name.getName()));
    }
  }

  /**
   * Adds a text node.
   */
  public JspNode addText(String text)
    throws JspParseException
  {
    for (int i = 0; i < text.length(); i++) {
      if (! XmlChar.isWhitespace(text.charAt(i))) {
        JspNode node = new StaticText(_gen, text, this);

        addChild(node);

        return node;
      }
    }

    return null;
  }

  /**
   * Called after all the attributes from the tag.
   */
  public void endAttributes()
    throws JspParseException
  {
    _gen.setOmitXmlDeclaration(true);

    if (getParent() != null && ! (getParent() instanceof JspTop))
      throw error(L.l("jsp:root must be the root JSP element."));

    if (_version == null)
      throw error(L.l("'version' is a required attribute of jsp:root"));
  }
  
  /**
   * Adds a child node.
   */
  public void addChild(JspNode node)
    throws JspParseException
  {
    super.addChild(node);
  }

  /**
   * Adds a namespace, e.g. from a prefix declaration.
   */
  @Override
  public void addNamespaceRec(String prefix, String value)
  {
    _namespaceMap.put(prefix, value);
    _revNamespaceMap.put(value, prefix);
  }

  /**
   * Adds a namespace, e.g. from a prefix declaration.
   */
  @Override
  public String getNamespacePrefix(String uri)
  {
    return _revNamespaceMap.get(uri);
  }
  
  /**
   * Set true if the node only has static text.
   */
  public boolean isStatic()
  {
    for (int i = 0; i < _children.size(); i++) {
      JspNode node = _children.get(i);

      if (! node.isStatic())
        return false;
    }

    return true;
  }

  /**
   * Generates the XML text representation for the tag validation.
   *
   * @param os write stream to the generated XML.
   */
  public void printXml(WriteStream os)
    throws IOException
  {
    os.print("");
    printXmlChildren(os);
    os.print("");
  }

  /**
   * Generates the code for the tag
   *
   * @param out the output writer for the generated java.
   */
  public void generate(JspJavaWriter out)
    throws Exception
  {
    if (! _gen.isOmitXmlDeclaration()) {
      String encoding = _gen.getCharacterEncoding();

      if (encoding == null)
        encoding = "UTF-8";

      out.addText("\n");
    }

    if (_gen.getDoctypeSystem() != null) {
      out.addText("\n");
    }
    
    generateChildren(out);
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy