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