@Test public void testGetElementJavaTypeFromInclude() { try { java.net.URL url=ClassLoader.getSystemClassLoader().getResource("models/requirements/PQM.bpmn"); java.io.File base=new java.io.File(url.getFile()); DefaultResourceLocator locator=new DefaultResourceLocator(base.getParentFile()); String javaType=JavaGeneratorUtil.getElementJavaType( new QName("http: "../schema/policyQuote.xsd", locator); if (javaType == null) { fail("No java type returned"); } if (!javaType.equals("org.example.policyquote.PolicyQuote")) { fail("Java type should be 'org.example.policyquote.PolicyQuote', but got: "+javaType); } } catch (Exception e) { fail("Failed: "+e); } }
public static String getElementJavaType(QName element, String location, ResourceLocator locator) { String ret=null; try { java.net.URI uri=locator.getResourceURI(location); DocumentBuilderFactory fact=DocumentBuilderFactory.newInstance(); fact.setNamespaceAware(true); DocumentBuilder builder=fact.newDocumentBuilder(); java.io.InputStream is=uri.toURL().openStream(); org.w3c.dom.Document doc=builder.parse(is); is.close(); org.w3c.dom.NodeList elemList= doc.getDocumentElement().getElementsByTagNameNS( "http: for (int i=0; i < elemList.getLength(); i++) { org.w3c.dom.Element elem=(org.w3c.dom.Element)elemList.item(i); String name=elem.getAttribute("name"); String elemType=elem.getAttribute("type"); if (!elem.hasAttribute("type")) { elemType = name; if (elemType.length() > 0) { elemType = Character.toUpperCase(elemType.charAt(0))+elemType.substring(1); } } if (name.equals(element.getLocalPart())) { String prefix=org.savara.common.util.XMLUtils.getPrefix(elemType); String ns=null; if (prefix != null && prefix.trim().length() > 0) { ns = elem.lookupNamespaceURI(prefix); } else { ns = elem.getOwnerDocument().getDocumentElement().getAttribute("targetNamespace"); } ret = getJavaPackage(ns); ret += "."+JavaGeneratorUtil.getJavaClassName( org.savara.common.util.XMLUtils.getLocalname(elemType)); } } if (ret == null) { org.w3c.dom.NodeList includeList= doc.getDocumentElement().getElementsByTagNameNS( "http: for (int i=0; ret == null && i < includeList.getLength(); i++) { org.w3c.dom.Element elem=(org.w3c.dom.Element)includeList.item(i); String schemaLocation=elem.getAttribute("schemaLocation"); java.io.File f=new java.io.File(location); if (f.getParentFile() != null) { schemaLocation = f.getParentFile().getPath()+java.io.File.separator+schemaLocation; } ret = getElementJavaType(element, schemaLocation, locator); } } } catch(Exception e) { LOG.log(Level.SEVERE, "Failed to obtain XSD schema '"+location+"'", e); } return (ret); }
JavaGeneratorUtil { public static String getElementJavaType(QName element, String location, ResourceLocator locator) { String ret=null; try { java.net.URI uri=locator.getResourceURI(location); DocumentBuilderFactory fact=DocumentBuilderFactory.newInstance(); fact.setNamespaceAware(true); DocumentBuilder builder=fact.newDocumentBuilder(); java.io.InputStream is=uri.toURL().openStream(); org.w3c.dom.Document doc=builder.parse(is); is.close(); org.w3c.dom.NodeList elemList= doc.getDocumentElement().getElementsByTagNameNS( "http: for (int i=0; i < elemList.getLength(); i++) { org.w3c.dom.Element elem=(org.w3c.dom.Element)elemList.item(i); String name=elem.getAttribute("name"); String elemType=elem.getAttribute("type"); if (!elem.hasAttribute("type")) { elemType = name; if (elemType.length() > 0) { elemType = Character.toUpperCase(elemType.charAt(0))+elemType.substring(1); } } if (name.equals(element.getLocalPart())) { String prefix=org.savara.common.util.XMLUtils.getPrefix(elemType); String ns=null; if (prefix != null && prefix.trim().length() > 0) { ns = elem.lookupNamespaceURI(prefix); } else { ns = elem.getOwnerDocument().getDocumentElement().getAttribute("targetNamespace"); } ret = getJavaPackage(ns); ret += "."+JavaGeneratorUtil.getJavaClassName( org.savara.common.util.XMLUtils.getLocalname(elemType)); } } if (ret == null) { org.w3c.dom.NodeList includeList= doc.getDocumentElement().getElementsByTagNameNS( "http: for (int i=0; ret == null && i < includeList.getLength(); i++) { org.w3c.dom.Element elem=(org.w3c.dom.Element)includeList.item(i); String schemaLocation=elem.getAttribute("schemaLocation"); java.io.File f=new java.io.File(location); if (f.getParentFile() != null) { schemaLocation = f.getParentFile().getPath()+java.io.File.separator+schemaLocation; } ret = getElementJavaType(element, schemaLocation, locator); } } } catch(Exception e) { LOG.log(Level.SEVERE, "Failed to obtain XSD schema '"+location+"'", e); } return (ret); } }
JavaGeneratorUtil { public static String getElementJavaType(QName element, String location, ResourceLocator locator) { String ret=null; try { java.net.URI uri=locator.getResourceURI(location); DocumentBuilderFactory fact=DocumentBuilderFactory.newInstance(); fact.setNamespaceAware(true); DocumentBuilder builder=fact.newDocumentBuilder(); java.io.InputStream is=uri.toURL().openStream(); org.w3c.dom.Document doc=builder.parse(is); is.close(); org.w3c.dom.NodeList elemList= doc.getDocumentElement().getElementsByTagNameNS( "http: for (int i=0; i < elemList.getLength(); i++) { org.w3c.dom.Element elem=(org.w3c.dom.Element)elemList.item(i); String name=elem.getAttribute("name"); String elemType=elem.getAttribute("type"); if (!elem.hasAttribute("type")) { elemType = name; if (elemType.length() > 0) { elemType = Character.toUpperCase(elemType.charAt(0))+elemType.substring(1); } } if (name.equals(element.getLocalPart())) { String prefix=org.savara.common.util.XMLUtils.getPrefix(elemType); String ns=null; if (prefix != null && prefix.trim().length() > 0) { ns = elem.lookupNamespaceURI(prefix); } else { ns = elem.getOwnerDocument().getDocumentElement().getAttribute("targetNamespace"); } ret = getJavaPackage(ns); ret += "."+JavaGeneratorUtil.getJavaClassName( org.savara.common.util.XMLUtils.getLocalname(elemType)); } } if (ret == null) { org.w3c.dom.NodeList includeList= doc.getDocumentElement().getElementsByTagNameNS( "http: for (int i=0; ret == null && i < includeList.getLength(); i++) { org.w3c.dom.Element elem=(org.w3c.dom.Element)includeList.item(i); String schemaLocation=elem.getAttribute("schemaLocation"); java.io.File f=new java.io.File(location); if (f.getParentFile() != null) { schemaLocation = f.getParentFile().getPath()+java.io.File.separator+schemaLocation; } ret = getElementJavaType(element, schemaLocation, locator); } } } catch(Exception e) { LOG.log(Level.SEVERE, "Failed to obtain XSD schema '"+location+"'", e); } return (ret); }  }
JavaGeneratorUtil { public static String getElementJavaType(QName element, String location, ResourceLocator locator) { String ret=null; try { java.net.URI uri=locator.getResourceURI(location); DocumentBuilderFactory fact=DocumentBuilderFactory.newInstance(); fact.setNamespaceAware(true); DocumentBuilder builder=fact.newDocumentBuilder(); java.io.InputStream is=uri.toURL().openStream(); org.w3c.dom.Document doc=builder.parse(is); is.close(); org.w3c.dom.NodeList elemList= doc.getDocumentElement().getElementsByTagNameNS( "http: for (int i=0; i < elemList.getLength(); i++) { org.w3c.dom.Element elem=(org.w3c.dom.Element)elemList.item(i); String name=elem.getAttribute("name"); String elemType=elem.getAttribute("type"); if (!elem.hasAttribute("type")) { elemType = name; if (elemType.length() > 0) { elemType = Character.toUpperCase(elemType.charAt(0))+elemType.substring(1); } } if (name.equals(element.getLocalPart())) { String prefix=org.savara.common.util.XMLUtils.getPrefix(elemType); String ns=null; if (prefix != null && prefix.trim().length() > 0) { ns = elem.lookupNamespaceURI(prefix); } else { ns = elem.getOwnerDocument().getDocumentElement().getAttribute("targetNamespace"); } ret = getJavaPackage(ns); ret += "."+JavaGeneratorUtil.getJavaClassName( org.savara.common.util.XMLUtils.getLocalname(elemType)); } } if (ret == null) { org.w3c.dom.NodeList includeList= doc.getDocumentElement().getElementsByTagNameNS( "http: for (int i=0; ret == null && i < includeList.getLength(); i++) { org.w3c.dom.Element elem=(org.w3c.dom.Element)includeList.item(i); String schemaLocation=elem.getAttribute("schemaLocation"); java.io.File f=new java.io.File(location); if (f.getParentFile() != null) { schemaLocation = f.getParentFile().getPath()+java.io.File.separator+schemaLocation; } ret = getElementJavaType(element, schemaLocation, locator); } } } catch(Exception e) { LOG.log(Level.SEVERE, "Failed to obtain XSD schema '"+location+"'", e); } return (ret); }  static String getElementJavaType(QName element, String location, ResourceLocator locator); static String getJavaPackage(String namespace); static String getJavaClassName(String name); }
JavaGeneratorUtil { public static String getElementJavaType(QName element, String location, ResourceLocator locator) { String ret=null; try { java.net.URI uri=locator.getResourceURI(location); DocumentBuilderFactory fact=DocumentBuilderFactory.newInstance(); fact.setNamespaceAware(true); DocumentBuilder builder=fact.newDocumentBuilder(); java.io.InputStream is=uri.toURL().openStream(); org.w3c.dom.Document doc=builder.parse(is); is.close(); org.w3c.dom.NodeList elemList= doc.getDocumentElement().getElementsByTagNameNS( "http: for (int i=0; i < elemList.getLength(); i++) { org.w3c.dom.Element elem=(org.w3c.dom.Element)elemList.item(i); String name=elem.getAttribute("name"); String elemType=elem.getAttribute("type"); if (!elem.hasAttribute("type")) { elemType = name; if (elemType.length() > 0) { elemType = Character.toUpperCase(elemType.charAt(0))+elemType.substring(1); } } if (name.equals(element.getLocalPart())) { String prefix=org.savara.common.util.XMLUtils.getPrefix(elemType); String ns=null; if (prefix != null && prefix.trim().length() > 0) { ns = elem.lookupNamespaceURI(prefix); } else { ns = elem.getOwnerDocument().getDocumentElement().getAttribute("targetNamespace"); } ret = getJavaPackage(ns); ret += "."+JavaGeneratorUtil.getJavaClassName( org.savara.common.util.XMLUtils.getLocalname(elemType)); } } if (ret == null) { org.w3c.dom.NodeList includeList= doc.getDocumentElement().getElementsByTagNameNS( "http: for (int i=0; ret == null && i < includeList.getLength(); i++) { org.w3c.dom.Element elem=(org.w3c.dom.Element)includeList.item(i); String schemaLocation=elem.getAttribute("schemaLocation"); java.io.File f=new java.io.File(location); if (f.getParentFile() != null) { schemaLocation = f.getParentFile().getPath()+java.io.File.separator+schemaLocation; } ret = getElementJavaType(element, schemaLocation, locator); } } } catch(Exception e) { LOG.log(Level.SEVERE, "Failed to obtain XSD schema '"+location+"'", e); } return (ret); }  static String getElementJavaType(QName element, String location, ResourceLocator locator); static String getJavaPackage(String namespace); static String getJavaClassName(String name);  }