xslwriter.java

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

JAVA
1,511
字号
    popText();    if (value instanceof NodeList) {      NodeList list = (NodeList) value;      int length = list.getLength();      for (int i = 0; i < length; i++) {	Node child = list.item(i);	copyOf(child);      }    }    else if (value instanceof ArrayList) {      ArrayList list = (ArrayList) value;      for (int i = 0; i < list.size(); i++) {	Node child = (Node) list.get(i);	copyOf(child);      }    }    else if (value instanceof Iterator) {      Iterator iter = (Iterator) value;      while (iter.hasNext()) {	Node child = (Node) iter.next();	copyOf(child);      }    }    else if (value instanceof Attr) {      Attr child = (Attr) value;      attribute(child.getNamespaceURI(),                child.getPrefix(),                child.getLocalName(),                child.getNodeName(),                child.getNodeValue());    }    else if (value instanceof QElement) {      QElement child = (QElement) value;      String oldSystemId = _systemId;      String oldFilename = _filename;      int oldLine = _line;      _systemId = child.getBaseURI();      _filename = child.getFilename();      _line = child.getLine();      startElement(child.getNamespaceURI(),                   child.getPrefix(),                   child.getLocalName(),                   child.getNodeName());      Node subNode = child.getFirstAttribute();      for (; subNode != null; subNode = subNode.getNextSibling()) {        QAttr attr = (QAttr) subNode;                attribute(attr.getNamespaceURI(),                  attr.getPrefix(),                  attr.getLocalName(),                  attr.getNodeName(),                  attr.getNodeValue());      }            for (subNode = child.getFirstChild();           subNode != null;           subNode = subNode.getNextSibling()) {        copyOf(subNode);      }      popElement();      _systemId = oldSystemId;      _filename = oldFilename;      _line = oldLine;    }    else if (value instanceof DocumentFragment) {      for (Node subNode = ((Node) value).getFirstChild();           subNode != null;           subNode = subNode.getNextSibling()) {        copyOf(subNode);      }    }    else if (value instanceof Text) {      Text child = (Text) value;      _text.append(child.getNodeValue());    }    else if (value instanceof Comment) {      Comment child = (Comment) value;      _xmlWriter.comment(child.getNodeValue());    }    else if (value instanceof ProcessingInstruction) {      ProcessingInstruction pi = (ProcessingInstruction) value;            _xmlWriter.processingInstruction(pi.getNodeName(),				       pi.getNodeValue());    }    else if (value instanceof EntityReference) {      EntityReference child = (EntityReference) value;      _text.append("&" + child.getNodeName() + ";");    }    else if (value instanceof Node) {      Node child = (Node) value;      _text.append(child.getNodeValue());    }    else {      print(Expr.toString(value));    }  }  public void addNamespace(String prefix, String url)  {    /*    if (url.startsWith("quote:"))      url = url.substring(6);    */    if (! url.equals("")) {      _namespaces.put(prefix, url);      _topNamespaces.add(prefix);    }  }  void startElement(String url, String prefix, String local, String qName)    throws IOException, SAXException  {    if (_attributeName != null)      throw error(L.l("element `{0}' is not allowed inside attribute `{1}'.  xsl:attribute must contain text only.", qName, _attributeName));    popText();    StackItem item = null;    if (_elementStack.size() <= _depth) {      item = new StackItem();      _elementStack.add(item);    }    else      item = _elementStack.get(_depth);    item.init(url, prefix, local, qName, isCdata);    if (_cdataElements != null && _cdataElements.get(qName) != null)      isCdata = true;        _depth++;    _xmlWriter.startElement(url, local, qName);    // Initialize top-level namespaces    if (_depth == 1) {      for (int i = 0; i < _topNamespaces.size(); i++) {        String topPrefix = _topNamespaces.get(i);        String topUrl = _namespaces.get(topPrefix);        if (topPrefix.equals("")) {	  _xmlWriter.startPrefixMapping(null, topUrl);          _xmlWriter.attribute(XMLNS, null, "xmlns", topUrl);	}        else {	  _xmlWriter.startPrefixMapping(topPrefix, topUrl);          _xmlWriter.attribute(XMLNS, topPrefix, "xmlns:" + topPrefix, topUrl);	}      }    }    if (url == null)      return;    bindNamespace(prefix, url);  }  public void popElement()    throws IOException, SAXException  {    popText();    _depth--;    StackItem item = _elementStack.get(_depth);        try{      _xmlWriter.endElement(item.getNamespace(),                           item.getLocalName(),                           item.getName());      // If this element bound namespaces, pop the old values      for (int i = 0; i < item.nsSize(); i++) {        String oldPrefix = item.getNSPrefix(i);        String oldUrl = item.getNSUrl(i);        if (oldUrl == null)          _namespaces.remove(oldPrefix);        else          _namespaces.put(oldPrefix, oldUrl);	_xmlWriter.endPrefixMapping(oldPrefix);      }      isCdata = item.getCdata();    } catch (Throwable e) {      log.log(Level.FINE, e.toString(), e);    }  }  /**   * Sends the attribute to the output   *   * @param url the namespace for the attribute name   * @param prefix the prefix for the attribute name   * @param local the local attribute name   * @param qName the full qualified name   * @param value the attribute's value   */  public void attribute(String url, String prefix, String local,			String qName, String value)    throws IOException, SAXException  {    if (qName.startsWith("xmlns:"))      bindNamespace(qName.substring("xmlns:".length()), value);    else if (qName.equals("xmlns"))      bindNamespace(null, value);    else {      _xmlWriter.attribute(url, local, qName, value);      // null namespace binding doesn't add binding      if (url != null && ! url.equals("") && ! prefix.equals(""))        bindNamespace(prefix, url);    }  }  /**   * Sends the attribute to the output   *   * @param url the namespace for the attribute name   * @param prefix the prefix for the attribute name   * @param local the local attribute name   * @param qName the full qualified name   * @param value the attribute's value   */  public void attribute(String qName, String value)    throws IOException, SAXException  {    _xmlWriter.attribute(null, null, qName, value);  }  public void bindNamespace(String prefix, String url)    throws IOException, SAXException  {    String oldUrl = _namespaces.get(prefix);    // If the namespace matches, return    if (oldUrl == null && url.equals("") ||        oldUrl != null && url.equals(oldUrl))      return;    // Send the namespace declaration to the writer    if (prefix != null) {      _xmlWriter.startPrefixMapping(prefix, url);      _xmlWriter.attribute(XMLNS, prefix, "xmlns:" + prefix, url);      _namespaces.put(prefix, url);    }    else {      _xmlWriter.startPrefixMapping(null, url);      _xmlWriter.attribute(XMLNS, null, "xmlns", url);      _namespaces.put(null, url);    }    StackItem item = _elementStack.get(_depth - 1);    item.addNamespace(prefix, oldUrl);  }  /**   * Pop the accumulated text to the DOM.   */  public void popText()    throws IOException, SAXException  {    if (_xmlWriter == ATTR_WRITER)      return;        Text textNode = null;        if (_text.length() == 0)      return;        if (_filename != null)      _line = _tailLine;    if (isCdata)      _xmlWriter.cdata(_text.getBuffer(), 0, _text.getLength());    else      _xmlWriter.text(_text.getBuffer(), 0, _text.getLength());        _text.clear();  }  /**   * Returns the attribute with the given name.   */  public Object getProperty(String name)  {    return _transformer.getProperty(name);  }  /**   * Sets the attribute with the given name.   */  public void setProperty(String name, Object value)  {    _transformer.setProperty(name, value);  }  /**   * removes the attribute with the given name.   */  public void removeProperty(String name)  {  }  /**   * Lists the names of all the attributes.   */  public Iterator getPropertyNames()  {    return null;  }  public Object getParameter(String name)  {    return _transformer.getParameter(name);  }  public Path getPwd()  {    return (Path) getProperty("caucho.pwd");  }  public OutputStream openWrite(ExprEnvironment env, String href)    throws IOException  {    if (_xmlWriter instanceof XmlPrinter) {      XmlPrinter printer = (XmlPrinter) _xmlWriter;      Path path = printer.getPath();      if (path != null) {	Path dst = path.getParent().lookup(href);	dst.getParent().mkdirs();	        return dst.openWrite();      }    }        Path stylesheetPath = env.getStylesheetEnv().getPath();        return stylesheetPath.getParent().lookup(href).openWrite();  }  public XslWriter openResultDocument(OutputStream os)    throws IOException, SAXException  {    XMLWriter writer = new XmlPrinter(os);    XslWriter out = new XslWriter(null, _stylesheet, _transformer);    out.init(writer);    writer.startDocument();    return out;  }  /**   * @deprecated   */  public javax.servlet.jsp.PageContext getPage()  {    return (javax.servlet.jsp.PageContext) getProperty("caucho.page.context");  }  private IOException error(String message)  {    if (_filename != null)      return new IOException(_filename + ":" + _line + ": " + message);    else      return new IOException(message);  }    public String getSystemId()  {    if (_systemId != null)      return _systemId;    else      return _filename;  }      public String getFilename()  {    if (_filename != null)      return _filename;    else      return _systemId;  }      public String getPublicId()  {    return null;  }  public int getLineNumber()  {    return _line;  }  public int getColumnNumber()  {    return 0;  }  static class StackItem {    String _url;    String _prefix;    String _local;    String _qName;    boolean _isCdata;    ArrayList<String> _nsPrefixes;    ArrayList<String> _nsUrls;    void clear()    {    }    void init(String url, String prefix, String local, String qName,              boolean isCdata)    {      if (_nsPrefixes != null) {        _nsPrefixes.clear();        _nsUrls.clear();      }      _url = url;      _prefix = prefix;      _local = local;      _qName = qName;      _isCdata = isCdata;    }    String getNamespace()    {      return _url;    }    String getPrefix()    {      return _prefix;    }    String getLocalName()    {      return _local;    }    String getName()    {      return _qName;    }    boolean getCdata()    {      return _isCdata;    }    int nsSize()    {      return _nsPrefixes == null ? 0 : _nsPrefixes.size();    }    String getNSPrefix(int i)    {      return _nsPrefixes.get(i);    }    String getNSUrl(int i)    {      return _nsUrls.get(i);    }    void addNamespace(String prefix, String oldUrl)    {      if (_nsPrefixes == null) {        _nsPrefixes = new ArrayList<String>();        _nsUrls = new ArrayList<String>();      }      _nsPrefixes.add(prefix);      _nsUrls.add(oldUrl);    }  }}

⌨️ 快捷键说明

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