1 19 38 39 package org.netbeans.modules.schema2beansdev.beangraph; 40 41 public class BeanGraph implements org.netbeans.modules.schema2beansdev.beangraph.CommonBean { 42 public static final String SCHEMA_TYPE_MAPPING = "SchemaTypeMapping"; 44 private java.util.List _SchemaTypeMapping = new java.util.ArrayList (); private java.lang.String schemaLocation; 46 47 50 public BeanGraph() { 51 } 52 53 56 public BeanGraph(org.netbeans.modules.schema2beansdev.beangraph.BeanGraph source) { 57 this(source, false); 58 } 59 60 64 public BeanGraph(org.netbeans.modules.schema2beansdev.beangraph.BeanGraph source, boolean justData) { 65 for (java.util.Iterator it = source._SchemaTypeMapping.iterator(); 66 it.hasNext(); ) { 67 org.netbeans.modules.schema2beansdev.beangraph.SchemaTypeMappingType srcElement = (org.netbeans.modules.schema2beansdev.beangraph.SchemaTypeMappingType)it.next(); 68 _SchemaTypeMapping.add((srcElement == null) ? null : newSchemaTypeMappingType(srcElement, justData)); 69 } 70 schemaLocation = source.schemaLocation; 71 } 72 73 public void setSchemaTypeMapping(org.netbeans.modules.schema2beansdev.beangraph.SchemaTypeMappingType[] value) { 75 if (value == null) 76 value = new SchemaTypeMappingType[0]; 77 _SchemaTypeMapping.clear(); 78 ((java.util.ArrayList ) _SchemaTypeMapping).ensureCapacity(value.length); 79 for (int i = 0; i < value.length; ++i) { 80 _SchemaTypeMapping.add(value[i]); 81 } 82 } 83 84 public void setSchemaTypeMapping(int index, org.netbeans.modules.schema2beansdev.beangraph.SchemaTypeMappingType value) { 85 _SchemaTypeMapping.set(index, value); 86 } 87 88 public org.netbeans.modules.schema2beansdev.beangraph.SchemaTypeMappingType[] getSchemaTypeMapping() { 89 SchemaTypeMappingType[] arr = new SchemaTypeMappingType[_SchemaTypeMapping.size()]; 90 return (SchemaTypeMappingType[]) _SchemaTypeMapping.toArray(arr); 91 } 92 93 public java.util.List fetchSchemaTypeMappingList() { 94 return _SchemaTypeMapping; 95 } 96 97 public org.netbeans.modules.schema2beansdev.beangraph.SchemaTypeMappingType getSchemaTypeMapping(int index) { 98 return (SchemaTypeMappingType)_SchemaTypeMapping.get(index); 99 } 100 101 public int sizeSchemaTypeMapping() { 103 return _SchemaTypeMapping.size(); 104 } 105 106 public int addSchemaTypeMapping(org.netbeans.modules.schema2beansdev.beangraph.SchemaTypeMappingType value) { 107 _SchemaTypeMapping.add(value); 108 int positionOfNewItem = _SchemaTypeMapping.size()-1; 109 return positionOfNewItem; 110 } 111 112 115 public int removeSchemaTypeMapping(org.netbeans.modules.schema2beansdev.beangraph.SchemaTypeMappingType value) { 116 int pos = _SchemaTypeMapping.indexOf(value); 117 if (pos >= 0) { 118 _SchemaTypeMapping.remove(pos); 119 } 120 return pos; 121 } 122 123 public void _setSchemaLocation(String location) { 124 schemaLocation = location; 125 } 126 127 public String _getSchemaLocation() { 128 return schemaLocation; 129 } 130 131 135 public org.netbeans.modules.schema2beansdev.beangraph.SchemaTypeMappingType newSchemaTypeMappingType() { 136 return new org.netbeans.modules.schema2beansdev.beangraph.SchemaTypeMappingType(); 137 } 138 139 143 public org.netbeans.modules.schema2beansdev.beangraph.SchemaTypeMappingType newSchemaTypeMappingType(SchemaTypeMappingType source, boolean justData) { 144 return new org.netbeans.modules.schema2beansdev.beangraph.SchemaTypeMappingType(source, justData); 145 } 146 147 public void write(java.io.File f) throws java.io.IOException { 148 java.io.OutputStream out = new java.io.FileOutputStream (f); 149 try { 150 write(out); 151 } finally { 152 out.close(); 153 } 154 } 155 156 public void write(java.io.OutputStream out) throws java.io.IOException { 157 write(out, null); 158 } 159 160 public void write(java.io.OutputStream out, String encoding) throws java.io.IOException { 161 java.io.Writer w; 162 if (encoding == null) { 163 encoding = "UTF-8"; } 165 w = new java.io.BufferedWriter (new java.io.OutputStreamWriter (out, encoding)); 166 write(w, encoding); 167 w.flush(); 168 } 169 170 174 public void write(java.io.Writer out, String encoding) throws java.io.IOException { 175 out.write("<?xml version='1.0'"); if (encoding != null) 177 out.write(" encoding='"+encoding+"'"); out.write(" ?>\n"); writeNode(out, "bean-graph", ""); } 181 182 public void writeNode(java.io.Writer out) throws java.io.IOException { 183 String myName; 184 myName = "bean-graph"; 185 writeNode(out, myName, ""); } 187 188 public void writeNode(java.io.Writer out, String nodeName, String indent) throws java.io.IOException { 189 writeNode(out, nodeName, null, indent, new java.util.HashMap ()); 190 } 191 192 195 public void writeNode(java.io.Writer out, String nodeName, String namespace, String indent, java.util.Map namespaceMap) throws java.io.IOException { 196 out.write(indent); 197 out.write("<"); 198 if (namespace != null) { 199 out.write((String )namespaceMap.get(namespace)); 200 out.write(":"); 201 } 202 out.write(nodeName); 203 if (schemaLocation != null) { 204 namespaceMap.put("http://www.w3.org/2001/XMLSchema-instance", "xsi"); 205 out.write(" xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='"); 206 out.write(schemaLocation); 207 out.write("'"); } 209 out.write(">\n"); 210 String nextIndent = indent + " "; 211 for (java.util.Iterator it = _SchemaTypeMapping.iterator(); 212 it.hasNext(); ) { 213 org.netbeans.modules.schema2beansdev.beangraph.SchemaTypeMappingType element = (org.netbeans.modules.schema2beansdev.beangraph.SchemaTypeMappingType)it.next(); 214 if (element != null) { 215 element.writeNode(out, "schema-type-mapping", null, nextIndent, namespaceMap); 216 } 217 } 218 out.write(indent); 219 out.write("</"); 220 if (namespace != null) { 221 out.write((String )namespaceMap.get(namespace)); 222 out.write(":"); 223 } 224 out.write(nodeName); 225 out.write(">\n"); 226 } 227 228 public static BeanGraph read(java.io.File f) throws javax.xml.parsers.ParserConfigurationException , org.xml.sax.SAXException , java.io.IOException { 229 java.io.InputStream in = new java.io.FileInputStream (f); 230 try { 231 return read(in); 232 } finally { 233 in.close(); 234 } 235 } 236 237 public static BeanGraph read(java.io.InputStream in) throws javax.xml.parsers.ParserConfigurationException , org.xml.sax.SAXException , java.io.IOException { 238 return read(new org.xml.sax.InputSource (in), false, null, null); 239 } 240 241 247 public static BeanGraph readNoEntityResolver(java.io.InputStream in) throws javax.xml.parsers.ParserConfigurationException , org.xml.sax.SAXException , java.io.IOException { 248 return read(new org.xml.sax.InputSource (in), false, 249 new org.xml.sax.EntityResolver () { 250 public org.xml.sax.InputSource resolveEntity(String publicId, String systemId) { 251 java.io.ByteArrayInputStream bin = new java.io.ByteArrayInputStream (new byte[0]); 252 return new org.xml.sax.InputSource (bin); 253 } 254 } 255 , null); 256 } 257 258 public static BeanGraph read(org.xml.sax.InputSource in, boolean validate, org.xml.sax.EntityResolver er, org.xml.sax.ErrorHandler eh) throws javax.xml.parsers.ParserConfigurationException , org.xml.sax.SAXException , java.io.IOException { 259 javax.xml.parsers.DocumentBuilderFactory dbf = javax.xml.parsers.DocumentBuilderFactory.newInstance(); 260 dbf.setValidating(validate); 261 dbf.setNamespaceAware(true); 262 javax.xml.parsers.DocumentBuilder db = dbf.newDocumentBuilder(); 263 if (er != null) db.setEntityResolver(er); 264 if (eh != null) db.setErrorHandler(eh); 265 org.w3c.dom.Document doc = db.parse(in); 266 return read(doc); 267 } 268 269 public static BeanGraph read(org.w3c.dom.Document document) { 270 BeanGraph aBeanGraph = new BeanGraph(); 271 aBeanGraph.readFromDocument(document); 272 return aBeanGraph; 273 } 274 275 protected void readFromDocument(org.w3c.dom.Document document) { 276 readNode(document.getDocumentElement()); 277 } 278 279 public void readNode(org.w3c.dom.Node node) { 280 readNode(node, new java.util.HashMap ()); 281 } 282 283 public void readNode(org.w3c.dom.Node node, java.util.Map namespacePrefixes) { 284 if (node.hasAttributes()) { 285 org.w3c.dom.NamedNodeMap attrs = node.getAttributes(); 286 org.w3c.dom.Attr attr; 287 java.lang.String attrValue; 288 boolean firstNamespaceDef = true; 289 for (int attrNum = 0; attrNum < attrs.getLength(); ++attrNum) { 290 attr = (org.w3c.dom.Attr ) attrs.item(attrNum); 291 String attrName = attr.getName(); 292 if (attrName.startsWith("xmlns:")) { 293 if (firstNamespaceDef) { 294 firstNamespaceDef = false; 295 namespacePrefixes = new java.util.HashMap (namespacePrefixes); 297 } 298 String attrNSPrefix = attrName.substring(6, attrName.length()); 299 namespacePrefixes.put(attrNSPrefix, attr.getValue()); 300 } 301 } 302 String xsiPrefix = "xsi"; 303 for (java.util.Iterator it = namespacePrefixes.keySet().iterator(); 304 it.hasNext(); ) { 305 String prefix = (String ) it.next(); 306 String ns = (String ) namespacePrefixes.get(prefix); 307 if ("http://www.w3.org/2001/XMLSchema-instance".equals(ns)) { 308 xsiPrefix = prefix; 309 break; 310 } 311 } 312 attr = (org.w3c.dom.Attr ) attrs.getNamedItem(""+xsiPrefix+":schemaLocation"); 313 if (attr != null) { 314 attrValue = attr.getValue(); 315 schemaLocation = attrValue; 316 } 317 } 318 org.w3c.dom.NodeList children = node.getChildNodes(); 319 for (int i = 0, size = children.getLength(); i < size; ++i) { 320 org.w3c.dom.Node childNode = children.item(i); 321 String childNodeName = (childNode.getLocalName() == null ? childNode.getNodeName().intern() : childNode.getLocalName().intern()); 322 String childNodeValue = ""; 323 if (childNode.getFirstChild() != null) { 324 childNodeValue = childNode.getFirstChild().getNodeValue(); 325 } 326 if (childNodeName == "schema-type-mapping") { 327 SchemaTypeMappingType aSchemaTypeMapping = newSchemaTypeMappingType(); 328 aSchemaTypeMapping.readNode(childNode, namespacePrefixes); 329 _SchemaTypeMapping.add(aSchemaTypeMapping); 330 } 331 else { 332 } 334 } 335 } 336 337 341 public static void writeXML(java.io.Writer out, String msg) throws java.io.IOException { 342 writeXML(out, msg, true); 343 } 344 345 public static void writeXML(java.io.Writer out, String msg, boolean attribute) throws java.io.IOException { 346 if (msg == null) 347 return; 348 int msgLength = msg.length(); 349 for (int i = 0; i < msgLength; ++i) { 350 char c = msg.charAt(i); 351 writeXML(out, c, attribute); 352 } 353 } 354 355 public static void writeXML(java.io.Writer out, char msg, boolean attribute) throws java.io.IOException { 356 if (msg == '&') 357 out.write("&"); 358 else if (msg == '<') 359 out.write("<"); 360 else if (msg == '>') 361 out.write(">"); 362 else if (attribute) { 363 if (msg == '"') 364 out.write("""); 365 else if (msg == '\'') 366 out.write("'"); 367 else if (msg == '\n') 368 out.write("
"); 369 else if (msg == '\t') 370 out.write("	"); 371 else 372 out.write(msg); 373 } 374 else 375 out.write(msg); 376 } 377 378 public static class ValidateException extends Exception { 379 private org.netbeans.modules.schema2beansdev.beangraph.CommonBean failedBean; 380 private String failedPropertyName; 381 private FailureType failureType; 382 public ValidateException(String msg, String failedPropertyName, org.netbeans.modules.schema2beansdev.beangraph.CommonBean failedBean) { 383 super(msg); 384 this.failedBean = failedBean; 385 this.failedPropertyName = failedPropertyName; 386 } 387 public ValidateException(String msg, FailureType ft, String failedPropertyName, org.netbeans.modules.schema2beansdev.beangraph.CommonBean failedBean) { 388 super(msg); 389 this.failureType = ft; 390 this.failedBean = failedBean; 391 this.failedPropertyName = failedPropertyName; 392 } 393 public String getFailedPropertyName() {return failedPropertyName;} 394 public FailureType getFailureType() {return failureType;} 395 public org.netbeans.modules.schema2beansdev.beangraph.CommonBean getFailedBean() {return failedBean;} 396 public static class FailureType { 397 private final String name; 398 private FailureType(String name) {this.name = name;} 399 public String toString() { return name;} 400 public static final FailureType NULL_VALUE = new FailureType("NULL_VALUE"); 401 public static final FailureType DATA_RESTRICTION = new FailureType("DATA_RESTRICTION"); 402 public static final FailureType ENUM_RESTRICTION = new FailureType("ENUM_RESTRICTION"); 403 public static final FailureType ALL_RESTRICTIONS = new FailureType("ALL_RESTRICTIONS"); 404 public static final FailureType MUTUALLY_EXCLUSIVE = new FailureType("MUTUALLY_EXCLUSIVE"); 405 } 406 } 407 408 public void validate() throws org.netbeans.modules.schema2beansdev.beangraph.BeanGraph.ValidateException { 409 boolean restrictionFailure = false; 410 boolean restrictionPassed = false; 411 for (int _index = 0; _index < sizeSchemaTypeMapping(); ++_index) { 413 org.netbeans.modules.schema2beansdev.beangraph.SchemaTypeMappingType element = getSchemaTypeMapping(_index); 414 if (element != null) { 415 element.validate(); 416 } 417 } 418 } 419 420 public void changePropertyByName(String name, Object value) { 421 if (name == null) return; 422 name = name.intern(); 423 if (name == "schemaTypeMapping") 424 addSchemaTypeMapping((SchemaTypeMappingType)value); 425 else if (name == "schemaTypeMapping[]") 426 setSchemaTypeMapping((SchemaTypeMappingType[]) value); 427 else 428 throw new IllegalArgumentException (name+" is not a valid property name for BeanGraph"); 429 } 430 431 public Object fetchPropertyByName(String name) { 432 if (name == "schemaTypeMapping[]") 433 return getSchemaTypeMapping(); 434 throw new IllegalArgumentException (name+" is not a valid property name for BeanGraph"); 435 } 436 437 public String nameSelf() { 438 return "/BeanGraph"; 439 } 440 441 public String nameChild(Object childObj) { 442 return nameChild(childObj, false, false); 443 } 444 445 450 public String nameChild(Object childObj, boolean returnConstName, boolean returnSchemaName) { 451 return nameChild(childObj, returnConstName, returnSchemaName, false); 452 } 453 454 459 public String nameChild(Object childObj, boolean returnConstName, boolean returnSchemaName, boolean returnXPathName) { 460 if (childObj instanceof SchemaTypeMappingType) { 461 SchemaTypeMappingType child = (SchemaTypeMappingType) childObj; 462 int index = 0; 463 for (java.util.Iterator it = _SchemaTypeMapping.iterator(); 464 it.hasNext(); ) { 465 org.netbeans.modules.schema2beansdev.beangraph.SchemaTypeMappingType element = (org.netbeans.modules.schema2beansdev.beangraph.SchemaTypeMappingType)it.next(); 466 if (child == element) { 467 if (returnConstName) { 468 return SCHEMA_TYPE_MAPPING; 469 } else if (returnSchemaName) { 470 return "schema-type-mapping"; 471 } else if (returnXPathName) { 472 return "schema-type-mapping[position()="+index+"]"; 473 } else { 474 return "SchemaTypeMapping."+Integer.toHexString(index); 475 } 476 } 477 ++index; 478 } 479 } 480 return null; 481 } 482 483 486 public org.netbeans.modules.schema2beansdev.beangraph.CommonBean[] childBeans(boolean recursive) { 487 java.util.List children = new java.util.LinkedList (); 488 childBeans(recursive, children); 489 org.netbeans.modules.schema2beansdev.beangraph.CommonBean[] result = new org.netbeans.modules.schema2beansdev.beangraph.CommonBean[children.size()]; 490 return (org.netbeans.modules.schema2beansdev.beangraph.CommonBean[]) children.toArray(result); 491 } 492 493 496 public void childBeans(boolean recursive, java.util.List beans) { 497 for (java.util.Iterator it = _SchemaTypeMapping.iterator(); 498 it.hasNext(); ) { 499 org.netbeans.modules.schema2beansdev.beangraph.SchemaTypeMappingType element = (org.netbeans.modules.schema2beansdev.beangraph.SchemaTypeMappingType)it.next(); 500 if (element != null) { 501 if (recursive) { 502 element.childBeans(true, beans); 503 } 504 beans.add(element); 505 } 506 } 507 } 508 509 public boolean equals(Object o) { 510 return o instanceof org.netbeans.modules.schema2beansdev.beangraph.BeanGraph && equals((org.netbeans.modules.schema2beansdev.beangraph.BeanGraph) o); 511 } 512 513 public boolean equals(org.netbeans.modules.schema2beansdev.beangraph.BeanGraph inst) { 514 if (inst == this) { 515 return true; 516 } 517 if (inst == null) { 518 return false; 519 } 520 if (sizeSchemaTypeMapping() != inst.sizeSchemaTypeMapping()) 521 return false; 522 for (java.util.Iterator it = _SchemaTypeMapping.iterator(), it2 = inst._SchemaTypeMapping.iterator(); 524 it.hasNext() && it2.hasNext(); ) { 525 org.netbeans.modules.schema2beansdev.beangraph.SchemaTypeMappingType element = (org.netbeans.modules.schema2beansdev.beangraph.SchemaTypeMappingType)it.next(); 526 org.netbeans.modules.schema2beansdev.beangraph.SchemaTypeMappingType element2 = (org.netbeans.modules.schema2beansdev.beangraph.SchemaTypeMappingType)it2.next(); 527 if (!(element == null ? element2 == null : element.equals(element2))) { 528 return false; 529 } 530 } 531 return true; 532 } 533 534 public int hashCode() { 535 int result = 17; 536 result = 37*result + (_SchemaTypeMapping == null ? 0 : _SchemaTypeMapping.hashCode()); 537 return result; 538 } 539 540 public String toString() { 541 java.io.StringWriter sw = new java.io.StringWriter (); 542 try { 543 writeNode(sw); 544 } catch (java.io.IOException e) { 545 throw new RuntimeException (e); 547 } 548 return sw.toString(); 549 } 550 551 } 552 553 554 599 | Popular Tags |