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

📄 editlib.java

📁 联合国农粮署牵头开发的geonetwork源代码最新版
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
//==============================================================================//===//=== EditLib//===//=============================================================================//===	Copyright (C) 2001-2007 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA//===//===	Contact: Jeroen Ticheler - FAO - Viale delle Terme di Caracalla 2,//===	Rome - Italy. email: geonetwork@osgeo.org//==============================================================================package org.fao.geonet.kernel;import java.util.*;import org.fao.geonet.kernel.schema.*;import org.jdom.*;import jeeves.utils.Xml;import java.io.File;import org.fao.geonet.constants.Edit;//=============================================================================public class EditLib{	private DataManager dataMan;	private Hashtable   htVersions   = new Hashtable(1000);	private Hashtable   htSchemas    = new Hashtable();	private Hashtable   htSchemaDirs = new Hashtable();	private Hashtable   htSchemaSugg = new Hashtable();	//--------------------------------------------------------------------------	//---	//--- Constructor	//---	//--------------------------------------------------------------------------	/** Init structures	  */	public EditLib(DataManager dataMan)	{		this.dataMan = dataMan;		htVersions.clear();		htSchemas .clear();	}	//--------------------------------------------------------------------------	//---	//--- API methods	//---	//--------------------------------------------------------------------------	/** Loads the metadata schema from disk and adds it to the pool	  */	public void addSchema(String id, String xmlSchemaFile, String xmlSuggestFile) throws Exception	{		String path = new File(xmlSchemaFile).getParent() +"/";		htSchemas   .put(id, new SchemaLoader().load(xmlSchemaFile));		htSchemaDirs.put(id, path);		htSchemaSugg.put(id, new SchemaSuggestions(xmlSuggestFile));	}	//--------------------------------------------------------------------------	public MetadataSchema getSchema(String name)	{		MetadataSchema schema = (MetadataSchema) htSchemas.get(name);		if (schema == null)			throw new IllegalArgumentException("Schema not registered : " + name);		return schema;	}	//--------------------------------------------------------------------------	public String getSchemaDir(String name)	{		String dir = (String) htSchemaDirs.get(name);		if (dir == null)			throw new IllegalArgumentException("Schema not registered : " + name);		return dir;	}	//--------------------------------------------------------------------------	public Iterator getSchemas()	{		return htSchemas.keySet().iterator();	}	//--------------------------------------------------------------------------	public boolean existsSchema(String name)	{		return htSchemas.containsKey(name);	}	//--------------------------------------------------------------------------	/** Expands a metadata adding all information needed for editing.	  */	public String addEditingInfo(String schema, String id, Element md) throws Exception	{		String version = getVersion(id, true) +"";		enumerateTree(md, 1);		expandTree(schema, getSchema(schema), md);		return version;	}	//--------------------------------------------------------------------------	public void enumerateTree(Element md)	{		enumerateTree(md, 1);	}	//--------------------------------------------------------------------------	public String getVersion(String id)	{		return Integer.toString(getVersion(id, false));	}	//--------------------------------------------------------------------------	public String getNewVersion(String id)	{		return Integer.toString(getVersion(id, true));	}	//--------------------------------------------------------------------------	/** Given an element, creates all mandatory sub-elements. The given element	  * should be empty.	  */	public void fillElement(String schema, Element parent, Element md) throws Exception	{		fillElement(getSchema(schema), getSchemaSuggestions(schema), parent, md);	}	//--------------------------------------------------------------------------	/** Given an expanded tree, removes all info added for editing	  */	public void removeEditingInfo(Element md)	{		//--- purge children		List list = md.getChildren();		for(int i=0; i<list.size(); i++)		{			Element child = (Element) list.get(i);			if (!Edit.NS_PREFIX.equals(child.getNamespacePrefix()))				removeEditingInfo(child);			else			{				child.detach();				i--;			}		}	}	//--------------------------------------------------------------------------	/** Returns the element at a given reference.	  * @param md the metadata element expanded with editing info	  * @param ref the element position in a pre-order visit	  */	public Element findElement(Element md, String ref)	{		Element elem = md.getChild(Edit.RootChild.ELEMENT, Edit.NAMESPACE);		if (ref.equals(elem.getAttributeValue(Edit.Element.Attr.REF)))			 return md;		//--- search on children		List list = md.getChildren();		for(int i=0; i<list.size(); i++)		{			Element child = (Element) list.get(i);			if (!Edit.NS_PREFIX.equals(child.getNamespacePrefix()))			{				child = findElement(child, ref);				if (child != null)					return child;			}		}		return null;	}	//--------------------------------------------------------------------------	public Element addElement(String schema, Element el, String qname) throws Exception	{//		System.out.println("#### in addElement()"); // DEBUG//		System.out.println("#### - parent = " + el.getName()); // DEBUG//		System.out.println("#### - child qname = " + qname); // DEBUG		String name   = getUnqualifiedName(qname);		String ns     = getNamespace(qname, el);		String prefix = getPrefix(qname);		String parentName = getParentNameFromChild(el);		// System.out.println("#### - parent name = " + parentName); // DEBUG		// System.out.println("#### - child name = " + name); // DEBUG		// System.out.println("#### - child namespace = " + ns); // DEBUG		// System.out.println("#### - child prefix = " + prefix); // DEBUG		List childS = el.getChildren();		if (childS.size() > 0) {			Element elChildS = (Element)childS.get(0);			// System.out.println("#### - child text = " + elChildS.getName()); // DEBUG		}		Element child = new Element(name, prefix, ns);		MetadataSchema    mdSchema = getSchema(schema);		SchemaSuggestions mdSugg   = getSchemaSuggestions(schema);		String typeName = mdSchema.getElementType(el.getQualifiedName(),parentName);		// System.out.println("#### - type name = " + typeName); // DEBUG 		MetadataType type = mdSchema.getTypeInfo(typeName);		// System.out.println("#### - metadata tpe = " + type); // DEBUG		//--- collect all children, adding the new one at the end of the others		Vector children = new Vector();		for(int i=0; i<type.getElementCount(); i++)		{			List list = getChildren(el, type.getElementAt(i));			// System.out.println("####   - children of type " + type.getElementAt(i) + " list size = " + list.size()); // DEBUG			for(int j=0; j<list.size(); j++)				children.add(list.get(j));			if (qname.equals(type.getElementAt(i)))				children.add(child);		}		//--- read collected children to element to assure a correct position		//--- for the new added one		el.removeContent();		for(int i=0; i<children.size(); i++)			el.addContent((Element) children.get(i));		//--- add mandatory sub-tags		fillElement(mdSchema, mdSugg, el, child);		return child;	}	//--------------------------------------------------------------------------	//---	//--- Private methods	//---	//--------------------------------------------------------------------------	private List getChildren(Element el, String qname)	{		Vector result = new Vector();		List children = el.getChildren();		for(int i=0; i<children.size(); i++)		{			Element child = (Element) children.get(i);			if (child.getQualifiedName().equals(qname))				result.add(child);		}		return result;	}	//--------------------------------------------------------------------------	/** Returns the version of a metadata, incrementing it if necessary	  */	private synchronized int getVersion(String id, boolean increment)	{		Integer inVer = (Integer) htVersions.get(id);		if (inVer == null)			inVer = new Integer(1);		if (increment)			inVer = new Integer(inVer.intValue() +1);		htVersions.put(id, inVer);		return inVer.intValue();	}	//--------------------------------------------------------------------------	private void fillElement(MetadataSchema schema, SchemaSuggestions sugg,														Element parent, Element md) throws Exception	{		//System.out.println("#### entering fillElement()"); // DEBUG		String elemName = md.getQualifiedName();		String parentName = parent.getQualifiedName();		//System.out.println("#### - elemName = " + elemName); // DEBUG		//System.out.println("#### - isSimpleElement(" + elemName + ") = " + schema.isSimpleElement(elemName,parentName)); // DEBUG		if (schema.isSimpleElement(elemName,parentName))			return;		MetadataType type = schema.getTypeInfo(schema.getElementType(elemName,parentName));		//System.out.println("#### - type:"); // DEBUG		//System.out.println("####   - name = " + type.getName()); // DEBUG		//System.out.println("####   - # attributes = " + type.getAttributeCount()); // DEBUG		//System.out.println("####   - # elements = " + type.getElementCount()); // DEBUG		//System.out.println("####   - # isOrType = " + type.isOrType()); // DEBUG		//-----------------------------------------------------------------------		//--- handle attributes		for(int i=0; i<type.getAttributeCount(); i++)		{			MetadataAttribute attr = type.getAttributeAt(i);			//System.out.println("####   - " + i + " attribute = " + attr.name); // DEBUG			//System.out.println("####     - required = " + attr.required); // DEBUG			if (attr.required)			{				String value = "";				if (attr.defValue != null)					value = attr.defValue;				String uname = getUnqualifiedName(attr.name);				String ns     = getNamespace(attr.name, md);				String prefix = getPrefix(attr.name);				// if the prefix is gml then we create the attribute with the				// appropriate namespace because gml types include ref'd attributes				// like gml:id - the others such as gmd etc don't include refs but				// define types that include attributes without namespace prefixes				if (prefix.equals("gml"))					md.setAttribute(new Attribute(uname, value, Namespace.getNamespace(prefix,ns)));				else					md.setAttribute(new Attribute(uname, value));			}		}		//-----------------------------------------------------------------------		//--- add mandatory children		if (!type.isOrType())		{			for(int i=0; i<type.getElementCount(); i++)			{				int    minCard   = type.getMinCardinAt(i);				String childName = type.getElementAt(i);				// Now if the element has subs examine them and substitute the				// correct element based on the namespace of the root element				if (type.examineSubs(i)) {					childName  = getProfileSubstituteName(schema,childName,md);					//System.out.println("- childName has substituted = " + childName);				}				if (minCard > 0 || sugg.isSuggested(elemName, childName))				{					MetadataType elemType = schema.getTypeInfo(schema.getElementType(childName,elemName));					//--- There can be 'or' elements with other 'or' elements inside them.					//--- In this case we cannot expand the inner 'or' elements so the					//--- only way to solve the problem is to avoid the creation of them					if (schema.isSimpleElement(elemName, childName) || !elemType.isOrType())					{						String name   = getUnqualifiedName(childName);						String ns     = getNamespace(childName, md);						String prefix = getPrefix(childName);						Element child = new Element(name, prefix, ns);						md.addContent(child);						fillElement(schema, sugg, md, child);					}				}			}		}		else		{			System.out.println("WARNING : requested expansion of an OR element : " +md.getName());		}	}	//--------------------------------------------------------------------------	//---	//--- Tree expansion methods	//---	//--------------------------------------------------------------------------	/** Does a pre-order visit enumerating each node	  */	private int enumerateTree(Element md, int ref)	{		Element elem = new Element(Edit.RootChild.ELEMENT, Edit.NAMESPACE);		elem.setAttribute(new Attribute(Edit.Element.Attr.REF, ref +""));		List list = md.getChildren();		for(int i=0; i<list.size(); i++)			ref = enumerateTree((Element) list.get(i), ref +1);		md.addContent(elem);		return ref;	}	//--------------------------------------------------------------------------	/** Given a metadata, does a recursive scan adding information for editing	  */	private void expandTree(String schemaName, MetadataSchema schema, Element md) throws Exception	{		expandElement(schemaName, schema, md);		List list = md.getChildren();		for(int i=0; i<list.size(); i++)		{			Element child = (Element) list.get(i);			if (!Edit.NS_PREFIX.equals(child.getNamespacePrefix()))				expandTree(schemaName, schema, child);		}	}

⌨️ 快捷键说明

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