1 19 20 package org.netbeans.modules.schema2beans; 21 22 import java.util.*; 23 import java.io.*; 24 25 import java.lang.reflect.*; 26 27 import org.w3c.dom.*; 28 29 30 50 public class DDFactory extends Object { 51 static HashMap beanClassMap = new HashMap(); 52 53 static int idCount = 0; 54 55 62 public static BaseBean create(InputStream in, String rootName) throws Schema2BeansException { 63 Document doc = null; 64 String docType; 65 BaseBean beanNode = null; 66 Class bean; 67 68 if (DDLogFlags.debug) { 69 TraceLogger.put(TraceLogger.DEBUG, TraceLogger.SVC_DD, 70 DDLogFlags.DBG_BLD, 1, 71 DDLogFlags.DDCREATE); 72 } 73 74 doc = GraphManager.createXmlDocument(in, false); 75 76 if (DDLogFlags.debug) { 77 String str = XmlToString(doc, 999); 78 TraceLogger.put(TraceLogger.DEBUG, TraceLogger.SVC_DD, 79 DDLogFlags.DBG_BLD, 20, 80 DDLogFlags.DDCREATED, 81 str ); 82 } 83 84 synchronized (beanClassMap) { 85 bean = (Class )beanClassMap.get(rootName); 86 } 87 88 if (bean == null) 89 throw new Schema2BeansException(Common.getMessage( 90 "CantCreateBeanForRootElement_msg", rootName)); 91 92 93 Constructor c = null; 94 95 try { 96 Class [] cc = new Class [] {org.w3c.dom.Node .class, 97 int.class}; 98 c = bean.getDeclaredConstructor(cc); 99 } 100 catch(NoSuchMethodException me) { 101 throw new Schema2BeansNestedException(Common.getMessage( 102 "CantGetConstructor_msg"), me); 103 } 104 105 Object [] p = new Object [] {doc, new Integer (Common.NO_DEFAULT_VALUES)}; 106 107 try { 108 beanNode = (BaseBean)c.newInstance(p); 109 } 110 catch(Exception e) { 111 TraceLogger.error(e); 112 throw new Schema2BeansNestedException(Common.getMessage( 113 "CantInstanciateBeanClass_msg"), e); 114 } 115 116 if (DDLogFlags.debug) { 117 TraceLogger.put(TraceLogger.DEBUG, TraceLogger.SVC_DD, 118 DDLogFlags.DBG_BLD, 1, 119 DDLogFlags.DDBEANED, 120 "Created bean graph node for " + rootName ); 121 } 122 123 return beanNode; 124 } 125 126 137 static public void register(String name, String className) 138 throws ClassNotFoundException { 139 Class c = null; 140 try { 141 c = Class.forName(className); 142 } catch(ClassNotFoundException e) { 143 c = name.getClass().forName(className); 145 } 146 DDFactory.register(name, c); 147 } 148 149 159 static public void register(String name, Class clazz) { 160 synchronized (beanClassMap) { 161 beanClassMap.put(name, clazz); 162 } 163 } 164 165 171 static synchronized int getUniqueId() { 172 return DDFactory.idCount++; 173 } 174 175 176 static String typeToString(short type) { 177 switch(type) { 178 case Node.ATTRIBUTE_NODE: return "attr"; case Node.CDATA_SECTION_NODE: return "cdata"; case Node.COMMENT_NODE : return "comment"; case Node.DOCUMENT_FRAGMENT_NODE: return "doc_fragment"; case Node.DOCUMENT_NODE: return "doc"; case Node.DOCUMENT_TYPE_NODE: return "doc_type"; case Node.ELEMENT_NODE: return "element"; case Node.ENTITY_NODE: return "entity"; case Node.ENTITY_REFERENCE_NODE: return "entity_ref"; case Node.NOTATION_NODE: return "notation"; case Node.PROCESSING_INSTRUCTION_NODE: return "processing_instr"; case Node.TEXT_NODE: return "text"; default: return "type:" + type; } 192 } 193 194 195 198 static public String XmlToString(Node n) { 199 return XmlToString(n, 9999, null); 200 } 201 202 206 static public String XmlToString(Node n, int depth) { 207 return XmlToString(n, depth, null); 208 } 209 210 211 216 static public String XmlToString(Node n, int depth, String filter) { 217 StringBuffer str = new StringBuffer (); 218 nodeToString("", str, n, depth, filter, true); return str.toString(); 220 } 221 222 static void nodeToString(String indent, StringBuffer str, Node n, 224 int depth, String filter, boolean root) { 225 226 if (root) 227 nodeChildrenToString(indent, str, n, depth, filter); 229 else { 230 for (;n != null; n = n.getNextSibling()) { 231 nodeChildrenToString(indent, str, n, depth, filter); 232 } 233 } 234 } 235 236 237 static void nodeChildrenToString(String indent, StringBuffer str, 239 Node n, int depth, String filter) { 240 241 if ((filter == null) || n.getNodeName().equals(filter)) { 242 String tmp = indent + n.getNodeName(); 243 String value = n.getNodeValue(); 244 short type = n.getNodeType(); 245 246 if (value == null) 247 value = ""; 251 if (!value.equals("")) tmp += "=" + value; 254 tmp += " - " + typeToString(type); 256 if ((type != Node.TEXT_NODE && type != Node.CDATA_SECTION_NODE) || (!value.trim().equals(""))) { 258 str.append(tmp); 259 str.append("\n"); } 261 262 NamedNodeMap map = n.getAttributes(); 263 if (map != null && map.getLength() != 0) { 264 List attrNames = new ArrayList(map.getLength()); 265 for (int i=0; i<map.getLength(); i++) { 266 Attr a = (Attr)map.item(i); 267 attrNames.add(a.getName()); 268 } 269 Collections.sort(attrNames); 270 for (Iterator it = attrNames.iterator(); it.hasNext(); ) { 271 Attr a = (Attr) map.getNamedItem((String )it.next()); 272 str.append(indent); 273 str.append("attribute: "); str.append(a.getName()); 275 str.append("="); str.append(a.getValue()); 277 str.append("\n"); } 279 } 280 } 281 282 if (n.getFirstChild() != null && (depth > 0) ) 283 nodeToString(indent + " ", str, n.getFirstChild(), depth-1, filter, false); 285 } 286 } 287 288 | Popular Tags |