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

📄 mockutils.java

📁 电子地图服务器,搭建自己的地图服务
💻 JAVA
📖 第 1 页 / 共 2 页
字号:

        Map nameSpaces = createNameSpaces();
        dataDto.setNameSpaces(nameSpaces);
        dataDto.setDefaultNameSpacePrefix("cite");

        Map styles = createStyles(new File(dir, "styles"));
        dataDto.setStyles(styles);

        Data catalog = new Data(dataDto, dir, geoserver);

        return catalog;
    }

    private static Map createDataStoresMap() throws IOException {
        Map map = new HashMap();
        DataStoreInfoDTO dsDto = new DataStoreInfoDTO();
        dsDto.setAbstract("test cite data for unit testing geoserver");
        dsDto.setEnabled(true);
        dsDto.setId("cite");
        dsDto.setNameSpaceId("cite");
        dsDto.setTitle("same as abstract");

        final File envTmpDir = new File(System.getProperty("java.io.tmpdir"));
        File tempDir = new File(envTmpDir, "cite_test_datastore");
        createCiteDataStore(tempDir);

        Map dsConnectionParams = new HashMap();
        dsConnectionParams.put("directory", tempDir);
        dsDto.setConnectionParams(dsConnectionParams);

        map.put("cite", dsDto);

        return map;
    }

    private static Map createFeatureTypes() {
        Map map = new HashMap();

        FeatureTypeInfoDTO ftDto;

        for (int i = 0; i < AbstractCiteDataTest.CITE_TYPE_NAMES.length; i++) {
            String typeName = AbstractCiteDataTest.CITE_TYPE_NAMES[i];
            ftDto = new FeatureTypeInfoDTO();
            ftDto.setAbstract(typeName + " abstract");
            ftDto.setDataStoreId("cite");
            ftDto.setDefaultStyle(typeName);
            ftDto.setDirName(null);
            ftDto.setName(typeName);
            ftDto.setSRS(4326);
            ftDto.setTitle("title for " + typeName);

            map.put(typeName, ftDto);
        }

        return map;
    }

    private static Map createNameSpaces() {
        Map map = new HashMap();
        NameSpaceInfoDTO ns = new NameSpaceInfoDTO();
        ns.setDefault(true);
        ns.setPrefix("cite");
        ns.setUri("http://www.axios.es");
        map.put("cite", ns);

        return map;
    }

    private static Map createStyles(File baseDir) {
        Map map = new HashMap();

        StyleDTO dto = new StyleDTO();
        dto.setDefault(false);
        dto.setFilename(new File(baseDir, "default.sld"));
        dto.setId("default");
        map.put("default", dto);

        for (int i = 0; i < AbstractCiteDataTest.CITE_TYPE_NAMES.length; i++) {
            String typeName = AbstractCiteDataTest.CITE_TYPE_NAMES[i];
            String sldName = typeName + ".sld";
            File sldFile = new File(baseDir, sldName);

            if (!sldFile.exists()) {
                System.err.println("Style file not found, unsing default.sld: " + sldFile);

                continue;
            }

            dto = new StyleDTO();
            dto.setDefault(false);
            dto.setFilename(sldFile);
            dto.setId(typeName);
            map.put(typeName, dto);
        }

        return map;
    }

    /**
     * Returns a <code>DataStore</code> containing CITE feature types.
     *
     * @return a property files backed DataStore which forces all the
     *         FeatureTypes it serves to be in WGS84 CRS.
     *
     * @throws IOException
     *             DOCUMENT ME!
     */
    public static DataStore createCiteDataStore(File tempDir)
        throws IOException {
        writeTempFiles(tempDir);

        DataStore propsDS = new ForceWGS84PropertyDataStore(tempDir);

        return propsDS;
    }

    /**
     * DOCUMENT ME!
     *
     * @throws IOException
     *             DOCUMENT ME!
     */
    private static void writeTempFiles(File tempDir) throws IOException {
        if (tempDir.exists()) {
            tempDir.delete();
        }

        tempDir.mkdir();
        

        if (!tempDir.exists() || !tempDir.isDirectory()) {
            throw new IOException(tempDir.getAbsolutePath() + " is not a writable directory");
        }

        for (int i = 0; i < AbstractCiteDataTest.CITE_TYPE_NAMES.length; i++) {
            writeTempFile(tempDir, AbstractCiteDataTest.CITE_TYPE_NAMES[i]);
        }
        tempDir.deleteOnExit();
    }

    /**
     * DOCUMENT ME!
     *
     * @param typeName
     *            DOCUMENT ME!
     *
     * @throws IOException
     *             DOCUMENT ME!
     * @throws NullPointerException
     *             DOCUMENT ME!
     */
    private static void writeTempFile(File tempDir, final String typeName)
        throws IOException {
        final String fileName = typeName + ".properties";

        File outFile = new File(tempDir, fileName);

        // perhaps it was not deleted in a previous, broken run...
        deleteTempFile(tempDir, typeName);

        // Atomically creates a new, empty file named by this abstract
        // pathname if and only if a file with this name does not yet exist.
        outFile.createNewFile();

        // Request that the file or directory denoted by this abstract
        // pathname be deleted when the virtual machine terminates.
        outFile.deleteOnExit();

        String resourceName = "test-data/featureTypes/" + fileName;

        InputStream in = MockUtils.class.getResourceAsStream(resourceName);

        if (in == null) {
            throw new NullPointerException(resourceName + " not found in classpath");
        }

        OutputStream out = new java.io.FileOutputStream(outFile);
        byte[] buff = new byte[512];
        int count;

        while ((count = in.read(buff)) > -1) {
            out.write(buff, 0, count);
        }

        in.close();
        out.flush();
        out.close();
    }

    /**
     * DOCUMENT ME!
     *
     * @param typeName
     *            DOCUMENT ME!
     */
    private static void deleteTempFile(File tempDir, String typeName) {
        deleteTempFile(new File(tempDir, typeName + ".properties"));
    }

    /**
     * DOCUMENT ME!
     *
     * @param f
     *            DOCUMENT ME!
     */
    private static void deleteTempFile(File f) {
        if(f.exists() && !f.delete())
            throw new RuntimeException("Could not delete file " + f);
    }

    /**
     * DOCUMENT ME!
     *
     * @author Gabriel Roldan, Axios Engineering
     * @version $Id: MockUtils.java 7349 2007-08-02 11:06:37Z aaime $
     */
    private static class ForceWGS84PropertyDataStore extends PropertyDataStore {
        /**
         * Creates a new ForceWGS84PropertyDataStore object.
         *
         * @param dir
         *            DOCUMENT ME!
         */
        public ForceWGS84PropertyDataStore(File dir) {
            super(dir);
        }

        /**
         * DOCUMENT ME!
         *
         * @param typeName
         *            DOCUMENT ME!
         *
         * @return DOCUMENT ME!
         *
         * @throws IOException
         *             DOCUMENT ME!
         * @throws DataSourceException
         *             DOCUMENT ME!
         */
        public FeatureType getSchema(String typeName) throws IOException {
            FeatureType schema = super.getSchema(typeName);

            try {
                return DataUtilities.createSubType(schema, null, DefaultGeographicCRS.WGS84);
            } catch (SchemaException e) {
                throw new DataSourceException(e.getMessage(), e);
            }
        }

        /**
         * DOCUMENT ME!
         */

        /*
         * public FeatureReader getFeatureReader(Query query, Transaction
         * transaction) throws IOException { FeatureReader reader =
         * super.getFeatureReader(query, transaction); try { return new
         * ForceCoordinateSystemFeatureReader(reader,
         * AbstractCiteDataTest.FORCED_WGS84); } catch (SchemaException e) {
         * throw new DataSourceException(e.getMessage(), e); } }
         */
    }
}

⌨️ 快捷键说明

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