📄 configxmlreader.java
字号:
/*
* $Id: ConfigXMLReader.java,v 1.2 2003/09/14 05:36:47 jonesde Exp $
*
* Copyright (c) 2001-2003 The Open For Business Project - www.ofbiz.org
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
* OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
* THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package org.ofbiz.content.webapp.control;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import org.ofbiz.base.util.Debug;
import org.ofbiz.base.util.UtilCache;
import org.ofbiz.base.util.UtilXml;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
/**
* ConfigXMLReader.java - Reads and parses the XML site config files.
*
* @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
* @version $Revision: 1.2 $
* @since 2.0
*/
public class ConfigXMLReader {
public static final String module = ConfigXMLReader.class.getName();
public static UtilCache requestCache = new UtilCache("webapp.ConfigXMLReader.Request");
public static UtilCache viewCache = new UtilCache("webapp.ConfigXMLReader.View");
public static UtilCache headCache = new UtilCache("webapp.ConfigXMLReader.Config");
public static UtilCache handlerCache = new UtilCache("webapp.ConfigXMLReader.Handler");
/** Site Config Variables */
public static final String DEFAULT_ERROR_PAGE = "errorpage";
public static final String SITE_OWNER = "owner";
public static final String SECURITY_CLASS = "security-class";
public static final String FIRSTVISIT = "firstvisit";
public static final String PREPROCESSOR = "preprocessor";
public static final String POSTPROCESSOR = "postprocessor";
/** URI Config Variables */
public static final String INCLUDE = "include";
public static final String INCLUDE_FILE = "file";
public static final String INCLUDE_URL = "url";
public static final String REQUEST_MAPPING = "request-map";
public static final String REQUEST_URI = "uri";
public static final String REQUEST_EDIT = "edit";
public static final String REQUEST_DESCRIPTION = "description";
public static final String ERROR_PAGE = "error";
public static final String NEXT_PAGE = "success";
public static final String SECURITY = "security";
public static final String SECURITY_HTTPS = "https";
public static final String SECURITY_AUTH = "auth";
public static final String SECURITY_EXTVIEW = "external-view";
public static final String SECURITY_DIRECT = "direct-request";
public static final String EVENT = "event";
public static final String EVENT_PATH = "path";
public static final String EVENT_TYPE = "type";
public static final String EVENT_METHOD = "invoke";
public static final String RESPONSE = "response";
public static final String RESPONSE_NAME = "name";
public static final String RESPONSE_TYPE = "type";
public static final String RESPONSE_VALUE = "value";
/** View Config Variables */
public static final String VIEW_MAPPING = "view-map";
public static final String VIEW_NAME = "name";
public static final String VIEW_PAGE = "page";
public static final String VIEW_TYPE = "type";
public static final String VIEW_INFO = "info";
public static final String VIEW_CONTENT_TYPE = "content-type";
public static final String VIEW_ENCODING = "encoding";
public static final String VIEW_DESCRIPTION = "description";
/** Handler Config Variables */
public static final String HANDLER = "handler";
public static final String HANDLER_NAME = "name";
public static final String HANDLER_TYPE = "type";
public static final String HANDLER_CLASS = "class";
/** Loads the XML file and returns the root element */
public static Element loadDocument(URL location) {
Document document = null;
try {
document = UtilXml.readXmlDocument(location, true);
Element rootElement = document.getDocumentElement();
// rootElement.normalize();
if (Debug.verboseOn()) Debug.logVerbose("Loaded XML Config - " + location, module);
return rootElement;
} catch (Exception e) {
Debug.logError(e, module);
}
return null;
}
/** Gets a HashMap of request mappings. */
public static HashMap getRequestMap(URL xml) {
HashMap requestMap = (HashMap) requestCache.get(xml);
if (requestMap == null) // don't want to block here
{
synchronized (ConfigXMLReader.class) {
// must check if null again as one of the blocked threads can still enter
requestMap = (HashMap) requestCache.get(xml);
if (requestMap == null) {
requestMap = loadRequestMap(xml);
requestCache.put(xml, requestMap);
}
}
}
// never return null, just an empty map...
if (requestMap == null) requestMap = new HashMap();
return requestMap;
}
/** Gets a HashMap of request mappings. */
public static HashMap loadRequestMap(URL xml) {
HashMap map = new HashMap();
Element root = loadDocument(xml);
if (root == null) return map;
NodeList list = root.getElementsByTagName(INCLUDE);
for (int rootCount = 0; rootCount < list.getLength(); rootCount++) {
Node node = list.item(rootCount);
// Make sure we are an element.
if (node instanceof Element) {
// Get the file to include
Element mapping = (Element) node;
String includeFile = mapping.getAttribute(INCLUDE_FILE);
if ((includeFile != null) && (includeFile.length() > 0)) {
File oldFile = new File(xml.getFile());
File newFile = new java.io.File("" + oldFile.getParent() + java.io.File.separator + includeFile);
try {
HashMap subMap = loadRequestMap(newFile.toURL());
map.putAll(subMap);
} catch (MalformedURLException mue) {
mue.printStackTrace();
}
}
String includeURL = mapping.getAttribute(INCLUDE_URL);
if ((includeURL != null) && (includeURL.length() > 0)) {
try {
HashMap subMap = loadRequestMap(new URL(includeURL));
map.putAll(subMap);
} catch (MalformedURLException mue) {
mue.printStackTrace();
}
}
}
}
list = root.getElementsByTagName(REQUEST_MAPPING);
for (int rootCount = 0; rootCount < list.getLength(); rootCount++) {
// Create a URI-MAP for each element found.
HashMap uriMap = new HashMap();
// Get the node.
Node node = list.item(rootCount);
// Make sure we are an element.
if (node instanceof Element) {
// Get the URI info.
Element mapping = (Element) node;
String uri = mapping.getAttribute(REQUEST_URI);
String edit = mapping.getAttribute(REQUEST_EDIT);
if (edit == null || edit.equals(""))
edit = "true";
if (uri != null) {
uriMap.put(REQUEST_URI, uri);
uriMap.put(REQUEST_EDIT, edit);
}
// Check for security.
NodeList securityList = mapping.getElementsByTagName(SECURITY);
if (securityList.getLength() > 0) {
Node securityNode = securityList.item(0); // There should be only one.
if (securityNode instanceof Element) { // We must be an element.
Element security = (Element) securityNode;
String securityHttps = security.getAttribute(SECURITY_HTTPS);
String securityAuth = security.getAttribute(SECURITY_AUTH);
String securityExtView = security.getAttribute(SECURITY_EXTVIEW);
String securityDirectRequest = security.getAttribute(SECURITY_DIRECT);
uriMap.put(SECURITY_HTTPS, securityHttps);
uriMap.put(SECURITY_AUTH, securityAuth);
uriMap.put(SECURITY_EXTVIEW, securityExtView);
uriMap.put(SECURITY_DIRECT, securityDirectRequest);
}
}
// Check for an event.
NodeList eventList = mapping.getElementsByTagName(EVENT);
if (eventList.getLength() > 0) {
Node eventNode = eventList.item(0); // There should be only one.
if (eventNode instanceof Element) { // We must be an element.
Element event = (Element) eventNode;
String type = event.getAttribute(EVENT_TYPE);
String path = event.getAttribute(EVENT_PATH);
String invoke = event.getAttribute(EVENT_METHOD);
uriMap.put(EVENT_TYPE, type);
uriMap.put(EVENT_PATH, path);
uriMap.put(EVENT_METHOD, invoke);
}
}
// Check for a description.
NodeList descList = mapping.getElementsByTagName(REQUEST_DESCRIPTION);
if (descList.getLength() > 0) {
Node descNode = descList.item(0); // There should be only one.
if (descNode instanceof Element) { // We must be an element.
NodeList children = descNode.getChildNodes();
if (children.getLength() > 0) {
Node cdata = children.item(0); // Just get the first one.
String description = cdata.getNodeValue();
if (description != null)
description = description.trim();
else
description = "";
uriMap.put(REQUEST_DESCRIPTION, description);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -