📄 xmlconfigreader.java
字号:
/* Copyright (c) 2001 - 2007 TOPP - www.openplans.org. All rights reserved.
* This code is licensed under the GPL 2.0 license, availible at the root
* application directory.
*/
package org.vfny.geoserver.global.xml;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.Reader;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLDecoder;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletContext;
import org.apache.xml.serialize.LineSeparator;
import org.apache.xml.serialize.OutputFormat;
import org.apache.xml.serialize.XMLSerializer;
import org.geoserver.ows.util.XmlCharsetDetector;
import org.geoserver.util.ReaderUtils;
import org.geotools.coverage.grid.GeneralGridRange;
import org.geotools.coverage.grid.GridGeometry2D;
import org.geotools.filter.FilterDOMParser;
import org.geotools.geometry.GeneralDirectPosition;
import org.geotools.geometry.GeneralEnvelope;
import org.geotools.referencing.CRS;
import org.geotools.util.NameFactory;
import org.geotools.util.NumberRange;
import org.opengis.coverage.grid.GridGeometry;
import org.opengis.filter.Filter;
import org.opengis.geometry.MismatchedDimensionException;
import org.opengis.referencing.FactoryException;
import org.opengis.referencing.NoSuchAuthorityCodeException;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.opengis.referencing.operation.TransformException;
import org.opengis.util.InternationalString;
import org.vfny.geoserver.global.ConfigurationException;
import org.vfny.geoserver.global.CoverageDimension;
import org.vfny.geoserver.global.FeatureTypeInfo;
import org.vfny.geoserver.global.GeoserverDataDirectory;
import org.vfny.geoserver.global.MetaDataLink;
import org.vfny.geoserver.global.dto.AttributeTypeInfoDTO;
import org.vfny.geoserver.global.dto.ContactDTO;
import org.vfny.geoserver.global.dto.CoverageInfoDTO;
import org.vfny.geoserver.global.dto.CoverageStoreInfoDTO;
import org.vfny.geoserver.global.dto.DataDTO;
import org.vfny.geoserver.global.dto.DataStoreInfoDTO;
import org.vfny.geoserver.global.dto.FeatureTypeInfoDTO;
import org.vfny.geoserver.global.dto.GeoServerDTO;
import org.vfny.geoserver.global.dto.LegendURLDTO;
import org.vfny.geoserver.global.dto.NameSpaceInfoDTO;
import org.vfny.geoserver.global.dto.ServiceDTO;
import org.vfny.geoserver.global.dto.StyleDTO;
import org.vfny.geoserver.global.dto.WCSDTO;
import org.vfny.geoserver.global.dto.WFSDTO;
import org.vfny.geoserver.global.dto.WMSDTO;
import org.vfny.geoserver.util.CoverageStoreUtils;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Envelope;
/**
* XMLConfigReader purpose.
*
* <p>
* Description of XMLConfigReader Static class to load a configuration
* org.vfny.geoserver.global.dto
* </p>
*
* <p>
* Example Use:
*
* <pre><code>
* ModelConfig m = XMLConfigReader.load(new File("/conf/"));
* </code></pre>
*
* </p>
*
* @author dzwiers, Refractions Research, Inc.
* @version $Id: XMLConfigReader.java 7746 2007-11-13 15:38:35Z aaime $
*/
public class XMLConfigReader {
/** Used internally to create log information to detect errors. */
private static final Logger LOGGER = org.geotools.util.logging.Logging.getLogger("org.vfny.geoserver.global");
/** The root directory from which the configuration is loaded. */
private File root;
/** Is set to true after the model is loaded into memory. */
private boolean initialized = false;
private WMSDTO wms;
private WFSDTO wfs;
private WCSDTO wcs;
private GeoServerDTO geoServer;
private DataDTO data;
/** the servlet context * */
ServletContext context;
/**
* XMLConfigReader constructor.
*
* <p>
* Should never be called.
* </p>
*/
protected XMLConfigReader(ServletContext context) {
this.context = context;
wms = new WMSDTO();
wfs = new WFSDTO();
wcs = new WCSDTO();
geoServer = new GeoServerDTO();
data = new DataDTO();
root = new File(".");
}
/**
* <p>
* This method loads the config files from the specified directory into a
* ModelConfig. If the path is incorrect, or the directory is formed
* correctly, a ConfigException will be thrown and/or null returned. <br>
* <br>
* The config directory is as follows:<br>
*
* <ul>
* <li> ./WEB-INF/catalog.xml </li>
* <li> ./WEB-INF/services.xml </li>
* <li> ./data/featuretypes/ /info.xml </li>
* <li> ./data/featuretypes/ /schema.xml </li>
* </ul>
* </p>
*
* @param root
* A directory which contains the config files.
*
* @throws ConfigurationException
* When an error occurs.
*/
public XMLConfigReader(File root, ServletContext context) throws ConfigurationException {
this.root = root;
this.context = context;
wms = new WMSDTO();
wfs = new WFSDTO();
wcs = new WCSDTO();
geoServer = new GeoServerDTO();
data = new DataDTO();
load();
initialized = true;
}
public boolean isInitialized() {
return initialized;
}
/**
* load purpose.
*
* <p>
* Main load routine, sets up file handles for various other portions of the
* load procedure.
* </p>
*
* @throws ConfigurationException
*/
protected void load() throws ConfigurationException {
try {
root = ReaderUtils.checkFile(root, true);
} catch (FileNotFoundException e) {
throw new ConfigurationException("Can't access " + root.getAbsolutePath(), e);
}
// //Doing some trys here for either being in the webapp, with data and
// web-inf defined
// //or in a true data_dir, with the catalog and service in the same
// root dir.
// try {
// configDir = ReaderUtils.checkFile(new File(root, "WEB-INF/"), true);
// } catch (ConfigurationException confE) {
// //no WEB-INF, so we're in a data_dir, use as root.
// configDir = root;
// }
// File configFile = ReaderUtils.checkFile(new File(configDir,
// "services.xml"), false);
File servicesFile = GeoserverDataDirectory.findConfigFile("services.xml");
loadServices(servicesFile);
File catalogFile = GeoserverDataDirectory.findConfigFile("catalog.xml");
File featureTypeDir = GeoserverDataDirectory.findConfigDir(root, "featureTypes/");
File styleDir = GeoserverDataDirectory.findConfigDir(root, "styles/");
File coverageDir = GeoserverDataDirectory.findConfigDir(root, "coverages/");
loadCatalog(catalogFile, featureTypeDir, styleDir, coverageDir);
// Future additions
// validationDir = ReaderUtils.initFile(new
// File(dataDir,"validation/"),true);
// loadValidation(validationDir);
}
/**
* loadServices purpose.
*
* <p>
* loads services.xml into memory with the assistance of other class
* methods.
* </p>
*
* @param configFile
* services.xml
*
* @throws ConfigurationException
* When an error occurs.
*/
protected void loadServices(File configFile) throws ConfigurationException {
if (LOGGER.isLoggable(Level.FINE)) {
LOGGER.config(new StringBuffer("Loading configuration file: ").append(configFile)
.toString());
}
Element configElem = null;
try {
Reader reader = XmlCharsetDetector
.getCharsetAwareReader(new FileInputStream(configFile));
configElem = ReaderUtils.parse(reader);
reader.close();
} catch (FileNotFoundException e) {
throw new ConfigurationException(e);
} catch (IOException e) {
throw new ConfigurationException(e);
}
if (LOGGER.isLoggable(Level.FINE)) {
LOGGER.fine("parsing configuration documents");
}
Element elem = (Element) configElem.getElementsByTagName("global").item(0);
loadGlobal(elem);
NodeList configuredServices = configElem.getElementsByTagName("service");
boolean foundWCS = false; // record if we have found them or not
int nServices = configuredServices.getLength();
for (int i = 0; i < nServices; i++) {
elem = (Element) configuredServices.item(i);
String serviceType = elem.getAttribute("type");
if ("WCS".equalsIgnoreCase(serviceType)) {
foundWCS = true;
loadWCS(elem);
} else if ("WFS".equalsIgnoreCase(serviceType)) {
loadWFS(elem);
} else if ("WMS".equalsIgnoreCase(serviceType)) {
loadWMS(elem);
} else {
LOGGER.warning("Ignoring unknown service type: " + serviceType);
}
}
if (!foundWCS) {
wcs = defaultWcsDto();
}
}
/**
* This is a very poor, but effective tempory method of setting a default
* service value for WCS, until we get a new config system.
*
* @return
*/
private WCSDTO defaultWcsDto() {
WCSDTO dto = new WCSDTO();
ServiceDTO service = new ServiceDTO();
service.setName("My GeoServer WCS");
service.setTitle("My GeoServer WCS");
service.setEnabled(true);
List keyWords = new ArrayList();
keyWords.add("WCS");
keyWords.add("WMS");
keyWords.add("GEOSERVER");
service.setKeywords(keyWords);
MetaDataLink mdl = new MetaDataLink();
mdl.setAbout("http://geoserver.org");
mdl.setType("undef");
mdl.setMetadataType("other");
mdl.setContent("NONE");
service.setMetadataLink(mdl);
service.setFees("NONE");
service.setAccessConstraints("NONE");
service.setMaintainer("http://jira.codehaus.org/secure/BrowseProject.jspa?id=10311");
try {
service.setOnlineResource(new URL("http://geoserver.org"));
} catch (MalformedURLException e) {
e.printStackTrace();
}
dto.setService(service);
return dto;
}
/**
* loadCatalog purpose.
*
* <p>
* loads catalog.xml into memory with the assistance of other class methods.
* </p>
*
* @param catalogFile
* catalog.xml
* @param featureTypeDir
* the directory containing the info.xml files for the
* featuretypes.
*
* @throws ConfigurationException
* When an error occurs.
*/
protected void loadCatalog(File catalogFile, File featureTypeDir, File styleDir,
File coverageDir) throws ConfigurationException {
Element catalogElem = null;
try {
if (LOGGER.isLoggable(Level.FINE)) {
LOGGER.config(new StringBuffer("Loading configuration file: ").append(catalogFile)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -