📄 schemagenerator.java
字号:
// now see if the mapping can extend base complex type boolean extend = false; for (int i = 0; i < childs.size(); i++) { ElementBase child = (ElementBase)childs.get(i); if (child instanceof StructureElement) { StructureElement struct = (StructureElement)child; if (struct.getMapAsMapping() == extended) { if (struct.getName() == null) { extend = true; } } } if (child instanceof IComponent) { } } } } */ } else { group = addChildElement(type, "all"); } // handle all nested elements of container m_structureStack.push(container); defineList(childs, group, type, container.type() == ElementBase.COLLECTION_ELEMENT); m_structureStack.pop(); return type; } /** * Generate a schema from a binding using supplied classpaths. If the schema * for the binding namespace (or default namespace) already exists the * definitions from this binding are added to the existing schema; otherwise * a new schema is created and added to the collection defined. * * @param binding root element of binding */ private void generateSchema(BindingElement binding) { // process each mapping definition for binding m_structureStack.push(binding); ArrayList tops = binding.topChildren(); for (int i = 0; i < tops.size(); i++) { ElementBase top = (ElementBase)tops.get(i); if (top.type() == ElementBase.MAPPING_ELEMENT) { // find or create schema for mapped class MappingElement mapping = (MappingElement)top; String uri = mapping.getNamespace().getUri(); Element schema = (Element)m_schemaMap.get(uri); if (schema == null) { // build new schema element for this namespace schema = m_document.createElementNS(XSD_URI, "schema"); if (uri != null) { schema.setAttribute("targetNamespace", uri); } m_schemaMap.put(uri, schema); // set qualification attributes if needed if (m_isElementQualified) { schema.setAttribute("elementFormDefault", "qualified"); } if (m_isAttributeQualified) { schema.setAttribute("attributeFormDefault", "qualified"); } // add namespace declarations to element if (uri != null) { schema.setAttributeNS(XMLNS_URI, "xmlns:tns", uri); } schema.setAttributeNS(XMLNS_URI, "xmlns:xsd", XSD_URI); schema.setAttributeNS(XMLNS_URI, "xmlns", XSD_URI); // add spacing for first child node indentForClose(schema); } // add spacer and comment before actual definition indentForClose(schema); String cname = mapping.getClassName(); addComment(schema, " Created from mapping for class " + cname + " "); if (mapping.isAbstract()) { // add mapping as global type in binding Element type = defineNestedStructure(mapping, schema); type.setAttribute("name", simpleClassName(cname)); } else { // add mapping as global element in binding Element element = addChildElement(schema, "element"); element.setAttribute("name", mapping.getName()); // check type of mapping definition if (mapping.getMarshaller() != null || mapping.getUnmarshaller() != null) { // use "any" for custom marshaller/unmarshaller Element type = addChildElement(element, "complexType"); Element seq = addChildElement(type, "sequence"); addComment(seq, " Replace \"any\" with details of " + "content to complete schema "); addChildElement(seq, "any"); } else { // use complex type for embedded definition defineNestedStructure(mapping, element); } } } } m_structureStack.pop(); } /** * Process a binding definition for schema generation. This first validates * the binding definition, and if it is valid then handles schema generation * from the binding. * * @param binding root element of binding * @exception JiBXException if error in generating the schema */ public void generate(BindingElement binding) throws JiBXException { // validate the binding definition ValidationContext vctx = new ValidationContext(m_classLocator); binding.runValidation(vctx); boolean usable = true; if (vctx.getProblems().size() > 0) { // report problems found System.err.println("Problems found in binding " + binding.getName()); ArrayList probs = vctx.getProblems(); for (int i = 0; i < probs.size(); i++) { ValidationProblem prob = (ValidationProblem)probs.get(i); System.err.println(prob.getDescription()); if (prob.getSeverity() > ValidationProblem.WARNING_LEVEL) { usable = false; } } } // check if binding usable for schema generation if (usable) { generateSchema(binding); } else { System.err.println ("Binding validation errors prevent schema generation"); System.exit(1); } } /** * Get simple class name. * * @param cname class name with full package specification * @return class name only */ private String simpleClassName(String cname) { int split = cname.lastIndexOf('.'); if (split >= 0) { cname = cname.substring(split+1); } return cname; } /** * Main method for running compiler as application. * * @param args command line arguments */ public static void main(String[] args) { if (args.length > 0) { try { // check for various flags set boolean verbose = false; boolean edflt = true; boolean adflt = false; ArrayList paths = new ArrayList(); int offset = 0; for (; offset < args.length; offset++) { String arg = args[offset]; if ("-v".equalsIgnoreCase(arg)) { verbose = true; } else if ("-e".equalsIgnoreCase(arg)) { edflt = false; } else if ("-a".equalsIgnoreCase(arg)) { adflt = true; } else if ("-p".equalsIgnoreCase(arg)) { paths.add(args[++offset]); } else { break; } } // set up path and binding lists String[] vmpaths = Utility.getClassPaths(); for (int i = 0; i < vmpaths.length; i++) { paths.add(vmpaths[i]); } ArrayList bindings = new ArrayList(); for (int i = offset; i < args.length; i++) { bindings.add(args[i]); } // report on the configuration System.out.println("Running schema generator version " + CURRENT_VERSION); if (verbose) { System.out.println("Using paths:"); for (int i = 0; i < paths.size(); i++) { System.out.println(" " + paths.get(i)); } System.out.println("Using input bindings:"); for (int i = 0; i < bindings.size(); i++) { System.out.println(" " + bindings.get(i)); } } // process all specified binding files SchemaGenerator schemagen = new SchemaGenerator(verbose, edflt, adflt, paths); for (int i = 0; i < bindings.size(); i++) { // read binding from file String bpath = (String)bindings.get(i); String name = Utility.convertName(Utility.fileName(bpath)); File file = new File(bpath); BindingElement binding = Utility.validateBinding(name, new URL("file://" + file.getAbsolutePath()), new FileInputStream(file)); // convert the binding to a schema if (binding != null) { schemagen.generate(binding); } } // output each schema to separate file Element[] schemas = schemagen.getSchemas(); for (int i = 0; i < schemas.length; i++) { // get base name for output file from last part of namespace Element schema = schemas[i]; String tns = schema.getAttribute("targetNamespace"); String name = tns; if (name.length() == 0) { // fix this to relate back to binding name = (String)bindings.get(0); int split = name.lastIndexOf('.'); if (split >= 0) { name = name.substring(0, split); } } else { int split = name.lastIndexOf('/'); if (split >= 0) { name = name.substring(split+1); } } try { // write schema to output file name += ".xsd"; FileOutputStream out = new FileOutputStream(name); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty("indent", "no"); DOMSource source = new DOMSource(schema); Result result = new StreamResult(out); transformer.transform(source, result); out.close(); System.out.print("Wrote schema " + name); if (tns.length() == 0) { System.out.println(" for default namespace"); } else { System.out.println(" for namespace " + tns); } } catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (TransformerFactoryConfigurationError e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } catch (JiBXException ex) { ex.printStackTrace(); System.exit(1); } catch (FileNotFoundException e) { e.printStackTrace(); System.exit(2); } catch (MalformedURLException e) { e.printStackTrace(); System.exit(3); } } else { System.out.println ("\nUsage: java org.jibx.binding.SchemaGenerator [-v] [-e]" + " [-a] [-p path]* binding1 binding2 ...\nwhere:" + "\n -v turns on verbose output," + "\n -e sets elementFormDefault=\"false\" for the schemas," + "\n -a sets attributeFormDefault=\"true\" for the schemas, " + "and\n -p gives a path component for looking up the classes " + "referenced in the binding\nThe binding# files are " + "different bindings to be used for schema generation.\n"); System.exit(1); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -