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

📄 wfsxmlwriter.java

📁 电子地图服务器,搭建自己的地图服务
💻 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.geoserver.wfs.response;

import org.geoserver.ows.util.ResponseUtils;
import org.geoserver.ows.xml.v1_0.OWS;
import org.geoserver.wfs.WFS;
import org.geotools.filter.v1_0.OGC;
import org.geotools.gml2.bindings.GML;
import org.geotools.xs.bindings.XS;
import org.xml.sax.helpers.NamespaceSupport;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;


/**
 * Utility class for writing out wfs xml documents.
 * <p>
 * Usage:
 * <pre>
 *         WFS wfs = ...;
 *  OutputStream output = ...;
 *
 *         WfsXmlWriter writer = new WfsXmlWriter.WFS1_0( wfs, output );
 *
 *  //declare application schema namespaces
 *  writer.getNamespaceSupport().declareNamespace( "cdf", "http://cite.opengeospatial.org/cite" );
 *
 *  //write the document
 *  writer.openTag( "wfs", "GetCapabilities" );
 *  ...
 *  //write the response
 *  writer.closeTag( "wfs", "GetCapabilities" );
 *  ....
 *
 *  //close the writer
 *  writer.close();
 * </pre>
 * </p>
 * @author Justin Deoliveira, The Open Planning Project
 *
 */
public abstract class WfsXmlWriter {
    /**
     * wfs configuration
     */
    WFS wfs;

    /**
     * The output stream
     */
    OutputStream output;

    /**
     * The character encoding.
     */
    String charSetEncoding;

    /**
     * Declared namespaces
     */
    NamespaceSupport namespaceSupport;

    /**
     * HashMap schemaLocations
     */
    HashMap schemaLocations;

    /**
     * The wfs version
     */
    String version;

    /**
     * writer
     */
    BufferedWriter writer;

    public WfsXmlWriter(WFS wfs, OutputStream output) {
        this.wfs = wfs;
        this.output = output;

        //default to wfs configured charset
        charSetEncoding = wfs.getCharSet().displayName();

        //schema locations
        schemaLocations = new HashMap();

        //declare the namespaces ( wfs is default )
        namespaceSupport = new NamespaceSupport();
        namespaceSupport.declarePrefix("xs", XS.NAMESPACE);
        namespaceSupport.declarePrefix("ogc", OGC.NAMESPACE);
        namespaceSupport.declarePrefix("gml", GML.NAMESPACE);
        ;
        namespaceSupport.declarePrefix("wfs", org.geoserver.wfs.xml.v1_0_0.WFS.NAMESPACE);
        namespaceSupport.declarePrefix("", org.geoserver.wfs.xml.v1_0_0.WFS.NAMESPACE);
    }

    public void setCharSetEncoding(String charSetEncoding) {
        this.charSetEncoding = charSetEncoding;
    }

    public NamespaceSupport getNamespaceSupport() {
        return namespaceSupport;
    }

    private void init() throws IOException {
        //namespace declarations
        for (Enumeration e = namespaceSupport.getDeclaredPrefixes(); e.hasMoreElements();) {
            String pre = (String) e.nextElement();
            String uri = namespaceSupport.getURI(pre);

            if ("".equals(pre)) {
                attribute("xmlns" + pre, uri);
            } else {
                attribute("xmlns:" + pre, uri);
            }
        }

        //schema locations
        if (!schemaLocations.isEmpty()) {
            StringBuffer buffer = new StringBuffer();

            for (Iterator e = schemaLocations.entrySet().iterator(); e.hasNext();) {
                Map.Entry entry = (Entry) e.next();
                String uri = (String) entry.getKey();
                String location = (String) entry.getValue();

                buffer.append(uri + " " + location);

                if (e.hasNext()) {
                    buffer.append(" ");
                }
            }

            attribute("xs:schemaLocation", buffer.toString());
        }
    }

    private void attribute(String name, String value) throws IOException {
        writer.write(" " + name + "=\"" + value + "\"");
    }

    public void openTag(String prefix, String name) throws IOException {
        openTag(prefix, name, null);
    }

    public void openTag(String prefix, String name, String[] attributes)
        throws IOException {
        boolean root = writer == null;

        if (root) {
            writer = new BufferedWriter(new OutputStreamWriter(output));

            //write the processing instruction
            writer.write("<?xml version=\"1.0\" encoding=\"" + charSetEncoding + "\"?>");
        }

        //write the element
        if (prefix != null) {
            writer.write("<" + prefix + ":" + name);
        } else {
            writer.write("<" + name);
        }

        if (root) {
            init();
        }

        if (attributes != null) {
            for (int i = 0; i < attributes.length; i += 2) {
                attribute(attributes[i], attributes[i + 1]);
            }
        }

        writer.write(">");
    }

    public void text(String text) throws IOException {
        writer.write(text);
    }

    public void closeTag(String prefix, String name) throws IOException {
        //write the element
        if (prefix != null) {
            writer.write("</" + prefix + ":" + name + ">");
        } else {
            writer.write("</" + name + ">");
        }
    }

    public void close() throws IOException {
        //close the writer
        writer.flush();
        writer.close();

        writer = null;
    }

    public static class WFS1_0 extends WfsXmlWriter {
        public WFS1_0(WFS wfs, OutputStream output) {
            super(wfs, output);

            //set the schema location
            schemaLocations.put(org.geoserver.wfs.xml.v1_0_0.WFS.NAMESPACE,
                ResponseUtils.appendPath(wfs.getSchemaBaseURL(), "wfs/1.0.0/WFS-transaction.xsd"));

            version = "1.0.0";
        }
    }

    public static class WFS1_1 extends WfsXmlWriter {
        public WFS1_1(WFS wfs, OutputStream output) {
            super(wfs, output);

            //add the ows namespace
            namespaceSupport.declarePrefix("ows", OWS.NAMESPACE);

            //set the schema location
            schemaLocations.put(org.geoserver.wfs.xml.v1_1_0.WFS.NAMESPACE,
                ResponseUtils.appendPath(wfs.getSchemaBaseURL(), "wfs/1.1.0/wfs.xsd"));

            version = "1.1.0";
        }
    }
}

⌨️ 快捷键说明

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