📄 xmltreefactory.java
字号:
/*
* ====================================================================
* The JSP Tree Software License, Version 1.1
*
* (this license is derived and fully compatible with the Apache Software
* License - see http://www.apache.org/LICENSE.txt)
*
* Copyright (c) 2002-2005 jsptree.sourceforge.net . All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. 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.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by
* jsptree.sourceforge.net (http://jsptree.sourceforge.net/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The name "JSP Tree" must not be used to endorse or promote
* products derived from this software without prior written permission.
* For written permission, please contact modano@users.sourceforge.net .
*
* 5. Products derived from this software may not be called "JSP Tree",
* nor may "JSP Tree" appear in their name, without prior written
* permission of jsptree.sourceforge.net.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED 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 APACHE SOFTWARE FOUNDATION OR
* ITS 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 net.sf.jsptree.tree;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import org.apache.log4j.Logger;
import org.w3c.dom.*;
import org.xml.sax.SAXException;
import net.sf.jsptree.component.JSPTreeNodeImpl;
/**
* Represents a TreeFactory for building {@link TreeStructureModel} instances from
* specified XML file. All of the trees it returns have a common {@link TreeStructureModel}
* interface. It needs to keep track of whether or not a
* particular {@link Tree} instance has been generated yet from XML file and that's
* why it represents <i>Singleton</i> pattern. It does not build tree
* instance each time it is requested, but use <i>Cloneable</i> mechanism
* of <code>TreeStructureModel</code> instead. In short, it parses XML file only once and then
* uses {@link TreeStructureModel#clone} method in order to return new <code>TreeStructureModel</code>
* instances.
* <p/>
* Actually this class represents <code>Abstract Factory</code> too. Because its
* {@link #getInstance} method builds new instance of <code>TreeFactory</code>
* per unique XML path and share these <code>Factories</code> between clients.
*
* @author Vladislav Kamensky
*/
public class XMLTreeFactory implements TreeFactory {
/**
* Logger instance.
*/
private static Logger LOG = Logger.getLogger(XMLTreeFactory.class);
private static Map m_instances = new HashMap();
private Map m_treeStructuresMap = null;
private Map m_treeStatesMap = null;
/**
* Path to XML file, which represents trees' definitions.
*/
private String m_pathToXMLTree = null;
/**
* Constructs new <code>XMLTreeFactory</code> instance and
* builds XML trees from spesified path.
*/
private XMLTreeFactory(String p_pathToXMLTree) throws TreeFactoryException {
m_pathToXMLTree = p_pathToXMLTree;
m_treeStructuresMap = new HashMap();
m_treeStatesMap = new HashMap();
for (Iterator wrappers = buildTree().entrySet().iterator(); wrappers.hasNext();) {
Map.Entry entry = (Map.Entry) wrappers.next();
Wrapper wrapper = (Wrapper) entry.getValue();
m_treeStructuresMap.put(entry.getKey(), wrapper.getStructureModel());
m_treeStatesMap.put(entry.getKey(), wrapper.getStateModel());
}
}
/**
* Creates (if necessary) a <code>TreeFactory</code> instance and returns it.
* If the <code>TreeFactory</code> instance with the given XML path has not been
* generated yet, it is created and cached in order to be shared subsequently.
*
* @param p_pathToXMLTree Path to XML file representing trees' definitions.
* @throws TreeFactoryException in case of specified path can not be resolved
*/
public synchronized static TreeFactory getInstance(String p_pathToXMLTree) throws TreeFactoryException {
TreeFactory a_treeFactory = (TreeFactory) m_instances.get(p_pathToXMLTree);
if (a_treeFactory == null) {
if (p_pathToXMLTree != null && p_pathToXMLTree.length() > 0) {
a_treeFactory = new XMLTreeFactory(p_pathToXMLTree);
m_instances.put(p_pathToXMLTree, a_treeFactory);
if (LOG.isDebugEnabled()) {
LOG.debug("New instance created with path: " + p_pathToXMLTree);
}
} else {
throw new TreeFactoryException("Path to xml tree can not be empty");
}
}
return a_treeFactory;
}
/**
* Returns new <code>TreeStructureModel</code> instance by its name.
*
* @param p_name The name of the tree to be built.
* @throws TreeFactoryException in case of tree with the given
* name can not be built
*/
public TreeStructureModel getTree(String p_name) throws TreeFactoryException {
TreeStructureModel a_tree = (TreeStructureModel) m_treeStructuresMap.get(p_name);
if (a_tree == null) {
throw new TreeFactoryException("Can not find tree with name: " + p_name);
}
try {
return (TreeStructureModel) a_tree.clone();
} catch (CloneNotSupportedException ex) {
throw new TreeFactoryException("CloneNotSupportedException:" + ex.getMessage());
}
}
public TreeStateModel getTreeStates(String p_name) throws TreeFactoryException {
TreeStateModel a_tree = (TreeStateModel) m_treeStatesMap.get(p_name);
if (a_tree == null) {
throw new TreeFactoryException("Can not find tree with name: " + p_name);
}
try {
return (TreeStateModel) a_tree.clone();
} catch (CloneNotSupportedException ex) {
throw new TreeFactoryException("CloneNotSupportedException:" + ex.getMessage());
}
}
private Map buildTree() throws TreeFactoryException {
if (LOG.isInfoEnabled()) {
LOG.info("started");
}
if (m_pathToXMLTree == null) {
LOG.error("xml tree file was not found");
throw new TreeFactoryException("xml tree file was not found");
}
if (LOG.isDebugEnabled()) {
LOG.debug("starting to construct tree from: " + m_pathToXMLTree);
}
return generateTreeMap(getDocument());
}
private Map generateTreeMap(Document p_document) throws TreeFactoryException {
Map a_map = new HashMap();
Element a_element = p_document.getDocumentElement();
p_document.getDocumentElement().normalize();
if (!a_element.getNodeName().equalsIgnoreCase(XMLTreeConstant.ROOT_ELEMENT)) {
throw new TreeFactoryException("There is no root element: " + XMLTreeConstant.ROOT_ELEMENT);
}
if (a_element.getChildNodes().getLength() == 0) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -