001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.xsl.java;
030:
031: import com.caucho.java.JavaWriter;
032: import com.caucho.xml.QName;
033: import com.caucho.xsl.XslParseException;
034:
035: import java.util.ArrayList;
036:
037: /**
038: * Represents any XSL node from the stylesheet.
039: */
040: public class XslAttribute extends XslNode {
041: private String _name;
042: private String _namespace;
043:
044: /**
045: * Returns the attribute name.
046: */
047: public String getName() {
048: return _name;
049: }
050:
051: /**
052: * Returns the attribute value.
053: */
054: public String getValue() {
055: ArrayList<XslNode> children = getChildren();
056:
057: if (children == null || children.size() == 0)
058: return "";
059:
060: XslNode node = children.get(0);
061:
062: if (node instanceof TextNode)
063: return ((TextNode) node).getText();
064: else
065: return "";
066: }
067:
068: /**
069: * Adds an attribute.
070: */
071: public void addAttribute(QName name, String value)
072: throws XslParseException {
073: if (name.getName().equals("name"))
074: _name = value;
075: else if (name.getName().equals("namespace"))
076: _namespace = value;
077: else
078: super .addAttribute(name, value);
079: }
080:
081: /**
082: * Ends the attributes.
083: */
084: public void endAttributes() throws XslParseException {
085: if (_name == null)
086: throw error(L.l("xsl:attribute needs a 'name' attribute."));
087: }
088:
089: /**
090: * Generates the code for the tag
091: *
092: * @param out the output writer for the generated java.
093: */
094: public void generate(JavaWriter out) throws Exception {
095: if (hasChildren() || _namespace != null) {
096: String var = "_xsl_attr_" + generateId();
097:
098: out.print("XMLWriter " + var + " = ");
099:
100: if (_namespace != null) {
101: out.print("out.pushAttributeNs(");
102:
103: generateString(out, _name, '+');
104:
105: out.print(", ");
106: generateString(out, _namespace, '+');
107:
108: out.println(");");
109: } else {
110: out.print("out.pushAttribute(");
111:
112: generateString(out, _name, '+');
113:
114: if (getOutputNamespace() != null) {
115: out.print(", ");
116: printNamespace(out, getOutputNamespace());
117: }
118:
119: out.println(");");
120: }
121:
122: generateChildren(out);
123:
124: out.println("out.popAttribute(" + var + ");");
125: } else {
126: out.print("out.setAttribute(");
127:
128: generateString(out, _name, '+');
129:
130: if (getOutputNamespace() != null) {
131: out.print(", ");
132: printNamespace(out, getOutputNamespace());
133: }
134:
135: out.println(", \"\");");
136: }
137: }
138: }
|