public void writeXSD(Model m) throws IOException { try { for (MainInfo main : m.getMains()) { List<Element> sources = new ArrayList<Element>(); sources.add(main.getElement()); sources.addAll(main.getInterceptorElements()); FileObject o = processingEnv .getFiler() .createResource( StandardLocation.CLASS_OUTPUT, main.getAnnotation().outputPackage(), main.getAnnotation().outputName(), sources.toArray(new Element[0])); BufferedWriter bw = new BufferedWriter(o.openWriter()); try { assembleXSD(bw, m, main); } finally { bw.close(); } } } catch (FilerException e) { if (e.getMessage().contains("Source file already created")) return; throw e; } }
private void assembleXSD(Writer w, Model m, MainInfo main) throws IOException, ProcessingException { String namespace = main.getAnnotation().targetNamespace(); w.append( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" + "<xsd:schema xmlns=\"" + namespace + "\"\r\n" + " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:beans=\"http://www.springframework.org/schema/beans\"\r\n" + " targetNamespace=\"" + namespace + "\"\r\n" + " elementFormDefault=\"qualified\" attributeFormDefault=\"unqualified\">\r\n" + "\r\n" + "<!-- Automatically generated by " + Schemas.class.getName() + ". -->\r\n" + "\r\n" + "<xsd:import namespace=\"http://www.springframework.org/schema/beans\" schemaLocation=\"http://www.springframework.org/schema/beans/spring-beans-3.1.xsd\" />\r\n" + "\r\n" + "<xsd:simpleType name=\"spel_number\">\r\n" + " <xsd:restriction base=\"xsd:string\">\r\n" + " <xsd:pattern value=\"-?[0-9]+|\\#\\{.*\\}\"></xsd:pattern>\r\n" + " </xsd:restriction>\r\n" + "</xsd:simpleType>\r\n" + "\r\n" + "<xsd:simpleType name=\"spel_boolean\">\r\n" + " <xsd:restriction base=\"xsd:string\">\r\n" + " <xsd:pattern value=\"[01]|true|false|\\#\\{.*\\}\"></xsd:pattern>\r\n" + " </xsd:restriction>\r\n" + "</xsd:simpleType>\r\n\r\n"); assembleDeclarations(w, m, main); w.append("</xsd:schema>"); }
private void assembleElementInfo(Writer w, Model m, MainInfo main, ElementInfo i) throws IOException { w.append("<xsd:sequence>\r\n"); for (ChildElementInfo cei : i.getCeis()) { w.append( "<xsd:choice" + (cei.isRequired() ? " minOccurs=\"1\"" : " minOccurs=\"0\"") + (cei.isList() ? " maxOccurs=\"unbounded\"" : "") + ">\r\n"); assembleDocumentation(w, i); for (ElementInfo ei : main.getChildElementDeclarations().get(cei.getTypeDeclaration()).getElementInfo()) { if (ei.getAnnotation().topLevel()) w.append("<xsd:element ref=\"" + ei.getAnnotation().name() + "\">\r\n"); else w.append( "<xsd:element name=\"" + ei.getAnnotation().name() + "\" type=\"" + ei.getXSDTypeName(m) + "\">\r\n"); assembleDocumentation(w, ei); w.append("</xsd:element>\r\n"); } if (cei.getAnnotation().allowForeign()) w.append("<xsd:any namespace=\"##other\" processContents=\"strict\" />\r\n"); w.append("</xsd:choice>\r\n"); } w.append("</xsd:sequence>\r\n"); for (AttributeInfo ai : i.getAis()) if (!ai.getXMLName().equals("id")) assembleAttributeDeclaration(w, ai); if (i.getOai() != null) { w.append("<xsd:anyAttribute processContents=\"skip\">\r\n"); assembleDocumentation(w, i.getOai()); w.append("</xsd:anyAttribute>\r\n"); } }
private void assembleDeclarations(Writer w, Model m, MainInfo main) throws ProcessingException, IOException { for (ElementInfo i : main.getElements().values()) assembleElementDeclaration(w, m, main, i); }