1 28 29 package com.caucho.xml2; 30 31 import org.w3c.dom.Node ; 32 33 import javax.xml.namespace.QName ; 34 import java.io.IOException ; 35 import java.util.ArrayList ; 36 37 class QElementDef extends QNode { 38 String _name; 39 Object _content; 40 ArrayList <QAttributeDef> _attr; 41 boolean _hasDefault; 42 QDocumentType _dtd; 43 44 QElementDef(String name) 45 { 46 _name = name; 47 } 48 49 public String getNodeName() { return "#element"; } 50 public String getTagName() { return "#element"; } 51 public short getNodeType() { return Node.ELEMENT_NODE; } 52 53 Node importNode(QDocument owner, boolean deep) 54 { 55 QElementDef def = new QElementDef(_name); 56 57 return def; 58 } 59 60 public void addAttribute(String name, String type, ArrayList enumeration, 61 String qualifier, String deflt) 62 { 63 if (_attr == null) 64 _attr = new ArrayList <QAttributeDef>(); 65 66 if (deflt != null) { 67 _hasDefault = true; 68 _dtd.setAttributeDefaults(); 69 } 70 71 _attr.add(new QAttributeDef(name, type, enumeration, qualifier, deflt)); 72 } 73 74 void fillDefaults(QElement element) 75 { 76 if (! _hasDefault) 77 return; 78 79 for (int i = 0; i < _attr.size(); i++) { 80 QAttributeDef attrDef = _attr.get(i); 81 if (attrDef._deflt != null && 82 element.getAttribute(attrDef._name).equals("")) { 83 QAttr attr = (QAttr) element._owner.createAttribute(attrDef._name, 84 attrDef._deflt); 85 attr._owner = element._owner; 86 attr.setSpecified(false); 87 element.setAttributeNode(attr); 88 } 89 } 90 } 91 92 void fillDefaults(QAttributes attributes) 93 { 94 if (! _hasDefault) 95 return; 96 97 for (int i = 0; i < _attr.size(); i++) { 98 QAttributeDef attrDef = _attr.get(i); 99 if (attrDef._deflt != null && 100 attributes.getIndex(attrDef._name) < 0) { 101 attributes.add(new QName (null, attrDef._name, null), attrDef._deflt); 102 } 103 } 104 } 105 106 public void print(XmlPrinter os) throws IOException 107 { 108 if (_content != null) { 109 os.print("<!ELEMENT "); 110 os.print(_name); 111 os.print(" "); 112 if (_content instanceof QContentParticle) 113 ((QContentParticle) _content).print(os); 114 else 115 os.print(String.valueOf(_content)); 116 os.println(">"); 117 } 118 119 if (_attr != null) { 120 os.print("<!ATTLIST "); 121 os.print(_name); 122 123 for (int i = 0; i < _attr.size(); i++) { 124 QAttributeDef attribute = _attr.get(i); 125 126 if (_attr.size() == 1) 127 os.print(" "); 128 else 129 os.print("\n "); 130 os.print(attribute._name); 131 if (attribute._type.equals("#ENUM")) { 132 os.print(" ("); 133 for (int j = 0; j < attribute._enumeration.size(); j++) { 134 String enumType = attribute._enumeration.get(j); 135 136 if (j != 0) 137 os.print(" | "); 138 os.print(enumType); 139 } 140 os.print(")"); 141 } else if (attribute._type.equals("NOTATION")) { 142 os.print(" NOTATION ("); 143 for (int j = 0; j < attribute._enumeration.size(); j++) { 144 String enumType = attribute._enumeration.get(j); 145 146 if (j != 0) 147 os.print(" | "); 148 os.print(enumType); 149 } 150 os.print(")"); 151 } else { 152 os.print(" "); 153 os.print(attribute._type); 154 } 155 156 if (attribute._qualifier != null) { 157 os.print(" "); 158 os.print(attribute._qualifier); 159 } 160 if (attribute._deflt != null) { 161 os.print(" \""); 162 os.print(attribute._deflt); 163 os.print("\""); 164 } 165 } 166 os.println(">"); 167 } 168 } 169 } 170 | Popular Tags |