⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 schemaloader.java

📁 由国外的一个著名的geonetwork修改而来
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
		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));		}		// RGFIX: 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);				System.out.println("# building global element " + (ee.name != null ? ee.name : ("ref --> " + ee.ref))); // RGFIX				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);						System.out.println("# - storing " + ee.name + " for substitution group " + ee.substGroup); // RGFIX						hmSubsLink.put(ee.name, al);		}		if (ee.abstrElem)		{			if (hmAbsElems.containsKey(ee.name))				throw new IllegalArgumentException("Namespace collision for : " + ee.name);			System.out.println("# - found abstract element " + (ee.name != null ? ee.name : ("ref --> " + ee.ref))); // RGFIX						hmAbsElems.put(ee.name, ee.type);						return; //RGFIX		}		if (ee.complexType != null)		{			String type = ee.name+"#I";			ee.complexType.name = type;			ee.type = type; // RGFIX			System.out.println("# - found complex type " + type); // RGFIX						if (hmElements.containsKey(ee.name))				throw new IllegalArgumentException("Namespace collision for : " + ee.name);			hmElements.put(ee.name, type);						System.out.println("# - stored type '" + type + "' for element '" + ee.name + "'"); // RGFIX						hmTypes.put(type, ee.complexType);		}		else if (ee.simpleType != null) // RGFIX: was just else		{			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);			System.out.println("# - found simple type " + ee.type); // RGFIX						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);		System.out.println("# building complex type " + ct.name); // RGFIX				if (hmTypes.containsKey(ct.name))			throw new IllegalArgumentException("Namespace collision for : " + ct.name);		System.out.println("# - registering complex type " + ct.name); // RGFIX				hmTypes.put(ct.name, ct);				System.out.println("# - read complex type " + hmTypes.get(ct.name)); // RGFIX	}	//---------------------------------------------------------------------------	private void buildSimpleType(ElementInfo ei)	{		SimpleTypeEntry  st = new SimpleTypeEntry(ei);		System.out.println("# building simple type " + st.name); // RGFIX				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);		System.out.println("# building global attribute " + at.name); // RGFIX				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);		System.out.println("# building global group " + ge.name); // RGFIX				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;		System.out.println("# building attribute group " + name); // RGFIX				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);		}	}	//---------------------------------------------------------------------------	//---	//---	//---	//---------------------------------------------------------------------------	private ArrayList resolveInheritance(ComplexTypeEntry cte, int n)	{		if (cte.complexContent == null)			return cte.alElements;		String baseType = cte.complexContent.base;		System.out.println(tab(n) + "# resolving inheritance for baseType " + baseType); // RGFIX				System.out.println(tab(n) + "# - getting complex type " + baseType); // RGFIX				System.out.println(tab(n) + "# - read complex type " + hmTypes.get(baseType)); // RGFIX				ComplexTypeEntry baseCTE = (ComplexTypeEntry) hmTypes.get(baseType);		System.out.println(tab(n) + "# - baseCTE = " + baseCTE); // RGFIX				if (baseCTE == null)			throw new IllegalArgumentException("Base type not found for : " + baseType);		ArrayList result = new ArrayList(resolveInheritance(baseCTE, n + 1));		ArrayList al = cte.complexContent.alElements;		for(int i=0; i<al.size(); i++)			result.add(al.get(i));		return result;	}	// RGFIX	private String tab(int n)	{		StringBuffer sb = new StringBuffer();		for (int i = 0; i < n; i++) sb.append("\t");		return sb.toString();	}		//---------------------------------------------------------------------------	private MetadataAttribute buildMetadataAttrib(AttributeEntry ae)	{		String name = ae.name;		String ref  = ae.reference;		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 = ae.required;		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)	{//		System.out.println("#### ElementInfo:\n" + Xml.getString(e)); // RGFIX				element        = e;		file           = f;		targetNS       = tns;		targetNSPrefix = tnsp;	}}//==============================================================================

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -