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

📄 schemaloader.java

📁 由国外的一个著名的geonetwork修改而来
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
//==============================================================================//===//===   SchemaLoader//===//==============================================================================//===	Copyright (C) 2001-2005 Food and Agriculture Organization of the//===	United Nations (FAO-UN), United Nations World Food Programme (WFP)//===	and United Nations Environment Programme (UNEP)//===//===	This program is free software; you can redistribute it and/or modify//===	it under the terms of the GNU General Public License as published by//===	the Free Software Foundation; either version 2 of the License, or (at//===	your option) any later version.//===//===	This program is distributed in the hope that it will be useful, but//===	WITHOUT ANY WARRANTY; without even the implied warranty of//===	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU//===	General Public License for more details.//===//===	You should have received a copy of the GNU General Public License//===	along with this program; if not, write to the Free Software//===	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA//===//===	Contact: Jeroen Ticheler - FAO - Viale delle Terme di Caracalla 2,//===	Rome - Italy. email: GeoNetwork@fao.org//==============================================================================package org.fao.geonet.kernel.schema;import java.io.File;import java.util.ArrayList;import java.util.HashMap;import java.util.HashSet;import java.util.Iterator;import java.util.List;import jeeves.utils.Xml;import org.jdom.Element;import org.jdom.JDOMException;import org.jdom.Namespace;//==============================================================================public class SchemaLoader{	private Element   elRoot;	private HashMap   hmElements = new HashMap();	private HashMap   hmTypes    = new HashMap();	private HashMap   hmAttrGrp  = new HashMap();	private HashMap   hmAbsElems = new HashMap();	private HashMap   hmSubsGrp  = new HashMap();	private HashMap   hmSubsLink = new HashMap();	private HashMap   hmAttribs  = new HashMap();	private HashMap   hmGroups   = new HashMap();	// RGFIX	private String targetNS;	private String targetNSPrefix;		/** Restrictions for simple types (element restriction) */	private HashMap   hmElemRestr = new HashMap();	/** Restrictions for simple types (type restriction) */	private HashMap   hmTypeRestr = new HashMap();	//---------------------------------------------------------------------------	//---	//--- Constructor	//---	//---------------------------------------------------------------------------	public SchemaLoader() {}	//---------------------------------------------------------------------------	//---	//--- API methods	//---	//---------------------------------------------------------------------------	public MetadataSchema load(String xmlSchemaFile) throws Exception	{		/*		// RGFIX		if (xmlSchemaFile.indexOf("19139") == -1)			return new MetadataSchema(new Element("MD_Metadata"));		if (xmlSchemaFile.indexOf("19139") != -1)			return new MetadataSchema(new Element("MD_Metadata"));		*/				//--- PHASE 1 : pre-processing		//---		//--- the xml file is parsed and simplified. Xml schema subtrees are		//--- wrapped in some classes		ArrayList alElementFiles = loadFile(xmlSchemaFile, new HashSet());		parseElements(alElementFiles);		//--- PHASE 2 : resolve abstract elements		for(Iterator i=hmSubsGrp.keySet().iterator(); i.hasNext();)		{			String elem = (String) i.next();			System.out.println("# resolving abstract element " + elem); // RGFIX						ArrayList elements = (ArrayList) hmSubsGrp.get(elem);			for(int j=0; j<elements.size(); j++)			{				ElementEntry ee = (ElementEntry) elements.get(j);				System.out.println("# - scanning element " + ee.name); // RGFIX								if (ee.type == null)				{					ee.type = (String) hmAbsElems.get(elem);					if (ee.type == null)						throw new IllegalArgumentException("Type is null for 'element' : " + ee.name);				}				hmElements.put(ee.name, ee.type);			}		}		//--- PHASE 3 : add elements		MetadataSchema mds = new MetadataSchema(elRoot);		for(Iterator i=hmElements.keySet().iterator(); i.hasNext();)		{			String elem = (String) i.next();			String type = (String) hmElements.get(elem);			ArrayList elemRestr = (ArrayList) hmElemRestr.get(elem);			ArrayList typeRestr = (ArrayList) hmTypeRestr.get(type);			if (elemRestr == null)				elemRestr = new ArrayList();			if (typeRestr != null)				elemRestr.addAll(typeRestr);			mds.addElement(elem, type, elemRestr);		}		//--- PHASE 4 : post-processing		//---		//--- resolve abstract types, attribute/substitution groups and other stuff		for(Iterator i=hmTypes.values().iterator(); i.hasNext();)		{			ComplexTypeEntry cte = (ComplexTypeEntry) i.next();			MetadataType mdt = new MetadataType();			mdt.setOrType(cte.isOrType);			//--- generate attribs			if (cte.attribGroup != null)			{				ArrayList al = (ArrayList) hmAttrGrp.get(cte.attribGroup);				if (al == null)					throw new IllegalArgumentException("Attribute group not found : " + cte.attribGroup);				for(int j=0; j<al.size(); j++)				{					AttributeEntry ae = (AttributeEntry) al.get(j);					mdt.addAttribute(buildMetadataAttrib(ae));				}			}			//--- resolve inheritance & add attribs from complexContent			if (cte.complexContent != null)			{				if (cte.complexContent.base != null)					cte.alElements = resolveInheritance(cte, 0);				//--- add attribs from complexContent (if any)				for(int j=0; j<cte.complexContent.alAttribs.size(); j++)				{					AttributeEntry ae = (AttributeEntry) cte.complexContent.alAttribs.get(j);					mdt.addAttribute(buildMetadataAttrib(ae));				}			}			if (cte.groupRef != null)			{				GroupEntry ge = (GroupEntry) hmGroups.get(cte.groupRef);				if (ge == null)					throw new IllegalArgumentException("Group ref not found for complex type :" +cte.groupRef);				if (ge.isChoice)				{					ElementEntry ee    = (ElementEntry) ge.alElements.get(0);					ElementEntry eeRef = (ElementEntry) hmElements.get(ee.ref);					//--- if the ref was not found then we have an abstract element					if (eeRef == null)					{						ArrayList al = (ArrayList) hmSubsGrp.get(ee.ref);						if (al == null)							throw new IllegalArgumentException("Abstract elem not found in substGroup : " +ee.ref);						for(int k=0; k<al.size(); k++)						{							ElementEntry eeDer = (ElementEntry) al.get(k);							mdt.addElement(eeDer.name, ee.min, ee.max);						}					}					else						throw new IllegalArgumentException("Found not abstract element in choice : " +eeRef.name);				}			}			//--- handle type's elements			else for(int j=0; j<cte.alElements.size(); j++)			{				ElementEntry ee = (ElementEntry) cte.alElements.get(j);				System.out.println("# resolving type of element " + ((ee.name != null) ? ee.name : ("ref --> " + ee.ref))); // RGFIX								String type;				if (ee.ref != null)				{					type = (String) hmElements.get(ee.ref);										System.out.println("# - got type '" + type + "' for element '" + ee.ref + "'"); // RGFIX				}				else				{					if (ee.name == null)						throw new IllegalArgumentException("Reference and name are null for element : " + ee.name);					type = "string";				}				//--- if type is null we have an abstract type				if (type == null)				{					System.out.println("# - ee.ref = '" + ee.ref + "'"); // RGFIX										ArrayList al = (ArrayList) hmSubsGrp.get(ee.ref);					if (al == null)					{						al = (ArrayList) hmSubsLink.get(ee.ref);						/* RGFIX: found singleton subst-group with only one abstract element						if (al == null)							throw new IllegalArgumentException("Reference not found inside subst-group : "+ee.ref);						mdt.addElement(ee.ref, ee.min, ee.max);						 */						if (al != null)							mdt.addElement(ee.ref, ee.min, ee.max);					}					else					{						mdt.setOrType(al.size() > 1);						for(int k=0; k<al.size(); k++)						{							ee = (ElementEntry) al.get(k);							mdt.addElement(ee.name, ee.min, ee.max);						}					}				}				else				{					String elemName = (ee.ref == null) ? ee.name : ee.ref;					mdt.addElement(elemName, ee.min, ee.max);				}			}			mds.addType(cte.name, mdt);		}		return mds;	}	//---------------------------------------------------------------------------	//---	//--- PHASE 1 : Schema loading	//---	//---------------------------------------------------------------------------	/** Loads the xml-schema file, removes annotations and resolve imports/includes */	private ArrayList loadFile(String xmlSchemaFile, HashSet loadedFiles) throws Exception	{		System.out.println("#### loading " + xmlSchemaFile); // RGFIX				loadedFiles.add(new File(xmlSchemaFile).getCanonicalPath());		Logger.log("Added : "+ new File(xmlSchemaFile).getCanonicalPath());		String path = new File(xmlSchemaFile).getParent() + "/";		//--- load xml-schema		elRoot = Xml.loadFile(xmlSchemaFile);				// RGFIX: 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;						System.out.println("#### - target namespace is " + targetNS + " (" + targetNSPrefix + ")"); // RGFIX				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();

⌨️ 快捷键说明

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