1 28 29 package com.caucho.jsp.java; 30 31 import com.caucho.jsp.JspParseException; 32 import com.caucho.vfs.WriteStream; 33 import com.caucho.xml.QName; 34 35 import java.io.IOException ; 36 import java.util.ArrayList ; 37 import java.util.HashSet ; 38 39 42 public class JspXmlElement extends JspContainerNode { 43 private ArrayList <QName> _attrNames = new ArrayList <QName>(); 44 private ArrayList <String > _attrValues = new ArrayList <String >(); 45 46 49 public void addAttribute(QName name, String value) 50 throws JspParseException 51 { 52 _attrNames.add(name); 53 _attrValues.add(value); 54 } 55 56 59 public void addAttribute(QName name, JspAttribute value) 60 throws JspParseException 61 { 62 throw error(L.l("jsp:attribute '{0}' is not allowed as a child of an XML element.", 63 name.getName())); 64 } 65 66 69 public boolean hasNamespace(String prefix, String uri) 70 { 71 QName name = getQName(); 72 73 if (prefix == null || uri == null) 74 return true; 75 else if (prefix.equals(name.getPrefix()) && 76 uri.equals(name.getNamespaceURI())) 77 return true; 78 else 79 return _parent.hasNamespace(prefix, uri); 80 } 81 82 87 public void printXml(WriteStream os) 88 throws IOException 89 { 90 os.print("<" + getTagName()); 91 92 for (int i = 0; i < _attrNames.size(); i++) { 93 QName name = _attrNames.get(i); 94 String value = _attrValues.get(i); 95 96 os.print(" " + name.getName() + "=\""); 97 98 printXmlText(os, value); 99 100 os.print("\""); 101 } 102 103 os.print(">"); 104 105 printXmlChildren(os); 106 107 os.print("</" + getTagName() + ">"); 108 } 109 110 115 public void generate(JspJavaWriter out) 116 throws Exception 117 { 118 out.addText("<"); 119 out.addText(getTagName()); 120 121 QName qName = getQName(); 122 123 HashSet <String > prefixes = new HashSet <String >(); 124 125 if (qName.getNamespaceURI() != null && 126 ! _parent.hasNamespace(qName)) { 127 prefixes.add(qName.getPrefix()); 128 129 out.addText(" "); 130 if (qName.getPrefix() == null || qName.getPrefix().equals("")) 131 out.addText("xmlns=\""); 132 else 133 out.addText("xmlns:" + qName.getPrefix() + "=\""); 134 out.addText(qName.getNamespaceURI()); 135 out.addText("\""); 136 } 137 138 for (int i = 0; i < _attrNames.size(); i++) { 139 QName name = _attrNames.get(i); 140 String value = _attrValues.get(i); 141 142 if (name.getNamespaceURI() != null && 143 ! prefixes.contains(name.getPrefix()) && 144 ! _parent.hasNamespace(name)) { 145 prefixes.add(name.getPrefix()); 146 out.addText(" "); 147 if (name.getPrefix() == null || name.getPrefix().equals("")) 148 out.addText("xmlns=\""); 149 else 150 out.addText("xmlns:" + name.getPrefix() + "=\""); 151 out.addText(name.getNamespaceURI()); 152 out.addText("\""); 153 } 154 155 out.addText(" "); 156 out.addText(name.getName()); 157 158 if (value == null || value.equals("")) { 159 161 out.addText("=\"\""); 162 } 163 else { 164 out.addText("=\""); 165 166 if (value.indexOf("${") < 0 && 167 ! value.startsWith("<%") && 168 ! value.startsWith("%")) { 169 out.addText(value); 170 } 171 else { 172 String javaValue = generateParameterValue(String .class, value, 173 true, null); 174 out.println("out.print(" + javaValue + ");"); 175 } 176 out.addText("\""); 177 } 178 } 179 180 if (getChildren() != null && getChildren().size() > 0) { 181 out.addText(">"); 182 183 generateChildren(out); 184 185 out.addText("</" + getTagName() + ">"); 186 } 187 else 188 out.addText(" />"); 189 } 190 } 191 | Popular Tags |