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

com.caucho.jsp.java.JspTop 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 Software Foundation, 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.LinkedHashMap;
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.XmlChar;

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

  private boolean _hasRoot;
  private int _maxFragmentIndex = -1;
  private int _maxStaticFragmentIndex = -1;

  private Map _namespaceMap = new TreeMap();
  private Map _revNamespaceMap
    = new TreeMap();

  /**
   * Adds a text node.
   */
  public JspNode addText(String text)
    throws JspParseException
  {
    // jsp/0705
    if (true || ! _hasRoot) {
      JspNode node = new StaticText(_gen, text, this);

      addChild(node);

      return node;
    }
    else {
      for (int i = 0; i < text.length(); i++)
        if (! XmlChar.isWhitespace(text.charAt(i)))
          throw error(L.l("JSP pages with  must not have text outside the ."));

      return null;
    }
  }

  /**
   * Adds a child node.
   */
  public void addChild(JspNode child)
    throws JspParseException
  {
    if (child instanceof JspRoot) {
      _hasRoot = true;
      _gen.setOmitXmlDeclaration(true);
    }
    
    super.addChild(child);
  }
  
  /**
   * 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;
  }

  /**
   * Returns the containing segment.
   */
  public JspSegmentNode getSegment()
  {
    return this;
  }
  
  /**
   * Returns the largest fragment index.
   */
  public int getMaxFragmentIndex()
  {
    return _maxFragmentIndex;
  }
  
  /**
   * Sets the largest fragment index.
   */
  public void setMaxFragmentIndex(int index)
  {
    if (_maxFragmentIndex < index)
      _maxFragmentIndex = index;
  }
  
  /**
   * Returns the largest static fragment index.
   */
  public int getMaxStaticFragmentIndex()
  {
    return _maxStaticFragmentIndex;
  }
  
  /**
   * Sets the largest static fragment index.
   */
  public void setMaxStaticFragmentIndex(int index)
  {
    if (_maxStaticFragmentIndex < index)
      _maxStaticFragmentIndex = index;
  }

  /**
   * 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);
  }

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

  /**
   * Returns true if the namespace decl has been printed.
   */
  public boolean hasNamespace(String prefix, String uri)
  {
    if ("".equals(uri) && ("".equals(prefix) || prefix == null))
      return true;
    else
      return false;
  }

  /**
   * Generates the code for the tag
   *
   * @param out the output writer for the generated java.
   */
  public void generate(JspJavaWriter out)
    throws Exception
  {
    if (! _hasRoot) {
      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