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:
034: /**
035: * Represents any XSL node from the stylesheet.
036: */
037: public class XslAttributeNode extends XslNode {
038: private QName _name;
039: private String _value;
040:
041: public XslAttributeNode(QName name, String value) {
042: _name = name;
043: _value = value;
044: }
045:
046: /**
047: * Generates the code for the attribute
048: *
049: * @param out the output writer for the generated java.
050: */
051: public void generate(JavaWriter out) throws Exception {
052: // XXX: filename:line
053:
054: String namespace = _name.getNamespaceURI();
055: String prefix = _name.getPrefix();
056: String local = _name.getLocalName();
057: String name = _name.getName();
058:
059: String[] postPrefix = _gen.getNamespaceAlias(namespace);
060:
061: if (postPrefix != null) {
062: prefix = postPrefix[0];
063: namespace = postPrefix[1];
064: if (prefix == null || prefix.equals(""))
065: name = local;
066: else
067: name = prefix + ":" + local;
068: }
069: /*
070: if (_excludedNamespaces.get(namespace) != null)
071: namespace = null;
072: */
073:
074: if (name.equals("xmlns")) {
075: out.print("out.bindNamespace(null, \"");
076: out.printJavaString(_value);
077: out.println("\");");
078: } else if (name.startsWith("xmlns:")) {
079: out.print("out.bindNamespace(\"" + name.substring(6)
080: + "\", \"");
081: out.printJavaString(_value);
082: out.println("\");");
083: } else if (namespace != null && _value.indexOf('{') < 0) {
084: out.print("out.attribute(");
085: out.print(namespace == null ? "null"
086: : ("\"" + namespace + "\""));
087: out.print(prefix == null ? ", null"
088: : (", \"" + prefix + "\""));
089: out.print(local == null ? ", null"
090: : (", \"" + local + "\""));
091: out.print(name == null ? ", null" : (", \"" + name + "\""));
092: out.print(", ");
093: if (_value == null)
094: out.print("null");
095: else {
096: out.print("\"");
097: out.printJavaString(_value);
098: out.print("\"");
099: }
100: out.println(");");
101: } else if (namespace != null) {
102: out.print("out.attribute(");
103: out.print(namespace == null ? "null"
104: : ("\"" + namespace + "\""));
105: out.print(prefix == null ? ", null"
106: : (", \"" + prefix + "\""));
107: out.print(local == null ? ", null"
108: : (", \"" + local + "\""));
109: out.print(name == null ? ", null" : (", \"" + name + "\""));
110: out.print(",");
111: generateString(out, _value, '+');
112: out.println(");");
113: } else if (_value.indexOf('{') < 0) {
114: out.print("out.attribute(");
115: out.print(name == null ? "null" : ("\"" + name + "\""));
116: out.print(", ");
117: if (_value == null)
118: out.print("null");
119: else {
120: out.print("\"");
121: out.printJavaString(_value);
122: out.print("\"");
123: }
124: out.println(");");
125: } else {
126: out.print("out.attribute(");
127: out.print(name == null ? "null" : ("\"" + name + "\""));
128: out.print(", ");
129: generateString(out, _value, '+');
130: out.println(");");
131: }
132: }
133:
134: public String toString() {
135: return "XslAttributeNode[" + _name + "," + _value + "]";
136: }
137: }
|