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

📄 mockdata.java

📁 电子地图服务器,搭建自己的地图服务
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    
    /**
     * Adds a new feature type.
     * <p>
     * Note that callers of this code should call <code>applicationContext.refresh()</code>
     * in order to force the catalog to reload.
     * </p>
     * <p>
     * The feature type is "empty", in that it has no attributes and no data.
     * Use {@link #addFeatureType(QName, InputStream)} to add a feature type 
     * with data.
     * </p>
     * 
     */
    public void addFeatureType( QName name ) throws IOException {
        
        addFeatureType( name, new ByteArrayInputStream( "-=".getBytes() ) );
    }
    
    /**
     * Adds a new feature type.
     * <p>
     * Note that callers of this code should call <code>applicationContext.refresh()</code>
     * in order to force the catalog to reload.
     * </p>
     * <p>
     * The <tt>properties</tt> parameter is an input stream containing the feature
     * type info and data to be used by property datastore.
     * </p>
     * 
     */
    public void addFeatureType( QName name, InputStream properties ) throws IOException {
        File directory = new File(data, name.getPrefix());
        if ( !directory.exists() ) {
            directory.mkdir();    
        }
        
        //create the properties file
        File f = new File(directory, name.getLocalPart() + ".properties");
        
        //copy over the contents
        copy( properties, f );
        info( name );
        
    }
    
    public void setUp() throws IOException {
        setUp(TYPENAMES);
    }

    /**
     * Sets up the data directory, creating all the necessary files.
     *
     * @throws IOException
     */
    public void setUp(QName[] typeNames) throws IOException {
        data.delete();
        data.mkdir();

        // create a featureTypes directory
        featureTypes = new File(data, "featureTypes");
        featureTypes.mkdir();

        // create the styles directory
        styles = new File(data, "styles");
        styles.mkdir();
        //copy over the minimal style and population
        copy(MockData.class.getResourceAsStream("Default.sld"), new File(styles, "Default.sld"));
        copy(MockData.class.getResourceAsStream("Population.sld"), new File(styles, "Population.sld"));

        //plugins
        plugIns = new File(data, "plugIns");
        plugIns.mkdir();

        //validation
        validation = new File(data, "validation");
        validation.mkdir();

        //templates
        templates = new File(data, "templates");
        templates.mkdir();

        //set up the types
        for (int i = 0; i < typeNames.length; i++) {
            setup(typeNames[i]);
        }

        // create the catalog.xml
        CatalogWriter writer = new CatalogWriter();

        // set up the datastores
        HashMap dataStores = new HashMap();

        HashMap params = new HashMap();
        params.put(PropertyDataStoreFactory.DIRECTORY.key, new File(data, CITE_PREFIX));
        params.put(PropertyDataStoreFactory.NAMESPACE.key, CITE_URI);
        dataStores.put(CITE_PREFIX, params);

        params = new HashMap();
        params.put(PropertyDataStoreFactory.DIRECTORY.key, new File(data, CDF_PREFIX));
        params.put(PropertyDataStoreFactory.NAMESPACE.key, CDF_URI);
        dataStores.put(CDF_PREFIX, params);

        params = new HashMap();
        params.put(PropertyDataStoreFactory.DIRECTORY.key, new File(data, CGF_PREFIX));
        params.put(PropertyDataStoreFactory.NAMESPACE.key, CGF_URI);
        dataStores.put(CGF_PREFIX, params);

        params = new HashMap();
        params.put(PropertyDataStoreFactory.DIRECTORY.key, new File(data, SF_PREFIX));
        params.put(PropertyDataStoreFactory.NAMESPACE.key, SF_URI);
        dataStores.put(SF_PREFIX, params);

        HashMap dataStoreNamepaces = new HashMap();
        dataStoreNamepaces.put(CITE_PREFIX, CITE_PREFIX);
        dataStoreNamepaces.put(CDF_PREFIX, CDF_PREFIX);
        dataStoreNamepaces.put(CGF_PREFIX, CGF_PREFIX);
        dataStoreNamepaces.put(SF_PREFIX, SF_PREFIX);

        writer.dataStores(dataStores, dataStoreNamepaces);

        // setup the namespaces
        HashMap namespaces = new HashMap();
        namespaces.put(CITE_PREFIX, CITE_URI);
        namespaces.put(CDF_PREFIX, CDF_URI);
        namespaces.put(CGF_PREFIX, CGF_URI);
        namespaces.put(SF_PREFIX, SF_URI);
        namespaces.put(DEFAULT_PREFIX, DEFAULT_URI);
        namespaces.put("", DEFAULT_URI);

        writer.namespaces(namespaces);

        // styles
        HashMap styles = new HashMap();

        List typeList = Arrays.asList(typeNames);
        for (int i = 0; i < WMS_TYPENAMES.length; i++) {
            if(typeList.contains(WMS_TYPENAMES[i])) {
                QName type = WMS_TYPENAMES[i];
                styles.put(type.getLocalPart(), type.getLocalPart() + ".sld");
            }
        }
        styles.put("Population", "Population.sld");
        styles.put("Default", "Default.sld");

        writer.styles(styles);

        writer.write(new File(data, "catalog.xml"));
    }

    void setup(QName type) throws IOException {
        properties(type);
        info(type);
        style(type);
    }

    void properties(QName name) throws IOException {
        // copy over the properties file
        InputStream from = MockData.class.getResourceAsStream(name.getLocalPart() + ".properties");
        
        File directory = new File(data, name.getPrefix());
        directory.mkdir();

        File to = new File(directory, name.getLocalPart() + ".properties");
        copy(from, to);     
    }

    void copy(InputStream from, File to) throws IOException {
        OutputStream out = new FileOutputStream(to);

        byte[] buffer = new byte[4096];
        int bytes = 0;
        while ((bytes = from.read(buffer)) != -1)
            out.write(buffer, 0, bytes);

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

    void info(QName name) throws IOException {
        String type = name.getLocalPart();
        String prefix = name.getPrefix();

        File featureTypeDir = new File(featureTypes, prefix + "_" + type);
        featureTypeDir.mkdir();

        File info = new File(featureTypeDir, "info.xml");
        info.createNewFile();

        FileWriter writer = new FileWriter(info);
        writer.write("<featureType datastore=\"" + prefix + "\">");
        writer.write("<name>" + type + "</name>");
        writer.write("<SRS>4326</SRS>");
        // this mock type may have wrong SRS compared to the actual one in the property files...
        // let's configure SRS handling not to alter the original one, and have 4326 used only
        // for capabilities
        writer.write("<SRSHandling>2</SRSHandling>");
        writer.write("<title>" + type + "</title>");
        writer.write("<abstract>abstract about " + type + "</abstract>");
        writer.write("<numDecimals value=\"8\"/>");
        writer.write("<keywords>" + type + "</keywords>");
        writer.write(
            "<latLonBoundingBox dynamic=\"false\" minx=\"-180\" miny=\"-90\" maxx=\"180\" maxy=\"90\"/>");

        if (MockData.class.getResource(type + ".sld") != null) {
            writer.write("<styles default=\"" + type + "\"/>");
        } else {
            writer.write("<styles default=\"Default\"/>");
        }

        writer.write("</featureType>");

        writer.flush();
        writer.close();
    }

    void style(QName name) throws IOException {
        String type = name.getLocalPart();

        //if there is not  astyle named "type".sld, use minimal
        InputStream from = null;

        if (MockData.class.getResource(type + ".sld") != null) {
            from = MockData.class.getResourceAsStream(type + ".sld");
        } else {
            from = MockData.class.getResourceAsStream("Default.sld");
        }

        File to = new File(styles, type + ".sld");
        copy(from, to);
    }

    /**
     * Kills the data directory, deleting all the files.
     *
     * @throws IOException
     */
    public void tearDown() throws IOException {
        delete(templates);
        delete(validation);
        delete(plugIns);
        delete(styles);
        delete(featureTypes);
        delete(data);

        styles = null;
        featureTypes = null;
        data = null;
    }

    void delete(File dir) throws IOException {
        File[] files = dir.listFiles();

        for (int i = 0; i < files.length; i++) {
            if (files[i].isDirectory()) {
                delete(files[i]);
            } else {
                files[i].delete();
            }
        }

        dir.delete();
    }
}

⌨️ 快捷键说明

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