xslwriter.java

来自「RESIN 3.2 最新源码」· Java 代码 · 共 1,511 行 · 第 1/3 页

JAVA
1,511
字号
/* * Copyright (c) 1998-2008 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.xsl;import com.caucho.log.Log;import com.caucho.util.CharBuffer;import com.caucho.util.IntArray;import com.caucho.util.L10N;import com.caucho.vfs.Path;import com.caucho.xml.*;import com.caucho.xpath.Expr;import com.caucho.xpath.ExprEnvironment;import com.caucho.xpath.NamespaceContext;import com.caucho.xpath.XPathException;import org.w3c.dom.*;import org.xml.sax.SAXException;import java.io.IOException;import java.io.OutputStream;import java.io.Writer;import java.util.ArrayList;import java.util.HashMap;import java.util.Iterator;import java.util.logging.Level;import java.util.logging.Logger;/** * Writer stream for generating stylesheet output. * * <p>Because XSL produces an XML tree, XslWriter contains extra * methods for constructing the tree. * * <p>The writer methods, e.g. println, add to the current text node. * * <p>In addition, stylesheets can access variables through getPwd and * getPage. */public class XslWriter extends Writer implements ExtendedLocator {  static final Logger log = Log.open(XslWriter.class);  static final L10N L = new L10N(XslWriter.class);  // This is the value Axis wants  private final static String XMLNS = "http://www.w3.org/2000/xmlns/";  private final static XMLWriter ATTR_WRITER = new DOMBuilder();  private XMLWriter _xmlWriter;    String _systemId;  String _filename;  int _line;  int _tailLine;  private IntArray flags = new IntArray();    private CharBuffer _text = new CharBuffer();  private String elementName;  private String _attributeURL;  private String _attributePrefix;  private String _attributeLocalName;  private String _attributeName;  private ArrayList depends = new ArrayList();  private boolean _isCacheable = true;  private boolean _disableEscaping;  private boolean generateLocation;  private Document _document;  private StylesheetImpl _stylesheet;  private TransformerImpl _transformer;  private HashMap<String,String> _cdataElements;  private boolean isCdata;  private HashMap<String,String> _namespaces;  private ArrayList<String> _topNamespaces;  private ArrayList<StackItem> _elementStack;  private int _depth;  private ExtendedLocator _locator = null;  XslWriter(HashMap env,            StylesheetImpl stylesheet,            TransformerImpl transformer)  {    _stylesheet = stylesheet;    _transformer = transformer;    ArrayList<String> cdata = stylesheet.getOutputFormat().getCdataSectionElements();    if (cdata != null) {      _cdataElements = new HashMap<String,String>();            for (int i = 0; i < cdata.size(); i++) {        String element = cdata.get(i);        _cdataElements.put(element, element);      }    }  }  void init(XMLWriter xmlWriter)  {    _xmlWriter = xmlWriter;    _namespaces = new HashMap<String,String>();    _topNamespaces = new ArrayList<String>();    _elementStack = new ArrayList<StackItem>();    _document = null;    _locator = this;    xmlWriter.setDocumentLocator(_locator);  }  public TransformerImpl getTransformer()  {    return _transformer;  }  /**   * Returns true if the generated stylesheet is currently cacheable.   */  boolean isCacheable()  {    return _isCacheable;  }  /**   * Returns the Path dependency list of the generated stylesheet.   */  ArrayList getDepends()  {    return depends;  }  /**   * Indicate that the result document is not cacheable.   */  public void setNotCacheable()  {    _isCacheable = false;  }  /**   * Add a dependency to the result document.  When the result is checked   * for modification, this path will also be checked.   */  public void addCacheDepend(Path path)  {    _transformer.addCacheDepend(path);  }  /**   * Implementation function so jsp:decl tags aren't repeated.   */  public boolean isFlagFirst(int id)  {    while (flags.size() <= id)      flags.add(0);    int value = flags.get(id);    flags.set(id, 1);    return value == 0;  }  /**   * Adds a byte to the current text node.   */  public void write(int ch)  {    _text.append((char) ch);  }  /**   * Adds a byte buffer to the current text node.   */  public void write(byte []buf, int offset, int length)  {    for (int i = 0; i < length; i++)      write(buf[offset + i]);  }  /**   * Adds a char buffer to the current text node.   */  public void write(char []buf, int offset, int length)  {    _text.append(buf, offset, length);  }  /**   * Adds a string to the current text node.   */  public void print(String string)  {    if (string == null) {      _text.append("null");      return;    }    _text.append(string);  }  /**   * Adds a boolean to the current text node.   */  public void print(boolean b)  {    _text.append(b);  }  /**   * Adds a character to the current text node.   */  public void print(char ch)  {    _text.append(ch);  }  /**   * Adds an integer to the current text node.   */  public void print(int i)  {    _text.append(i);  }  /**   * Adds an integer to the current text node.   */  public void print(long l)  {    _text.append(l);  }  /**   * Adds a float to the current text node.   */  public void print(float f)  {    _text.append(f);  }  /**   * Adds a double to the current text node.   */  public void print(double d)  {    _text.append(d);  }  /**   * Adds an object to the current text node, converted by   * String.valueOf.   */  public void print(Object o)  {    _text.append(o);  }  /**   * Adds a newline to the current text node.   */  public void println()  {    _text.append('\n');    _tailLine++;  }  /**   * Adds a boolean to the current text node.   */  public void println(boolean b)  {    _text.append(b);    println();  }  /**   * Adds a string to the current text node.   */  public void println(String s)  {    print(s);    println();  }  /**   * Adds a character to the current text node.   */  public void println(char ch)  {    print(ch);    println();  }  /**   * Adds an integer to the current text node.   */  public void println(int i)  {    _text.append(i);    println();  }  /**   * Adds a long to the current text node.   */  public void println(long l)  {    _text.append(l);    println();  }  /**   * Adds a double to the current text node.   */  public void println(double d)  {    _text.append(d);    println();  }  /**   * Adds a float to the current text node.   */  public void println(float f)  {    _text.append(f);    println();  }  /**   * Adds an object to the current text node, converted by String.valueOf.   */  public void println(Object o)  {    _text.append(o);    println();  }  /**   * flush is meaningless for XslWriter.  It's only added to conform to Writer.   */  public void flush()  {  }  public void close()    throws IOException  {    try {      for (int i = 0; i < _topNamespaces.size(); i++) {        String topPrefix = _topNamespaces.get(i);        String topUrl = _namespaces.get(topPrefix);        if (topPrefix.equals(""))	  _xmlWriter.endPrefixMapping(null);        else	  _xmlWriter.endPrefixMapping(topPrefix);      }      popText();      _xmlWriter.endDocument();    } catch (SAXException e) {      throw new IOException(e.toString());    }  }  public boolean getDisableEscaping()  {    return _disableEscaping;  }    public boolean disableEscaping(boolean disable)    throws IOException, SAXException  {    if (disable != _disableEscaping) {      popText();      _xmlWriter.setEscapeText(! disable);    }    boolean old = _disableEscaping;    _disableEscaping = disable;    return old;  }  public void setLocation(String systemId, String filename, int line)    throws IOException, SAXException  {    // Don't need to pop the text if the line # matches    if (filename == null || ! filename.equals(_filename) ||        line != _tailLine)      popText();    _systemId = systemId;    _filename = filename;    _line = line;    _tailLine = line;  }  /**   * Adds a new element to the current node, making the new element the   * current node.   *   * <p>Each pushElement should be matched by a popElement.   *   * @param name name of the element   */  public void pushElement(String name)   throws IOException, SAXException  {    popText();    String local;    int p = name.lastIndexOf(':');    if (p > 0)      local = name.substring(p + 1);    else      local = name;        startElement(null, null, local, name);  }  /**   * Adds a new element to the current node, making the new element the   * current node.   *   * <p>Each pushElement should be matched by a popElement.   *   * @param name name of the element   * @param namespace namespace context   */  public void pushElement(String name, NamespaceContext namespace)   throws IOException, SAXException  {    popText();    // Look up the proper namespace for the element.    int p = name.indexOf(':');    if (p <= 0) {      startElement(null, null, name, name);      return;    }    String prefix = name.substring(0, p);    String url = namespace.find(namespace, prefix);        if (url != null)      startElement(url, prefix, name.substring(p + 1), name);    else      startElement(null, null, name, name);  }  /**   * Adds a new element to the current node, making the new element the   * current node.   *   * <p>Each pushElement should be matched by a popElement.   *   * @param name name of the element   * @param url namespace url   */  public void pushElementNs(String name, String url)   throws IOException, SAXException  {    popText();        // Look up the proper namespace for the element.    int p = name.indexOf(':');    if (p <= 0) {      startElement(url, "", name, name);      return;    }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?