1 23 24 package org.enhydra.xml.xmlc.metadata; 25 26 import java.lang.reflect.Array ; 27 import java.util.StringTokenizer ; 28 29 import org.enhydra.apache.xerces.dom.DocumentImpl; 30 import org.enhydra.apache.xerces.dom.ElementImpl; 31 import org.enhydra.xml.xmlc.XMLCException; 32 import org.w3c.dom.Attr ; 33 import org.w3c.dom.Document ; 34 import org.w3c.dom.Element ; 35 import org.w3c.dom.NamedNodeMap ; 36 import org.w3c.dom.Node ; 37 38 42 public class MetaDataElement extends ElementImpl { 43 46 private static final String [] emptyStringArray = new String [0]; 47 48 54 55 58 protected MetaDataElement(Document ownerDoc, 59 String name) { 60 super((DocumentImpl)ownerDoc, name); 61 } 62 63 66 public MetaDataDocument getDocument() { 67 return (MetaDataDocument)getOwnerDocument(); 68 } 69 70 73 public MetaData getMetaData() { 74 return getDocument().getMetaData(); 75 } 76 77 83 89 protected MetaDataElement getCreateChild(Class elementClass) { 90 Node child = getChild(elementClass); 91 if (child == null) { 92 child = getDocument().createElement(elementClass); 94 appendChild(child); 95 } 96 return (MetaDataElement)child; 97 } 98 99 106 protected MetaDataElement getChild(Class elementClass) { 107 for (Node child = getFirstChild(); child != null; 108 child = child.getNextSibling()) { 109 if (elementClass.isInstance(child)) { 110 return (MetaDataElement)child; 111 } 112 } 113 return null; 114 } 115 116 120 protected void setChild(MetaDataElement element) { 121 Node current = getChild(element.getClass()); 122 if (current == null) { 123 appendChild(element); 124 } else { 125 replaceChild(element, current); 126 } 127 } 128 129 134 protected void deleteChild(Class elementClass) { 135 Node current = getChild(elementClass.getClass()); 136 if (current != null) { 137 removeChild(current); 138 } 139 } 140 141 144 protected Node [] getChildren(Class elementClass) { 145 int numChildren = 0; 147 for (Node child = getFirstChild(); child != null; 148 child = child.getNextSibling()) { 149 if (elementClass.isInstance(child)) { 150 numChildren++; 151 } 152 } 153 154 Node [] children = (Node [])Array.newInstance(elementClass, numChildren); 156 int idx = 0; 157 for (Node child = getFirstChild(); 158 (child != null) && (idx < numChildren); 159 child = child.getNextSibling()) { 160 if (elementClass.isInstance(child)) { 161 children[idx++] = child; 162 } 163 } 164 return children; 165 } 166 167 168 171 protected boolean isAttributeSpecified(String attrName) { 172 Attr attr = getAttributeNode(attrName); 173 if (attr == null) { 174 return false; 175 } else { 176 return attr.getSpecified(); 177 } 178 } 179 180 181 188 protected Boolean getBooleanObjectAttribute(String attrName) { 189 String attrValue = getAttributeNull(attrName); 190 if (attrValue == null) { 191 return null; 192 } else { 193 if (attrValue.equalsIgnoreCase("true") 194 || attrValue.equalsIgnoreCase("yes")) { 195 return Boolean.TRUE; 196 } else { 197 return Boolean.FALSE; 198 } 199 } 200 } 201 202 209 protected void setBooleanObjectAttribute(String attrName, 210 Boolean value) { 211 if (value == null) { 212 removeAttribute(attrName); 213 } else { 214 setAttribute(attrName, value.toString()); 215 } 216 } 217 218 225 protected boolean getBooleanAttribute(String attrName, 226 boolean defaultValue) { 227 String attrValue = getAttributeNull(attrName); 228 if (attrValue == null) { 229 return defaultValue; 230 } else { 231 if (attrValue.equalsIgnoreCase("true") 232 || attrValue.equalsIgnoreCase("yes")) { 233 return true; 234 } else { 235 return false; 236 } 237 } 238 } 239 240 247 protected boolean getBooleanAttribute(String attrName) { 248 return getBooleanAttribute(attrName, false); 249 } 250 251 261 protected void setBooleanAttribute(String attrName, 262 boolean value, 263 boolean defaultValue) { 264 if (value == defaultValue) { 265 removeAttribute(attrName); 266 } else { 267 setAttribute(attrName, (value ? "true" : "false")); 268 } 269 } 270 271 278 protected void setBooleanAttribute(String attrName, 279 boolean value) { 280 setBooleanAttribute(attrName, value, false); 281 } 282 283 286 protected String getAttributeNull(String attrName) { 287 Attr attr = getAttributeNode(attrName); 288 if (attr == null) { 289 return null; 290 } else { 291 return attr.getValue(); 292 } 293 } 294 295 298 protected void setRemoveAttribute(String attrName, 299 String value) { 300 if (value == null) { 301 removeAttribute(attrName); 302 } else { 303 setAttribute(attrName, value); 304 } 305 } 306 307 310 protected String [] getStringArrayAttribute(String attrName) { 311 String value = getAttributeNull(attrName); 312 String [] values; 313 if (value == null) { 314 values = emptyStringArray; 315 } else { 316 StringTokenizer tokenizer = new StringTokenizer (value); 318 values = new String [tokenizer.countTokens()]; 319 for (int idx = 0; idx < values.length; idx++) { 320 values[idx] = tokenizer.nextToken(); 321 } 322 } 323 return values; 324 } 325 326 330 protected void setRemoveStringArrayAttribute(String attrName, 331 String [] values) { 332 if (values == null) { 333 removeAttribute(attrName); 334 } else { 335 StringBuffer value = new StringBuffer (); 337 for (int idx = 0; idx < values.length; idx++) { 338 if (idx > 0) { 339 value.append(' '); 340 } 341 value.append(values[idx]); 342 } 343 setAttribute(attrName, value.toString()); 344 } 345 } 346 347 350 protected void addStringArrayAttribute(String attrName, 351 String value) { 352 String currentValue = getAttributeNull(attrName); 353 if (currentValue == null) { 354 currentValue = ""; 355 } 356 StringBuffer currentBuf = new StringBuffer (currentValue); 357 if (currentBuf.length() > 0) { 358 currentBuf.append(' '); 359 } 360 currentBuf.append(value); 361 setAttribute(attrName, currentBuf.toString()); 362 } 363 364 372 protected void completeModifications() throws XMLCException { 373 for (Node child = getFirstChild(); 374 child != null; 375 child = child.getNextSibling()) { 376 if (child instanceof MetaDataElement) { 377 ((MetaDataElement)child).completeModifications(); 378 } 379 } 380 } 381 382 386 protected void mergeAttributes(Element srcElement) { 387 Document doc = getOwnerDocument(); 388 NamedNodeMap attrs = srcElement.getAttributes(); 389 int len = attrs.getLength(); 390 for (int idx = 0; idx < len; idx++) { 391 Attr attr = (Attr )attrs.item(idx); 392 if (attr.getSpecified()) { 393 setAttributeNode((Attr )doc.importNode(attr, true)); 394 } 395 } 396 } 397 398 402 protected void mergeSingletonChild(Class childClass, 403 MetaDataElement srcElement) { 404 MetaDataElement srcChild = srcElement.getChild(childClass); 405 if (srcChild != null) { 406 MetaDataElement destChild = getChild(childClass); 407 if (destChild != null) { 408 destChild.mergeElement(srcChild); 409 } else { 410 appendChild(getOwnerDocument().importNode(srcChild, true)); 411 } 412 } 413 } 414 415 422 protected void mergeElement(MetaDataElement srcElement) { 423 mergeAttributes(srcElement); 424 425 Document document = getOwnerDocument(); 426 for (Node child = srcElement.getFirstChild(); 427 child != null; 428 child = child.getNextSibling()) { 429 appendChild(document.importNode(child, true)); 430 } 431 } 432 } 433 | Popular Tags |