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

📄 xmlconfigwriter.java

📁 电子地图服务器,搭建自己的地图服务
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
/* 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 com.vividsolutions.jts.geom.Envelope;
import org.geotools.filter.FilterTransformer;
import org.geotools.geometry.GeneralEnvelope;
import org.opengis.coverage.grid.GridGeometry;
import org.opengis.util.InternationalString;
import org.vfny.geoserver.global.ConfigurationException;
import org.vfny.geoserver.global.CoverageDimension;
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.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 java.awt.Color;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.transform.TransformerException;


/**
 * XMLConfigWriter purpose.
 *
 * <p>
 * This class is intended to store a configuration to be written and complete
 * the output to XML.
 * </p>
 *
 * <p></p>
 *
 * @author dzwiers, Refractions Research, Inc.
 * @version $Id: XMLConfigWriter.java 7746 2007-11-13 15:38:35Z aaime $
 */
public class XMLConfigWriter {
    /** Used internally to create log information to detect errors. */
    private static final Logger LOGGER = org.geotools.util.logging.Logging.getLogger("org.vfny.geoserver.global");

    /**
     * XMLConfigWriter constructor.
     *
     * <p>
     * Should never be called.
     * </p>
     */
    private XMLConfigWriter() {
    }

    public static void store(DataDTO data, File root) throws ConfigurationException {
        if (LOGGER.isLoggable(Level.FINE)) {
            LOGGER.fine("In method store DataDTO");
        }

        if (data == null) {
            throw new ConfigurationException("DataDTO is null: cannot write.");
        }

        WriterUtils.initFile(root, true);

        //        boolean inDataDir = GeoserverDataDirectory.isTrueDataDir();

        //We're just checking if it's actually a data_dir, not trying to
        //to do backwards compatibility.  So if an old data_dir is made in
        //the old way, on save it'll come to the new way.
        File fileDir = root; // ? root : new File(root, "WEB-INF/");
        File configDir = WriterUtils.initFile(fileDir, true);

        File catalogFile = WriterUtils.initWriteFile(new File(configDir, "catalog.xml"), false);

        try {
            Writer fw = new OutputStreamWriter(new FileOutputStream(catalogFile), getDefaultEncoding());
            storeCatalog(new WriterHelper(fw), data);
            fw.close();
        } catch (IOException e) {
            throw new ConfigurationException("Store" + root, e);
        }

        File dataDir;

        //        if (!inDataDir) {
        //            dataDir = WriterUtils.initFile(new File(root, "data/"), true);
        //        } else {
        dataDir = root;

        //        }
        File featureTypeDir = WriterUtils.initFile(new File(dataDir, "featureTypes/"), true);
        storeFeatures(featureTypeDir, data);

        File coverageDir = WriterUtils.initFile(new File(dataDir, "coverages/"), true);
        storeCoverages(coverageDir, data);
    }

    /**
     * Returns the default encoding for configuration files. For the moment we default to
     * UTF8, but we may want to make this user configurable (UTF-16 may be needed?)
     * @return
     */
    private static String getDefaultEncoding() {
        return "UTF-8";
    }

    public static void store(WCSDTO wcs, WMSDTO wms, WFSDTO wfs, GeoServerDTO geoServer, File root)
        throws ConfigurationException {
        if (LOGGER.isLoggable(Level.FINEST)) {
            LOGGER.finest("In method store WCSDTO,WMSDTO,WFSDTO, GeoServerDTO");
        }

        if (geoServer == null) {
            throw new ConfigurationException(
                "null parameter in store(WCSDTO,WMSDTO,WFSDTO, GeoServerDTO): cannot write.");
        }

        WriterUtils.initFile(root, true);

        //        boolean inDataDir = GeoserverDataDirectory.isTrueDataDir();

        //We're just checking if it's actually a data_dir, not trying to
        //to do backwards compatibility.  So if an old data_dir is made in
        //the old way, on save it'll come to the new way.
        File fileDir = root; //inDataDir ? root : new File(root, "WEB-INF/");
        File configDir = WriterUtils.initFile(fileDir, true);
        File configFile = WriterUtils.initWriteFile(new File(configDir, "services.xml"), false);

        try {
            Writer fw = new OutputStreamWriter(new FileOutputStream(configFile), getDefaultEncoding());
            storeServices(new WriterHelper(fw), wcs, wms, wfs, geoServer);
            fw.close();
        } catch (IOException e) {
            throw new ConfigurationException("Store" + root, e);
        }
    }

    public static void store(WCSDTO wcs, WMSDTO wms, WFSDTO wfs, GeoServerDTO geoServer,
        DataDTO data, File root) throws ConfigurationException {
        store(wcs, wms, wfs, geoServer, root);
        store(data, root);
    }

    /**
     * storeServices purpose.
     *
     * <p>
     * Writes the services.xml file from the model in memory.
     * </p>
     *
     * @param cw The Configuration Writer
     * @param wms DOCUMENT ME!
     * @param wfs DOCUMENT ME!
     * @param geoServer DOCUMENT ME!
     *
     * @throws ConfigurationException When an IO exception occurs.
     */
    protected static void storeServices(WriterHelper cw, WCSDTO wcs, WMSDTO wms, WFSDTO wfs,
        GeoServerDTO geoServer) throws ConfigurationException {
        if (LOGGER.isLoggable(Level.FINER)) {
            LOGGER.finer("In method storeServices");
        }

        cw.writeln("<?config.xml version=\"1.0\" encoding=\"UTF-8\"?>");
        cw.comment("Service level configuration");
        cw.openTag("serverConfiguration");

        GeoServerDTO g = geoServer;

        if (g != null) {
            cw.openTag("global");

            if (g.getLog4jConfigFile() != null) {
                cw.textTag("log4jConfigFile", g.getLog4jConfigFile());
            }

            cw.valueTag("suppressStdOutLogging", g.getSuppressStdOutLogging() + "");

            if (g.getLogLocation() != null) {
                cw.textTag("logLocation", g.getLogLocation());
            }

            cw.valueTag("JaiMemoryCapacity", "" + g.getJaiMemoryCapacity());
            cw.valueTag("JaiMemoryThreshold", "" + g.getJaiMemoryThreshold());
            cw.valueTag("JaiTileThreads", "" + g.getJaiTileThreads());
            cw.valueTag("JaiTilePriority", "" + g.getJaiTilePriority());
            cw.valueTag("JaiRecycling", "" + g.getJaiRecycling());
            cw.valueTag("ImageIOCache", "" + g.getImageIOCache());
            cw.valueTag("JaiJPEGNative", "" + g.getJaiJPEGNative());
            cw.valueTag("JaiPNGNative", "" + g.getJaiPNGNative());

            /*if(g.getBaseUrl()!=null && g.getBaseUrl()!=""){
               cw.comment("The base URL where this servlet will run.  If running locally\n"+
               "then http://localhost:8080 (or whatever port you're running on)\n"+
               "should work.  If you are serving to the world then this must be\n"+
               "the location where the geoserver servlets appear");
               cw.textTag("URL",g.getBaseUrl());
               }*/
            cw.comment("Sets the max number of Features returned by GetFeature");
            cw.valueTag("maxFeatures", "" + g.getMaxFeatures());
            cw.comment("Whether newlines and indents should be returned in \n"
                + "XML responses.  Default is false");
            cw.valueTag("verbose", "" + g.isVerbose());
            cw.comment("Whether the Service Exceptions returned to clients should contain\n"
                + "full java stack traces (useful for debugging). ");
            cw.valueTag("verboseExceptions", "" + g.isVerboseExceptions());
            cw.comment("Sets the max number of decimal places past the zero returned in\n"
                + "a GetFeature response.  Default is 4");
            cw.valueTag("numDecimals", "" + g.getNumDecimals());

            if (g.getCharSet() != null) {
                cw.comment("Sets the global character set.  This could use some more testing\n"
                    + "from international users, but what it does is sets the encoding\n"
                    + "globally for all postgis database connections (the charset tag\n"
                    + "in FeatureTypeConfig), as well as specifying the encoding in the return\n"
                    + "config.xml header and mime type.  The default is UTF-8.  Also be warned\n"
                    + "that GeoServer does not check if the CharSet is valid before\n"
                    + "attempting to use it, so it will fail miserably if a bad charset\n"
                    + "is used.");
                cw.valueTag("charSet", g.getCharSet().toString());
            }

            if ((g.getSchemaBaseUrl() != null) && (g.getSchemaBaseUrl() != "")) {
                cw.comment("Define a base url for the location of the wfs schemas.\n"
                    + "By default GeoServer loads and references its own at\n"
                    + "<URL>/data/capabilities. Uncomment to enable.  The\n"
                    + "standalone Tomcat server needs SchemaBaseUrl defined\n" + "for validation.");
                cw.textTag("SchemaBaseUrl", g.getSchemaBaseUrl());
            }

            if ((g.getProxyBaseUrl() != null) && (g.getSchemaBaseUrl() != "")) {
                cw.comment("Define a base url for the geoserver application.\n"
                    + "By default GeoServer uses the local one, but it may "
                    + "be wrong if you're using a reverse proxy in front of Geoserver");
                cw.textTag("ProxyBaseUrl", g.getProxyBaseUrl());
            }

            // removed, the user is now stored in the users.properties file
//            if ((g.getAdminUserName() != null) && (g.getAdminUserName() != "")) {
//                cw.comment("Defines the user name of the administrator for log in\n"
//                    + "to the web based administration tool.");
//                cw.textTag("adminUserName", g.getAdminUserName());
//            }
//
//            if ((g.getAdminPassword() != null) && (g.getAdminPassword() != "")) {
//                cw.comment("Defines the password of the administrator for log in\n"
//                    + "to the web based administration tool.");
//                cw.textTag("adminPassword", g.getAdminPassword());
//            }

            if (g.getContact() != null) {
                storeContact(g.getContact(), cw);
            }

            if ((g.getTileCache() != null) && !"".equals(g.getTileCache().trim())) {
                cw.comment("Defines hte location of a tile cache (full url or relative path)");
                cw.textTag("tileCache", g.getTileCache());
            }

            cw.closeTag("global");
        }

        if (!((wcs == null) && (wfs == null) && (wms == null))) {
            cw.openTag("services");

            if (wcs != null) {
                storeService(wcs, cw);
            }

            if (wfs != null) {
                storeService(wfs, cw);
            }

            if (wms != null) {
                storeService(wms, cw);
            }

            // Z39.50 is not used in the current system.
            cw.closeTag("services");
        }

        cw.closeTag("serverConfiguration");
    }

    /**
     * storeContact purpose.
     *
     * <p>
     * Writes a contact into the WriterUtils provided from the ContactConfig
     * provided.
     * </p>
     *
     * @param c The ContactConfig to write.
     * @param cw The Configuration Writer
     *
     * @throws ConfigurationException When an IO exception occurs.
     */
    protected static void storeContact(ContactDTO c, WriterHelper cw)
        throws ConfigurationException {
        if (LOGGER.isLoggable(Level.FINER)) {
            LOGGER.finer("In method storeContact");
        }

        if ((c != null) && !c.equals(new ContactDTO())) {
            cw.openTag("ContactInformation");
            cw.openTag("ContactPersonPrimary");
            cw.textTag("ContactPerson", c.getContactPerson());
            cw.textTag("ContactOrganization", c.getContactOrganization());
            cw.closeTag("ContactPersonPrimary");
            cw.textTag("ContactPosition", c.getContactPosition());
            cw.openTag("ContactAddress");
            cw.textTag("AddressType", c.getAddressType());
            cw.textTag("Address", c.getAddress());
            cw.textTag("City", c.getAddressCity());
            cw.textTag("StateOrProvince", c.getAddressState());
            cw.textTag("PostCode", c.getAddressPostalCode());
            cw.textTag("Country", c.getAddressCountry());
            cw.closeTag("ContactAddress");
            cw.textTag("ContactVoiceTelephone", c.getContactVoice());
            cw.textTag("ContactFacsimileTelephone", c.getContactFacsimile());
            cw.textTag("ContactElectronicMailAddress", c.getContactEmail());
            cw.textTag("ContactOnlineResource", c.getOnlineResource());
            cw.closeTag("ContactInformation");
        }
    }

    /**

⌨️ 快捷键说明

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