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

📄 svgwriter.java

📁 电子地图服务器,搭建自己的地图服务
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
                atts.remove("#BOUNDS");
                LOGGER.finer("Added BOUNDS handler decorator");
            }

            if (atts.size() > 0) {
                this.writerHandler = new AttributesSVGHandler(this.writerHandler);
                LOGGER.finer("Added ATTRIBUTES handler decorator");
            }
        }
    }

    /**
     * DOCUMENT ME!
     *
     * @param ft
     *
     * @throws IOException si algo ocurre escribiendo a <code>out</code>
     */
    public void writeFeature(Feature ft) throws IOException {
        writerHandler.startFeature(featureWriter, ft);
        writerHandler.startGeometry(featureWriter, ft);
        writerHandler.writeGeometry(featureWriter, ft);
        writerHandler.endGeometry(featureWriter, ft);
        writerHandler.endFeature(featureWriter, ft);
    }

    /**
     * DOCUMENT ME!
     *
     * @author $author$
     * @version $Revision: 1.5 $
     */
    public class SVGFeatureWriterHandler {
        /**
         * DOCUMENT ME!
         *
         * @param featureWriter DOCUMENT ME!
         * @param ft DOCUMENT ME!
         *
         * @throws IOException DOCUMENT ME!
         */
        public void startFeature(SVGFeatureWriter featureWriter, Feature ft)
            throws IOException {
            featureWriter.startElement(ft);
        }

        /**
         * DOCUMENT ME!
         *
         * @param featureWriter DOCUMENT ME!
         * @param ft DOCUMENT ME!
         *
         * @throws IOException DOCUMENT ME!
         */
        public void endFeature(SVGFeatureWriter featureWriter, Feature ft)
            throws IOException {
            featureWriter.endElement(ft);
        }

        /**
         * DOCUMENT ME!
         *
         * @param featureWriter DOCUMENT ME!
         * @param ft DOCUMENT ME!
         *
         * @throws IOException DOCUMENT ME!
         */
        public void startGeometry(SVGFeatureWriter featureWriter, Feature ft)
            throws IOException {
            featureWriter.startGeometry(ft.getDefaultGeometry());
        }

        /**
         * DOCUMENT ME!
         *
         * @param featureWriter DOCUMENT ME!
         * @param ft DOCUMENT ME!
         *
         * @throws IOException DOCUMENT ME!
         */
        public void writeGeometry(SVGFeatureWriter featureWriter, Feature ft)
            throws IOException {
            featureWriter.writeGeometry(ft.getDefaultGeometry());
        }

        /**
         * DOCUMENT ME!
         *
         * @param featureWriter DOCUMENT ME!
         * @param ft DOCUMENT ME!
         *
         * @throws IOException DOCUMENT ME!
         */
        public void endGeometry(SVGFeatureWriter featureWriter, Feature ft)
            throws IOException {
            featureWriter.endGeometry(ft.getDefaultGeometry());
        }
    }

    /**
     * DOCUMENT ME!
     *
     * @author $author$
     * @version $Revision: 1.5 $
     */
    public class CollectSVGHandler extends SVGFeatureWriterHandler {
        /** DOCUMENT ME! */
        private SVGFeatureWriter featureWriter;

        /**
         * Creates a new CollectSVGHandler object.
         *
         * @param featureWriter DOCUMENT ME!
         */
        public CollectSVGHandler(SVGFeatureWriter featureWriter) {
            this.featureWriter = featureWriter;
        }

        /**
         * DOCUMENT ME!
         *
         * @param ft DOCUMENT ME!
         *
         * @throws IOException DOCUMENT ME!
         */
        public void writeFeature(Feature ft) throws IOException {
            featureWriter.writeGeometry(ft.getDefaultGeometry());
            write('\n');
        }
    }

    /**
     * decorator handler that adds the feature id as the "id" attribute
     */
    public class FIDSVGHandler extends SVGFeatureWriterHandler {
        /** DOCUMENT ME! */
        private SVGFeatureWriterHandler handler;

        /**
         * Creates a new NormalSVGHandler object.
         *
         * @param handler DOCUMENT ME!
         */
        public FIDSVGHandler(SVGFeatureWriterHandler handler) {
            this.handler = handler;
        }

        /**
         * DOCUMENT ME!
         *
         * @param featureWriter DOCUMENT ME!
         * @param ft DOCUMENT ME!
         *
         * @throws IOException DOCUMENT ME!
         */
        public void startFeature(SVGFeatureWriter featureWriter, Feature ft)
            throws IOException {
            handler.startFeature(featureWriter, ft);
            write(" id=\"");

            try {
                write(ft.getID());
            } catch (IOException ex) {
                System.err.println("error getting fid from " + ft);
                throw ex;
            }

            write("\"");
        }
    }

    /**
     * decorator handler that adds the feature id as the "id" attribute
     */
    public class BoundsSVGHandler extends SVGFeatureWriterHandler {
        /** DOCUMENT ME! */
        private SVGFeatureWriterHandler handler;

        /**
         * Creates a new NormalSVGHandler object.
         *
         * @param handler DOCUMENT ME!
         */
        public BoundsSVGHandler(SVGFeatureWriterHandler handler) {
            this.handler = handler;
        }

        /**
         * DOCUMENT ME!
         *
         * @param featureWriter DOCUMENT ME!
         * @param ft DOCUMENT ME!
         *
         * @throws IOException DOCUMENT ME!
         */
        public void startFeature(SVGFeatureWriter featureWriter, Feature ft)
            throws IOException {
            handler.startFeature(featureWriter, ft);

            Geometry geom = ft.getDefaultGeometry();
            Envelope env = geom.getEnvelopeInternal();
            write(" bounds=\"");
            write(env.getMinX());
            write(' ');
            write(env.getMinY());
            write(' ');
            write(env.getMaxX());
            write(' ');
            write(env.getMaxY());
            write('\"');
        }
    }

    /**
     * decorator handler that adds the feature id as the "id" attribute
     */
    public class AttributesSVGHandler extends SVGFeatureWriterHandler {
        /** DOCUMENT ME! */
        private SVGFeatureWriterHandler handler;

        /**
         * Creates a new NormalSVGHandler object.
         *
         * @param handler DOCUMENT ME!
         */
        public AttributesSVGHandler(SVGFeatureWriterHandler handler) {
            this.handler = handler;
        }

        /**
         * DOCUMENT ME!
         *
         * @param featureWriter DOCUMENT ME!
         * @param ft DOCUMENT ME!
         *
         * @throws IOException DOCUMENT ME!
         */
        public void startFeature(SVGFeatureWriter featureWriter, Feature ft)
            throws IOException {
            handler.startFeature(featureWriter, ft);

            FeatureType type = ft.getFeatureType();
            int numAtts = type.getAttributeCount();
            String name;
            Object value;

            for (int i = 0; i < numAtts; i++) {
                value = ft.getAttribute(i);

                if ((value != null) && !(value instanceof Geometry)) {
                    write(' ');
                    write(type.getAttributeType(i).getName());
                    write("=\"");
                    encodeAttribute(String.valueOf(value));
                    write('\"');
                }
            }
        }

        /**
         * Parses the passed string, and encodes the special characters (used
         * in xml for special purposes) with the appropriate codes. e.g.
         * '&lt;' is changed to '&amp;lt;'
         *
         * @param inData The string to encode into xml.
         *
         * @throws IOException DOCUMENT ME!
         *
         * @task REVISIT: Once we write directly to out, as we should, this
         *       method should be simpler, as we can just write strings with
         *       escapes directly to out, replacing as we iterate of chars to
         *       write them.
         */
        private void encodeAttribute(String inData) throws IOException {
            //return null, if null is passed as argument
            if (inData == null) {
                return;
            }

            //get the length of input String
            int length = inData.length();

            char charToCompare;

            //iterate over the input String
            for (int i = 0; i < length; i++) {
                charToCompare = inData.charAt(i);

                //if the ith character is special character, replace by code
                if (charToCompare == '"') {
                    write("&quot;");
                } else if (charToCompare > 127) {
                    writeUnicodeEscapeSequence(charToCompare);
                } else {
                    write(charToCompare);
                }
            }
        }

        /**
         * returns the xml unicode escape sequence for the character
         * <code>c</code>, such as <code>"&#x00d1;"</code> for the character
         * <code>'?'</code>
         *
         * @param c DOCUMENT ME!
         *
         * @throws IOException DOCUMENT ME!
         */
        private void writeUnicodeEscapeSequence(char c)
            throws IOException {
            write("&#x");

            String hex = Integer.toHexString(c);
            int pendingZeros = 4 - hex.length();

            for (int i = 0; i < pendingZeros; i++)
                write('0');

            write(hex);
            write(';');
        }
    }

    /**
     * DOCUMENT ME!
     *
     * @author $author$
     * @version $Revision: 1.5 $
     */
    private abstract class SVGFeatureWriter {
        /**
         * DOCUMENT ME!
         * @param feature TODO
         *
         * @throws IOException DOCUMENT ME!
         */
        protected abstract void startElement(Feature feature)
            throws IOException;

        /**
         * DOCUMENT ME!
         * @param geom TODO
         *
         * @throws IOException DOCUMENT ME!
         */
        protected abstract void startGeometry(Geometry geom)
            throws IOException;

        /**
         * DOCUMENT ME!
         *
         * @param geom DOCUMENT ME!
         *
         * @throws IOException DOCUMENT ME!
         */
        protected abstract void writeGeometry(Geometry geom)
            throws IOException;

        /**
         * DOCUMENT ME!
         * @param geom TODO
         *
         * @throws IOException DOCUMENT ME!
         */
        protected void endGeometry(Geometry geom) throws IOException {
            write("\"");
        }

        /**
         * DOCUMENT ME!
         * @param feature TODO
         *
         * @throws IOException DOCUMENT ME!
         */
        protected void endElement(Feature feature) throws IOException {
            write("/>\n");
        }

        /**

⌨️ 快捷键说明

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