1 13 package org.ccil.cowan.tagsoup; 14 15 21 22 public class ElementType { 23 24 private String theName; private String theNamespace; private String theLocalName; private int theModel; private int theMemberOf; private int theFlags; private AttributesImpl theAtts; private ElementType theParent; private Schema theSchema; 34 48 49 public ElementType(String name, int model, int memberOf, int flags, Schema schema) { 50 theName = name; 51 theModel = model; 52 theMemberOf = memberOf; 53 theFlags = flags; 54 theAtts = new AttributesImpl(); 55 theSchema = schema; 56 theNamespace = namespace(name, false); 57 theLocalName = localName(name); 58 } 59 60 68 public String namespace(String name, boolean attribute) { 69 int colon = name.indexOf(':'); 70 if (colon == -1) { 71 return attribute ? "" : theSchema.getURI(); 72 } 73 String prefix = name.substring(0, colon); 74 if (prefix.equals("xml")) { 75 return "http://www.w3.org/XML/1998/namespace"; 76 } 77 else { 78 return ("urn:x-prefix:" + prefix).intern(); 79 } 80 } 81 82 87 public String localName(String name) { 88 int colon = name.indexOf(':'); 89 if (colon == -1) { 90 return name; 91 } 92 else { 93 return name.substring(colon+1).intern(); 94 } 95 } 96 97 101 102 public String name() { return theName; } 103 104 108 109 public String namespace() { return theNamespace; } 110 111 115 116 public String localName() { return theLocalName; } 117 118 122 123 public int model() { return theModel; } 124 125 130 131 public int memberOf() { return theMemberOf; } 132 133 137 138 public int flags() { return theFlags; } 139 140 148 149 public AttributesImpl atts() {return theAtts;} 150 151 155 156 public ElementType parent() {return theParent;} 157 158 162 163 public Schema schema() {return theSchema;} 164 165 166 173 174 public boolean canContain(ElementType other) { 175 return (theModel & other.theMemberOf) != 0; 176 } 177 178 179 187 188 public void setAttribute(AttributesImpl atts, String name, String type, String value) { 189 if (name.equals("xmlns") || name.startsWith("xmlns:")) { 190 return; 191 } 192 ; 193 String namespace = namespace(name, true); 194 String localName = localName(name); 195 int i = atts.getIndex(name); 196 if (i == -1) { 197 name = name.intern(); 198 if (type == null) type = "CDATA"; 199 if (!type.equals("CDATA")) value = normalize(value); 200 atts.addAttribute(namespace, localName, name, type, value); 201 } 202 else { 203 if (type == null) type = atts.getType(i); 204 if (!type.equals("CDATA")) value=normalize(value); 205 atts.setAttribute(i, namespace, localName, name, type, value); 206 } 207 } 208 209 215 public static String normalize(String value) { 216 if (value == null) return value; 217 value = value.trim(); 218 if (value.indexOf(" ") == -1) return value; 219 boolean space = false; 220 int len = value.length(); 221 StringBuffer b = new StringBuffer (len); 222 for (int i = 0; i < len; i++) { 223 char v = value.charAt(i); 224 if (v == ' ') { 225 if (!space) b.append(v); 226 space = true; 227 } 228 else { 229 b.append(v); 230 space = false; 231 } 232 } 233 return b.toString(); 234 } 235 236 242 243 public void setAttribute(String name, String type, String value) { 244 setAttribute(theAtts, name, type, value); 245 } 246 247 251 252 public void setModel(int model) { theModel = model; } 253 254 258 259 public void setMemberOf(int memberOf) { theMemberOf = memberOf; } 260 261 265 266 public void setFlags(int flags) { theFlags = flags; } 267 268 272 273 public void setParent(ElementType parent) { theParent = parent; } 274 275 } 276 | Popular Tags |