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

📄 dcinputsreader.java

📁 dspace 用j2ee架构的一个数字图书馆.开源程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * DCInputsReader.java * * Version: $Revision: 1.4 $ * * Date: $Date: 2006/01/18 19:24:47 $ * * Copyright (c) 2002-2005, Hewlett-Packard Company and Massachusetts * Institute of Technology.  All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are * met: * * - Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * - Neither the name of the Hewlett-Packard Company nor the name of the * Massachusetts Institute of Technology nor the names of their * contributors may be used to endorse or promote products derived from * this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH * DAMAGE. */package org.dspace.app.webui.util;import java.io.File;import java.util.Vector;import java.util.HashMap;import java.util.Iterator;import java.lang.Exception;import javax.servlet.ServletException;import org.xml.sax.SAXException;import org.w3c.dom.*;import javax.xml.parsers.*;import org.apache.log4j.Logger;import org.dspace.core.ConfigurationManager;import org.dspace.app.webui.servlet.SubmitServlet;/** * Submission form generator for DSpace. Reads and parses the installation  * form definitions file, input-forms.xml, from the configuration directory. * A forms definition details the page and field layout of the metadata * collection pages used by the submission process. Each forms definition * starts with a unique name that gets associated with that form set. * * The file also specifies which collections use which form sets. At a * minimum, the definitions file must define a default mapping from the * placeholder collection #0 to the distinguished form 'default'. Any * collections that use a custom form set are listed paired with the name * of the form set they use.  * * The definitions file also may contain sets of value pairs. Each value pair * will contain one string that the user reads, and a paired string that will * supply the value stored in the database if its sibling display value gets * selected from a choice list. * * @author  Brian S. Hughes * @version $Revision: 1.4 $ */public class DCInputsReader{    /** The ID of the default collection. Will never be the ID of a named collection */    static final String DEFAULT_COLLECTION = "default";    /** Name of the form definition XML file  */    static final String FORM_DEF_FILE = "input-forms.xml";    /** Keyname for storing dropdown value-pair set name */    static final String PAIR_TYPE_NAME = "value-pairs-name";    /** log4j logger */    private static Logger log = Logger.getLogger(DCInputsReader.class);    /** The fully qualified pathname of the form definition XML file */    private String defsFile = ConfigurationManager.getProperty("dspace.dir") + 	    File.separator + "config" + File.separator + FORM_DEF_FILE;    /** Reference to the collections to forms map,     *  computed from the forms definition file      */    private HashMap whichForms = null;    /** Reference to the forms definitions map,      * computed from the forms definition file      */    private HashMap formDefns  = null;    /** Reference to the value-pairs map,      * computed from the forms defition file      */    private HashMap valuePairs = null;    // Holds display/storage pairs        /**     * Mini-cache of last DCInputSet requested. If submissions are not     * typically form-interleaved, there will be a modest win.      */    private DCInputSet lastInputSet = null;    /**     * Parse an XML encoded submission forms template file, and create     * a hashmap containing all the form information. This     * hashmap will contain three top level structures: a map between     * collections and forms, the definition for each page of each form,     * and lists of pairs of values that populate selection boxes.     */    public DCInputsReader()          throws ServletException    {    	buildInputs(defsFile);    }    public DCInputsReader(String fileName)          throws ServletException    {    	buildInputs(fileName);    }    private void buildInputs(String fileName)          throws ServletException    {    	whichForms = new HashMap();    	formDefns  = new HashMap();    	valuePairs = new HashMap();    	String uri = "file:" + new File(fileName).getAbsolutePath();    	try    	{        	DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();        	factory.setValidating(false);        	factory.setIgnoringComments(true);        	factory.setIgnoringElementContentWhitespace(true);        	    		DocumentBuilder db = factory.newDocumentBuilder();    		Document doc = db.parse(uri);    		doNodes(doc);    		checkValues();    	}    	catch (FactoryConfigurationError fe)    	{    		throw new ServletException("Cannot create Submission form parser",fe);    	}    	catch (Exception e)    	{    		throw new ServletException("Error creating submission forms: "+e);    	}    }       /**     * Returns the set of DC inputs used for a particular collection,     * or the default set if no inputs defined for the collection     * @param  collectionHandle   collection's unique Handle     * @return DC input set     * @throws ServletException if no default set defined     */    public DCInputSet getInputs(String collectionHandle)    		throws ServletException    {       	String formName = (String)whichForms.get(collectionHandle);    	if (formName == null)    	{    		formName = (String)whichForms.get(DEFAULT_COLLECTION);    	}    	if (formName == null)    	{    		throw new ServletException("No form designated as default");    	}    	// check mini-cache, and return if match    	if ( lastInputSet != null && lastInputSet.getFormName().equals( formName ) )    	{    		return lastInputSet;    	}    	// cache miss - construct new DCInputSet    	Vector pages = (Vector)formDefns.get(formName);    	if ( pages == null )    	{    		throw new ServletException("Missing the default form");    		    	}    	lastInputSet = new DCInputSet(formName, pages, valuePairs);    	return lastInputSet;    }        /**     * Return the number of pages the inputs span for a desginated collection     * @param  collectionHandle   collection's unique Handle     * @return number of pages of input     * @throws ServletException if no default set defined     */    public int getNumberInputPages(String collectionHandle)    	throws ServletException    {    	return getInputs(collectionHandle).getNumberPages();    }        /**     * Process the top level child nodes in the passed top-level node.     * These should correspond to the collection-form maps, the form     * definitions, and the display/storage word pairs.     */    private void doNodes(Node n)		throws SAXException, ServletException    {    	if (n == null)    	{    		return;    	}    	Node e = getElement(n);    	NodeList nl = e.getChildNodes();    	int len = nl.getLength();    	boolean foundMap  = false;    	boolean foundDefs = false;    	for (int i = 0; i < len; i++)    	{    		Node nd = nl.item(i);    		if ((nd == null) || isEmptyTextNode(nd))    		{    			continue;    		}    		String tagName = nd.getNodeName();    		if (tagName.equals("form-map"))    		{    			processMap(nd);    			foundMap = true;    		}    		else if (tagName.equals("form-definitions"))    		{    			processDefinition(nd);    			foundDefs = true;    		}    		else if (tagName.equals("form-value-pairs"))    		{    			processValuePairs(nd);    		}    		// Ignore unknown nodes    	}    	if (!foundMap)        {    		throw new ServletException("No collection to form map found");        }    	if (!foundDefs)        {    		throw new ServletException("No form definition found");        }    }    /**     * Process the form-map section of the XML file.     * Each element looks like:      *   <name-map collection-handle="hdl" form-name="name" />     * Extract the collection handle and form name, put name in hashmap keyed     * by the collection handle.     */    private void processMap(Node e)         throws SAXException    {    	NodeList nl = e.getChildNodes();    	int len = nl.getLength();    	for (int i = 0; i < len; i++)    	{    		Node nd = nl.item(i);    		if (nd.getNodeName().equals("name-map"))    		{    			String id = getAttribute(nd, "collection-handle");    			String value = getAttribute(nd, "form-name");			String content = getValue(nd);    			if (id == null)    			{    				throw new SAXException("name-map element is missing collection-handle attribute");    			}			if (value == null)			{				throw new SAXException("name-map element is missing form-name attribute");			}			if (content != null && content.length() > 0)			{				throw new SAXException("name-map element has content, it should be empty.");			}    			whichForms.put(id, value);    		}  // ignore any child node that isn't a "name-map"    	}    }    /**     * Process the form-definitions section of the XML file.     * Each element is formed thusly: <form name="formname">...pages...</form>     * Each pages subsection is formed: <page number="#"> ...fields... </page>     * Each field is formed from: dc-element, dc-qualifier, label, hint,     * input-type name, required text, and repeatable flag.     */    private void processDefinition(Node e)         throws SAXException, ServletException    {    	int numForms = 0;    	NodeList nl = e.getChildNodes();    	int len = nl.getLength();    	for (int i = 0; i < len; i++)    	{    		Node nd = nl.item(i);    		// process each form definition    		if (nd.getNodeName().equals("form"))    		{    			numForms++;    			String formName = getAttribute(nd, "name");    			if (formName == null)    			{    				throw new SAXException("form element has no name attribute");    			}    			Vector pages = new Vector(); // the form contains pages    			formDefns.put(formName, pages);    			NodeList pl = nd.getChildNodes();    			int lenpg = pl.getLength();    			for (int j = 0; j < lenpg; j++)    			{    				Node npg = pl.item(j);    				// process each page definition    				if (npg.getNodeName().equals("page"))    				{    					String pgNum = getAttribute(npg, "number");    					if (pgNum == null)    					{    						throw new SAXException("Form " + formName + " has no identified pages");    					}    					Vector page = new Vector();    					pages.add(page);    					NodeList flds = npg.getChildNodes();    					int lenflds = flds.getLength();    					for (int k = 0; k < lenflds; k++)

⌨️ 快捷键说明

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