📄 deafaultnodetemplatefactory.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.template;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.apache.log4j.Logger;
/**
* @author Vladislav Kamensky
*/
public class DeafaultNodeTemplateFactory implements NodeTemplateFactory {
private static Logger LOG = Logger.getLogger(DeafaultNodeTemplateFactory.class);
private static Map m_map = new HashMap();
private Map m_templates = Collections.synchronizedMap(new HashMap());
private String m_templatesPath = null;
private DeafaultNodeTemplateFactory(String p_templatePath, String[] p_templates) {
m_templatesPath = p_templatePath;
loadTemplates(p_templatePath, p_templates);
}
public synchronized static DeafaultNodeTemplateFactory getInstance(String p_templatePath, String[] p_templates) {
if (LOG.isInfoEnabled()) {
LOG.info("Getting DeafaultNodeTemplateFactory for path: " + p_templatePath);
}
if (!m_map.containsKey(p_templatePath)) {
m_map.put(p_templatePath, new DeafaultNodeTemplateFactory(p_templatePath, p_templates));
if (LOG.isInfoEnabled()) {
LOG.info("New DeafaultNodeTemplateFactory was created");
}
}
return (DeafaultNodeTemplateFactory) m_map.get(p_templatePath);
}
public NodeTemplate getTemplate(String p_name) {
NodeTemplateImpl a_nodeTemplate = (NodeTemplateImpl) m_templates.get(p_name);
if (a_nodeTemplate == null) {
return new NullNodeTemplate();
}
return a_nodeTemplate;
}
public void loadTemplates(String[] p_templates) {
loadTemplates(m_templatesPath, p_templates);
}
private void loadTemplates(String p_templatePath, String[] p_templates) {
InputStream a_inputStream = null;
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
if (classLoader == null) {
classLoader = this.getClass().getClassLoader();
}
for (int i = 0; i < p_templates.length; i++) {
String a_tmplPath = p_templatePath + p_templates[i];
a_inputStream = classLoader.getResourceAsStream(a_tmplPath);
if (a_inputStream == null) {
if (LOG.isInfoEnabled()) {
LOG.info("Template path: " + a_tmplPath + " cann't be accessed");
}
continue;
}
try {
BufferedReader a_reader
= new BufferedReader(new InputStreamReader(a_inputStream));
StringBuffer a_buffer = new StringBuffer();
for (String a_line = a_reader.readLine(); a_line != null; a_line = a_reader.readLine()) {
a_buffer.append(a_line);
}
NodeTemplateImpl a_nodeTemplate = new NodeTemplateImpl(a_buffer.toString());
m_templates.put(p_templates[i], a_nodeTemplate);
} catch (IOException e) {
LOG.error("Template path: " + a_tmplPath + " cann't be accessed", e);
continue;
} finally {
if (a_inputStream != null) {
try {
a_inputStream.close();
} catch (IOException ignored) {
}
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -