schemaloader.java
来自「联合国农粮署牵头开发的geonetwork源代码最新版」· Java 代码 · 共 759 行 · 第 1/2 页
JAVA
759 行
Integer numberRecursed = recursivelyDealWithAbstractElements(mdt,(ArrayList) hmSubsGrp.get(ee.name)); number = number + numberRecursed; } else { number++; mdt.addElementWithType(ee.name, ee.type, ee.min, ee.max); } } return number; } //--------------------------------------------------------------------------- //--- //--- PHASE 1 : Schema loading //--- //--------------------------------------------------------------------------- /** Loads the xml-schema file, removes annotations and resolve imports/includes */ private ArrayList loadFile(String xmlSchemaFile, HashSet loadedFiles) throws Exception { loadedFiles.add(new File(xmlSchemaFile).getCanonicalPath()); String path = new File(xmlSchemaFile).getParent() + "/"; //--- load xml-schema elRoot = Xml.loadFile(xmlSchemaFile); // change target namespace String oldtargetNS = targetNS; String oldtargetNSPrefix = targetNSPrefix; targetNS = elRoot.getAttributeValue("targetNamespace"); targetNSPrefix = null; if (targetNS != null) { for (Iterator i = elRoot.getAdditionalNamespaces().iterator(); i.hasNext(); ) { Namespace ns = (Namespace)i.next(); if (targetNS.equals(ns.getURI())) { targetNSPrefix = ns.getPrefix(); break; } } if ("".equals(targetNSPrefix)) targetNSPrefix = null; } List children = elRoot.getChildren(); //--- collect elements into an array because we have to add elements //--- when we encounter the "import" element ArrayList alElementFiles = new ArrayList(); for(int i=0; i<children.size(); i++) { Element elChild = (Element) children.get(i); String name = elChild.getName(); if (name.equals("annotation")) ; else if (name.equals("import") || name.equals("include")) { String schemaLoc = elChild.getAttributeValue("schemaLocation"); //--- we must prevent imports from the web if (schemaLoc.startsWith("http:")) { int lastSlash = schemaLoc.lastIndexOf("/"); schemaLoc = schemaLoc.substring(lastSlash +1); } if (!loadedFiles.contains(new File(path + schemaLoc).getCanonicalPath())) alElementFiles.addAll(loadFile(path + schemaLoc, loadedFiles)); } else alElementFiles.add(new ElementInfo(elChild, xmlSchemaFile, targetNS, targetNSPrefix)); } // restore target namespace targetNS = oldtargetNS; targetNSPrefix = oldtargetNSPrefix; return alElementFiles; } //--------------------------------------------------------------------------- //--- //--- PHASE 2 : Parse elements building intermediate data structures //--- //--------------------------------------------------------------------------- private void parseElements(ArrayList alElementFiles) throws JDOMException { //--- clear some structures hmElements .clear(); hmTypes .clear(); hmAttrGrp .clear(); hmAbsElems .clear(); hmSubsGrp .clear(); hmSubsLink .clear(); hmElemRestr.clear(); hmTypeRestr.clear(); hmAttribs .clear(); hmGroups .clear(); for(int i=0; i<alElementFiles.size(); i++) { ElementInfo ei = (ElementInfo) alElementFiles.get(i); Element elChild = ei.element; String name = elChild.getName(); if (name.equals("element")) buildGlobalElement(ei); else if (name.equals("complexType")) buildComplexType(ei); else if (name.equals("simpleType")) buildSimpleType(ei); else if (name.equals("attribute")) buildGlobalAttrib(ei); else if (name.equals("group")) buildGlobalGroup(ei); else if (name.equals("attributeGroup")) buildAttributeGroup(ei); else Logger.log("Unknown global element : " + elChild.getName(), ei); } } //--------------------------------------------------------------------------- private void buildGlobalElement(ElementInfo ei) { ElementEntry ee = new ElementEntry(ei); if (ee.name == null) throw new IllegalArgumentException("Name is null for element : " + ee.name); if (ee.substGroup != null) { ArrayList al = (ArrayList) hmSubsGrp.get(ee.substGroup); if (al == null) { al = new ArrayList(); hmSubsGrp.put(ee.substGroup, al); } al.add(ee); ArrayList alLink = (ArrayList) hmSubsLink.get(ee.name); if (alLink == null) { alLink = new ArrayList(); hmSubsLink.put(ee.name,alLink); } alLink.add(ee.substGroup); } if (ee.abstrElem) { if (hmAbsElems.containsKey(ee.name)) throw new IllegalArgumentException("Namespace collision for : " + ee.name); hmAbsElems.put(ee.name, ee.type); return; } if (ee.complexType != null) { String type = ee.name+"#I"; ee.complexType.name = type; ee.type = type; if (hmElements.containsKey(ee.name)) throw new IllegalArgumentException("Namespace collision for : " + ee.name); hmElements.put(ee.name, type); hmTypes.put(type, ee.complexType); } else if (ee.simpleType != null) { if (ee.type == null) throw new IllegalArgumentException("Type is null for element : " + ee.name); if (hmElements.containsKey(ee.name)) throw new IllegalArgumentException("Namespace collision for : " + ee.name); hmElements .put(ee.name, ee.type); hmElemRestr.put(ee.name, ee.simpleType.alEnum); } else { hmElements.put(ee.name, ee.type); } } //--------------------------------------------------------------------------- private void buildComplexType(ElementInfo ei) { ComplexTypeEntry ct = new ComplexTypeEntry(ei); if (hmTypes.containsKey(ct.name)) throw new IllegalArgumentException("Namespace collision for : " + ct.name); hmTypes.put(ct.name, ct); } //--------------------------------------------------------------------------- private void buildSimpleType(ElementInfo ei) { SimpleTypeEntry st = new SimpleTypeEntry(ei); if (hmTypeRestr.containsKey(st.name)) throw new IllegalArgumentException("Namespace collision for : " + st.name); hmTypeRestr.put(st.name, st.alEnum); } //--------------------------------------------------------------------------- private void buildGlobalAttrib(ElementInfo ei) { AttributeEntry at = new AttributeEntry(ei); if (hmAttribs.containsKey(at.name)) throw new IllegalArgumentException("Namespace collision for : " + at.name); hmAttribs.put(at.name, at); } //--------------------------------------------------------------------------- private void buildGlobalGroup(ElementInfo ei) { GroupEntry ge = new GroupEntry(ei); if (hmGroups.containsKey(ge.name)) throw new IllegalArgumentException("Namespace collision for : " + ge.name); hmGroups.put(ge.name, ge); } //--------------------------------------------------------------------------- private void buildAttributeGroup(ElementInfo ei) { String name = ei.element.getAttributeValue("name"); if (ei.targetNSPrefix != null) name = ei.targetNSPrefix + ":" + name; ArrayList al = (ArrayList) hmAttrGrp.get(name); if (al == null) { al = new ArrayList(); hmAttrGrp.put(name, al); } List children = ei.element.getChildren(); for(int i=0; i<children.size(); i++) { Element elChild = (Element) children.get(i); if (elChild.getName().equals("attribute")) al.add(new AttributeEntry(elChild, ei.file, ei.targetNS, ei.targetNSPrefix)); else Logger.log("Unknown child in 'attributeGroup' : " + elChild.getName(), ei); } } //--------------------------------------------------------------------------- //--- //--- Add in attributes from complexType that come from base type (if any) //--- //--------------------------------------------------------------------------- private ArrayList resolveAttributeInheritance(ComplexTypeEntry cte) { if (cte.complexContent == null) return cte.alAttribs; String baseType = cte.complexContent.base; ComplexTypeEntry baseCTE = (ComplexTypeEntry) hmTypes.get(baseType); if (baseCTE == null) throw new IllegalArgumentException("Base type not found for : " + baseType); ArrayList result = new ArrayList(resolveAttributeInheritance(baseCTE)); ArrayList al = cte.complexContent.alAttribs; for(int i=0; i<al.size(); i++) result.add(al.get(i)); return result; } //--------------------------------------------------------------------------- //--- //--- Add in elements to complexType that come from base type (if any) //--- //--------------------------------------------------------------------------- private ArrayList resolveInheritance(ComplexTypeEntry cte) { if (cte.complexContent == null) return cte.alElements; String baseType = cte.complexContent.base; ComplexTypeEntry baseCTE = (ComplexTypeEntry) hmTypes.get(baseType); if (baseCTE == null) throw new IllegalArgumentException("Base type not found for : " + baseType); ArrayList result = new ArrayList(resolveInheritance(baseCTE)); ArrayList al = cte.complexContent.alElements; for(int i=0; i<al.size(); i++) result.add(al.get(i)); return result; } //--------------------------------------------------------------------------- private MetadataAttribute buildMetadataAttrib(AttributeEntry ae) { String name = ae.name; String ref = ae.reference; Boolean overRequired = ae.required; if (ref != null) { ae = (AttributeEntry) hmAttribs.get(ref); if (ae == null) throw new IllegalArgumentException("Reference '"+ref+"' not found for attrib : " +name); } MetadataAttribute ma = new MetadataAttribute(); ma.name = ae.name; ma.defValue = ae.defValue; ma.required = overRequired; for(int k=0; k<ae.alValues.size(); k++) ma.values.add(ae.alValues.get(k)); return ma; }}//==============================================================================class ElementInfo{ public Element element; public String file; public String targetNS; public String targetNSPrefix; //--------------------------------------------------------------------------- public ElementInfo(Element e, String f, String tns, String tnsp) { element = e; file = f; targetNS = tns; targetNSPrefix = tnsp; }}//==============================================================================
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?