generator.java

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

JAVA
2,537
字号
    if (resultPrefix.equals("#default")) {      resultPrefix = "";      resultNs = "";    }    else if (resultNs.equals(""))      throw error("`" + resultPrefix + "' is not a valid namespace prefix");        String result[] = new String[] { resultPrefix, resultNs };    _namespaceAliases.put(stylesheetNs, result);    */  }  public void addNamespaceAlias(String namespace, String []result)  {    _namespaceAliases.put(namespace, result);  }  public String []getNamespaceAlias(String namespace)  {    return _namespaceAliases.get(namespace);  }  /**   * Scans through the stylesheet, grabbing the attribute set   * definitions.   *   * @param element the current nost   */  /*  void generateAttributeSets(Element element)    throws Exception  {    Node child = element.getFirstChild();    for (; child != null; child = child.getNextSibling()) {      if (! "attribute-set".equals(getXslLocal(child)))	continue;      QElement elt = (QElement) child;      String name = elt.getAttribute("name");      if (name.equals(""))	throw error(L.l("xsl:attribute-set expects `name' attribute."));      generateAttributeSet(element, name);    }  }  */  /**   * Scans through the stylesheet, grabbing the attribute set   * definitions.   *   * @param element the current node   */  /*  HashMap<String,String> generateAttributeSet(Element element, String setName)    throws Exception  {    Node child = element.getFirstChild();    for (; child != null; child = child.getNextSibling()) {      if (! "attribute-set".equals(getXslLocal(child)))	continue;      QElement elt = (QElement) child;      String name = elt.getAttribute("name");      if (name.equals(""))	throw error(L.l("xsl:attribute-set expects `name' attribute."));      if (! name.equals(setName))        continue;      HashMap<String,String> set = _attributeSets.get(name);      if (set != null)        return set;      set = new HashMap<String,String>();      _attributeSets.put(name, set);      // add any attributes from use-attribute-sets      for (Node attr = elt.getFirstAttribute();	   attr != null;	   attr = attr.getNextSibling()) {	if (attr.getNodeName().equals("use-attribute-sets")) {	  HashMap<String,String> subset = generateAttributeSet(element, attr.getNodeValue());          	  if (subset == null)	    throw error(elt, L.l("Unknown attribute-set `{0}'.  Each use-attribute-sets needs a matching xsl:attribute-set.", attr.getNodeValue()));	  Iterator<String> iter = subset.keySet().iterator();	  while (iter.hasNext()) {	    String key = iter.next();	    set.put(key, subset.get(key));	  }	}      }      for (Node attr = elt.getFirstChild();	   attr != null;	   attr = attr.getNextSibling()) {	if (! "attribute".equals(getXslLocal(attr)))	  continue;	Element attrElt = (Element) attr;	String attrName = attrElt.getAttribute("name");	if (attrName.equals(""))	  throw error(L.l("xsl:attribute expects `name' attribute."));	set.put(attrName, ((QElement) attr).getTextValue());      }      for (Attr attr = ((QElement) elt).getFirstAttribute();	   attr != null;	   attr = (Attr) attr.getNextSibling()) {	if (attr.getNodeName().equals("name") ||	    attr.getNodeName().equals("use-attribute-sets"))	  continue;	set.put(attr.getNodeName(), attr.getNodeValue());      }      return set;    }    return null;  }  */  public void addAttributeSet(String name, XslAttributeSet attributeSet)  {    _attributeSets.put(name, attributeSet);  }  /*  public XslAttributeSet getAttributeSet(String name)  {    return _attributeSets.get(name);  }  */  public void setDisableOutputEscaping(boolean disable)  {    _isRawText = disable;  }  public boolean getDisableOutputEscaping()  {    return _isRawText;  }    private void generateOutput(Element element)    throws Exception  {    Node attr = ((QElement) element).getFirstAttribute();    if (element.getFirstChild() != null)      throw error("xsl:output must be empty");    String disableEscaping;    disableEscaping = element.getAttribute("resin:disable-output-escaping");    if (disableEscaping.equals(""))      disableEscaping = element.getAttribute("disable-output-escaping");    if (disableEscaping.equals("no") ||        disableEscaping.equals("false"))      _isRawText = false;    else if (! disableEscaping.equals(""))      _isRawText = true;    // Only top-level xsl:output matters (XXX: spec?)    if (! _isTop)      return;    if (_outputAttributes == null)      _outputAttributes = new HashMap<String,String>();    for (; attr != null; attr = attr.getNextSibling()) {      _outputAttributes.put(attr.getNodeName(), attr.getNodeValue());    }  }  public void setOutputAttribute(String name, String value)  {    _outputAttributes.put(name, value);  }  private void generatePreserveSpace(Element element)    throws Exception  {    String elements = element.getAttribute("elements");    if (elements.equals(""))      throw error("xsl:preserve-space expects `elements' attribute.");        if (element.getFirstChild() != null)      throw error("xsl:preserve-space must be empty");        int i = 0;    int len = elements.length();    for (; i < len && XmlChar.isWhitespace(elements.charAt(i)); i++) {    }    CharBuffer cb = new CharBuffer();    while (i < len) {      cb.clear();      for (; i < len && ! XmlChar.isWhitespace(elements.charAt(i)); i++)	cb.append(elements.charAt(i));      _preserve.put(cb.toString(), "true");      for (; i < len && XmlChar.isWhitespace(elements.charAt(i)); i++) {      }    }  }  private void generateStripSpace(Element element)    throws Exception  {    throw new UnsupportedOperationException();    /*    String elements = element.getAttribute("elements");    if (elements.equals(""))      throw error("xsl:strip-space expects `elements' attribute.");        if (element.getFirstChild() != null)      throw error("xsl:strip-space must be empty");    */  }  public void addStripSpace(String elements)  {    int i = 0;    int len = elements.length();    for (; i < len && XmlChar.isWhitespace(elements.charAt(i)); i++) {    }    CharBuffer cb = new CharBuffer();    while (i < len) {      cb.clear();      for (; i < len && ! XmlChar.isWhitespace(elements.charAt(i)); i++) {	cb.append(elements.charAt(i));      }      _strip.put(cb.toString(), "true");      for (; i < len && XmlChar.isWhitespace(elements.charAt(i)); i++) {      }    }  }  public void addPreserveSpace(String elements)  {    int i = 0;    int len = elements.length();    for (; i < len && XmlChar.isWhitespace(elements.charAt(i)); i++) {    }    CharBuffer cb = new CharBuffer();    while (i < len) {      cb.clear();      for (; i < len && ! XmlChar.isWhitespace(elements.charAt(i)); i++) {	cb.append(elements.charAt(i));      }      _preserve.put(cb.toString(), "true");      for (; i < len && XmlChar.isWhitespace(elements.charAt(i)); i++) {      }    }  }  protected void generateChildren(Node node)    throws Exception  {    _vars.add(0);    for (Node child = node.getFirstChild();         child != null;	 child = child.getNextSibling()) {      generateChild(child);    }    int count = _vars.pop();    if (count > 0 && _vars.size() > 0)      printPopScope(count);  }    protected void generateChild(Node child)    throws Exception  {    generateChildImpl(child);  }  public void generateChildImpl(Node child)    throws Exception  {    String nodeName = getXslLocal(child);    int code = -1;    if (nodeName != null)      code = _tags.get(nodeName);    else if ((nodeName = getXtpLocal(child)) != null)      code = _xtpTags.get(nodeName);    /* XXX: xsl/04al    else if (macros.get(child.getNodeName()) != null) {      generateMacro((Element) child);      return;    }    */    if (nodeName == null) {      if (child.getNodeType() == child.TEXT_NODE)        generateText(child);      else if (child.getNodeType() == child.ELEMENT_NODE) {        NamespaceContext oldNamespace = addNamespace((Element) child);        printElement((Element) child);        _namespace = oldNamespace;      }      return;    }    if (child instanceof QElement) {      NamespaceContext oldNamespace = addNamespace((QElement) child);      generateChild(child, code);      _namespace = oldNamespace;    }    else      generateChild(child, code);  }    public void generateChild(Node child, int code)    throws Exception  {    if (child instanceof QAbstractNode) {      QAbstractNode qChild = (QAbstractNode) child;      setLocation(qChild.getBaseURI(), qChild.getFilename(), qChild.getLine());    }    switch (code) {    case TEXT:      generateText(child);      break;    case XSL_TEXT:      generateXslText((Element) child);      break;    case APPLY_TEMPLATES:      generateApplyTemplates((Element) child);      break;    case APPLY_IMPORTS:      generateApplyImports((Element) child);      break;    case CALL_TEMPLATE:      generateCallTemplate((Element) child);      break;    case PARAM:      generateParamVariable((Element) child);      break;    case VARIABLE:      generateVariable((Element) child);      break;    case VALUE_OF:      generateValueOf((Element) child);      break;    case COPY_OF:      generateCopyOf((Element) child);      break;    case FOR_EACH:      generateForEach((Element) child);      break;    case IF:      generateIf((Element) child);      break;    case CHOOSE:      generateChoose((Element) child);      break;    case NUMBER:      generateNumber((Element) child);      break;    case COPY:      printCopy((Element) child);      break;    case COPY_ELEMENT:      printCopyElement((Element) child);      break;    case ELEMENT:      generateElement((Element) child);      break;    case ATTRIBUTE:      generateAttribute((Element) child);      break;    case PI:      printPi((Element) child);      break;    case COMMENT:      printComment((Element) child);      break;    case MESSAGE:      printMessage((Element) child);      break;    case EXPRESSION:      if (! _defaultCacheable)	_isCacheable = false;      printExpression((Element) child);      break;    case SCRIPTLET:      if (! _defaultCacheable)	_isCacheable = false;      printScriptlet((Element) child);      break;    case DIRECTIVE_CACHE:      generateCacheDepends(((Element) child).getAttribute("file"));      if (! ((Element) child).getAttribute("no-cache").equals("")) {	_isCacheable = false;	_defaultCacheable = false;      }      else	_defaultCacheable = true;      break;    case WHILE:      generateWhile((Element) child);      break;    case ASSIGN:      generateAssign((Element) child);      break;    case RESULT_DOCUMENT:      generateResultDocument((Element) child);      break;    case IGNORE:      break;    default:      if (child instanceof QElement &&          XSLNS.equals(((QElement) child).getNamespaceURI()) &&	  _version != null && _version.equals("1.0"))	throw error(child, "unknown XSL element `" +		    child.getNodeName() + "'");      else {	Node subchild = child.getFirstChild();	boolean hasFallback = false;	for (; subchild != null; subchild = subchild.getNextSibling()) {	  String local = getXslLocal(subchild);	  if (local != null && local.equals("fallback")) {	    hasFallback = true;	    generateChildren(subchild);	  }	}	if (! hasFallback) // && child.getNamespace().equals(XSLNS))	  printError(L.l("expected xsl tag at `{0}'",                         child.getNodeName()));      }      break;    }  }  private void generateText(Node node)    throws Exception  {    String data = node.getNodeValue();    int length = data.length();    if (length == 0)      return;    int i = 0;    for (; i < length && XmlChar.isWhitespace(data.charAt(i)); i++) {    }    if (i == length && stripNode(node))      return;    if (data != null && data.length() > 0 && node instanceof QAbstractNode) {      setLocation(node);      writeText(data);    }  }  private boolean stripNode(Node node)  {    for (node = node.getParentNode();	 node != null;	 node = node.getParentNode()) {      if (node instanceof Element) {	Element elt = (Element) node;	String space = elt.getAttribute("xml:space");	if (! space.equals(""))	  return ! space.equals("preserve");      }    }

⌨️ 快捷键说明

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