Example usage for javax.xml.namespace QName getPrefix

List of usage examples for javax.xml.namespace QName getPrefix

Introduction

In this page you can find the example usage for javax.xml.namespace QName getPrefix.

Prototype

public String getPrefix() 

Source Link

Document

Get the prefix of this QName.

The prefix assigned to a QName might NOT be valid in a different context.

Usage

From source file:org.geoserver.data.test.MockData.java

/**
 * Adds a property file as a feature type in a property datastore.
 * /*from   w  ww .  ja  va2  s .  c  o  m*/
 * @param name
 *            the fully qualified name of the feature type. The prefix and
 *            namespace URI will be used to create a namespace, the prefix
 *            will be used as the datastore name, the local name will become
 *            the feature type name
 * @param properties
 *            a URL to the property file backing the feature type. If null,
 *            an emtpy property file will be used
 * @param extraParams
 *            a map from extra configurable keys to their values (see for example
 * @throws IOException
 */
public void addPropertiesType(QName name, URL properties, Map extraParams) throws IOException {
    // sanitize input params
    if (extraParams == null)
        extraParams = Collections.EMPTY_MAP;

    // setup the type directory if needed
    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
    InputStream propertiesContents;
    if (properties == null)
        propertiesContents = new ByteArrayInputStream("-=".getBytes());
    else
        propertiesContents = properties.openStream();
    IOUtils.copy(propertiesContents, f);

    // write the info file
    info(name, extraParams);

    // setup the meta information to be written in the catalog 
    namespaces.put(name.getPrefix(), name.getNamespaceURI());
    dataStoreNamepaces.put(name.getPrefix(), name.getPrefix());
    Map params = new HashMap();
    params.put(PropertyDataStoreFactory.DIRECTORY.key, directory);
    params.put(PropertyDataStoreFactory.NAMESPACE.key, name.getNamespaceURI());
    dataStores.put(name.getPrefix(), params);
}

From source file:org.geoserver.data.test.MockData.java

/**
 * Adds a new coverage.//w ww.ja v a2 s  .  c  om
 *<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>coverage</tt> parameter is an input stream containing a single uncompressed
 * file that's supposed to be a coverage (e.g., a GeoTiff).
 * </p>
        
 * @param name
 * @param coverage
 */
public void addCoverage(QName name, URL coverage, String extension, String styleName) throws Exception {
    if (extension == null)
        throw new IllegalArgumentException("Use addCoverageFromZip instead of passing NULL");

    File directory = new File(data, name.getPrefix());
    if (!directory.exists()) {
        directory.mkdir();
    }

    // create the coverage file
    File f = new File(directory, name.getLocalPart() + "." + extension);

    IOUtils.copy(coverage.openStream(), f);

    addCoverageFromPath(name, f, "file:" + name.getPrefix() + "/" + name.getLocalPart() + "." + extension,
            styleName);
}

From source file:org.geoserver.data.test.MockData.java

public void addCoverageFromZip(QName name, URL coverage, String extension, String styleName) throws Exception {
    File directory = new File(data, name.getPrefix());
    if (!directory.exists()) {
        directory.mkdir();/*from   www.ja v a2s  .  c  om*/
    }

    File f = new File(directory, name.getLocalPart());
    f.mkdir();

    File compressedFile = new File(f, name.getLocalPart() + ".zip");
    IOUtils.copy(coverage.openStream(), compressedFile);
    IOUtils.decompress(compressedFile, f);
    final File srcDir = new File(f, name.getLocalPart());
    srcDir.mkdir();
    FileUtils.copyDirectory(srcDir, f, true);

    if (extension != null) {
        File coverageFile = new File(srcDir, name.getLocalPart() + "." + extension);
        addCoverageFromPath(name, coverageFile, "file:" + name.getPrefix() + "/" + name.getLocalPart() + "/"
                + name.getLocalPart() + "." + extension, styleName);
    } else {
        addCoverageFromPath(name, f, "file:" + name.getPrefix() + "/" + name.getLocalPart(), styleName);
    }
}

From source file:org.geoserver.data.test.MockData.java

private void addCoverageFromPath(QName name, File coverage, String relpath, String styleName) throws Exception {
    coverageInfo(name, coverage, styleName);

    // setup the meta information to be written in the catalog 
    AbstractGridFormat format = (AbstractGridFormat) GridFormatFinder.findFormat(coverage);
    namespaces.put(name.getPrefix(), name.getNamespaceURI());
    coverageStoresNamespaces.put(name.getLocalPart(), name.getPrefix());
    Map params = new HashMap();
    params.put(CatalogWriter.COVERAGE_TYPE_KEY, format.getName());
    params.put(CatalogWriter.COVERAGE_URL_KEY, relpath);
    coverageStores.put(name.getLocalPart(), params);
}

From source file:org.geoserver.data.test.MockData.java

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();//from   w w  w. jav a  2s .  c om

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

From source file:org.geoserver.data.test.MockData.java

void info(QName name, Map<String, Object> extraParams) throws IOException {
    String type = name.getLocalPart();
    String prefix = name.getPrefix();

    // prepare extra params default
    Map<String, Object> params = new HashMap<String, Object>();
    params.put(KEY_STYLE, "Default");
    params.put(KEY_ALIAS, null);//from   w w  w.ja v a2s. c o  m

    Integer srs = SRS.get(name);
    if (srs == null) {
        srs = 4326;
    }
    params.put(KEY_SRS_NUMBER, srs);

    // override with whatever the user provided
    params.putAll(extraParams);

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

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

    FileWriter writer = new FileWriter(info);
    writer.write("<featureType datastore=\"" + prefix + "\">");
    writer.write("<name>" + type + "</name>");
    if (params.get(KEY_ALIAS) != null)
        writer.write("<alias>" + params.get(KEY_ALIAS) + "</alias>");
    writer.write("<SRS>" + params.get(KEY_SRS_NUMBER) + "</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
    int srsHandling = 2;
    Object handling = params.get(KEY_SRS_HANDLINGS);
    if (handling != null) {
        if (handling instanceof ProjectionPolicy) {
            srsHandling = ((ProjectionPolicy) params.get(KEY_SRS_HANDLINGS)).getCode();
        } else if (handling instanceof Number) {
            srsHandling = ((Number) params.get(KEY_SRS_HANDLINGS)).intValue();
        }
    }
    writer.write("<SRSHandling>" + srsHandling + "</SRSHandling>");
    writer.write("<title>" + type + "</title>");
    writer.write("<abstract>abstract about " + type + "</abstract>");
    writer.write("<numDecimals value=\"8\"/>");
    writer.write("<keywords>" + type + "</keywords>");
    Envelope llEnvelope = (Envelope) params.get(KEY_LL_ENVELOPE);
    if (llEnvelope == null)
        llEnvelope = DEFAULT_ENVELOPE;
    writer.write("<latLonBoundingBox dynamic=\"false\" minx=\"" + llEnvelope.getMinX() + "\" miny=\""
            + llEnvelope.getMinY() + "\" maxx=\"" + llEnvelope.getMaxX() + "\" maxy=\"" + llEnvelope.getMaxY()
            + "\"/>");

    Envelope nativeEnvelope = (Envelope) params.get(KEY_NATIVE_ENVELOPE);
    if (nativeEnvelope != null)
        writer.write("<nativeBBox dynamic=\"false\" minx=\"" + nativeEnvelope.getMinX() + "\" miny=\""
                + nativeEnvelope.getMinY() + "\" maxx=\"" + nativeEnvelope.getMaxX() + "\" maxy=\""
                + nativeEnvelope.getMaxY() + "\"/>");

    String style = (String) params.get(KEY_STYLE);
    if (style == null)
        style = "Default";
    writer.write("<styles default=\"" + style + "\"/>");

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

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

From source file:org.geoserver.data.test.SystemTestData.java

/**
 * Adds a vector layer to the catalog setup.
 * <p>/*from  w ww  .  j  av  a 2 s  .  com*/
 * The layer is created within a store named <code>qName.getPrefix()</code>, creating it 
 * if it does not exist. The resulting store is a {@link PropertyDataStore} that points at the 
 * directory <code>getDataDirectoryRoot()/qName.getPrefix()</code>. Similarily the layer and
 * store are created within a workspace named <code>qName.getPrefix()</code>, which is created
 * if it does not already exist.
 * </p>
 * <p>
 * The properties data for the layer is copied from the classpath, with a file name of 
 * "<code>filename</code>.properties". The <tt>scope</tt> parameter is used as the 
 * class from which to load the properties file relative to. 
 * </p>
 * <p>
 * The <tt>props</tt> parameter is used to define custom properties for the layer. See the 
 * {@link LayerProperty} class for supported properties. 
 * </p>
 */
public void addVectorLayer(QName qName, Map<LayerProperty, Object> props, String filename, Class scope,
        Catalog catalog) throws IOException {
    String prefix = qName.getPrefix();
    String name = qName.getLocalPart();
    String uri = qName.getNamespaceURI();

    //configure workspace if it doesn;t already exist
    if (catalog.getWorkspaceByName(prefix) == null) {
        addWorkspace(prefix, uri, catalog);
    }

    //configure store if it doesn't already exist

    File storeDir = catalog.getResourceLoader().findOrCreateDirectory(prefix);

    DataStoreInfo store = catalog.getDataStoreByName(prefix);
    if (store == null) {
        store = catalog.getFactory().createDataStore();
        store.setName(prefix);
        store.setWorkspace(catalog.getWorkspaceByName(prefix));
        store.setEnabled(true);

        store.getConnectionParameters().put(PropertyDataStoreFactory.DIRECTORY.key, storeDir);
        store.getConnectionParameters().put(PropertyDataStoreFactory.NAMESPACE.key, uri);
        catalog.add(store);
    }

    //copy the properties file over

    catalog.getResourceLoader().copyFromClassPath(filename, new File(storeDir, filename), scope);

    //configure feature type
    FeatureTypeInfo featureType = catalog.getFactory().createFeatureType();
    featureType.setStore(store);
    featureType.setNamespace(catalog.getNamespaceByPrefix(prefix));
    featureType.setName(LayerProperty.NAME.get(props, name));
    featureType.setNativeName(FilenameUtils.getBaseName(filename));
    featureType.setTitle(name);
    featureType.setAbstract("abstract about " + name);

    Integer srs = LayerProperty.SRS.get(props, SRS.get(qName));
    if (srs == null) {
        srs = 4326;
    }
    featureType.setSRS("EPSG:" + srs);
    try {
        featureType.setNativeCRS(CRS.decode("EPSG:" + srs));
    } catch (Exception e) {
        LOGGER.warning("Failed to decode EPSG:" + srs + ", setting the native SRS to null");
    }
    featureType.setNumDecimals(8);
    featureType.getKeywords().add(new Keyword(name));
    featureType.setEnabled(true);
    featureType.setProjectionPolicy(LayerProperty.PROJECTION_POLICY.get(props, ProjectionPolicy.NONE));
    featureType.setLatLonBoundingBox(LayerProperty.LATLON_ENVELOPE.get(props, DEFAULT_LATLON_ENVELOPE));
    featureType.setNativeBoundingBox(LayerProperty.ENVELOPE.get(props, null));

    FeatureTypeInfo ft = catalog.getFeatureTypeByDataStore(store, name);
    LayerInfo layer = catalog.getLayerByName(new NameImpl(prefix, name));
    if (ft == null) {
        ft = featureType;
        catalog.add(featureType);
    } else {
        if (layer == null) {
            // handles the case of layer removed, but feature type not
            catalog.remove(ft);
            ft = featureType;
            catalog.add(featureType);
        } else {
            new CatalogBuilder(catalog).updateFeatureType(ft, featureType);
            catalog.save(ft);
        }
    }

    if (layer == null || !layer.getResource().getNamespace().equals(catalog.getNamespaceByPrefix(prefix))) {
        layer = catalog.getFactory().createLayer();
    }

    layer.setResource(ft);

    StyleInfo defaultStyle = null;
    if (LayerProperty.STYLE.get(props, null) != null) {
        defaultStyle = catalog.getStyleByName(LayerProperty.STYLE.get(props, null));
    } else {
        //look for a style matching the layer name
        defaultStyle = catalog.getStyleByName(name);
        if (defaultStyle == null) {
            //see if the resource exists and we just need to create it
            if (getClass().getResource(name + ".sld") != null) {
                addStyle(name, catalog);
                defaultStyle = catalog.getStyleByName(name);
            }
        }
    }

    if (defaultStyle == null) {
        defaultStyle = catalog.getStyleByName(DEFAULT_VECTOR_STYLE);
    }

    layer.getStyles().clear();
    layer.setDefaultStyle(defaultStyle);
    layer.setType(LayerInfo.Type.VECTOR);
    layer.setEnabled(true);

    if (layer.getId() == null) {
        catalog.add(layer);
    } else {
        catalog.save(layer);
    }
}

From source file:org.geoserver.data.test.SystemTestData.java

/**
 * Adds a raster layer to the setup./*from  w  w  w.java  2  s  .  co m*/
 * <p>
 * This method configures a raster layer with the name <code>qName.getLocalPart()</code>. A 
 * coverage store is created (if it doesn't already exist) with the same name. The workspace
 * of the resulting store and layer is determined by <code>qName.getPrefix()</code>.
 * </p>
 * <p>
 * The <tt>filename</tt> parameter defines the raster file to be loaded from the classpath 
 * and copied into the data directory. The <tt>scope</tt> is used as the class from which to 
 * load the file from.
 * </p>
 * <p>
 * In the case of adding a zipped archive that contains multiple file the <tt>filename</tt> 
 * paramter should have a ".zip" extension and the <tt>extension</tt> parameter must define the 
 * extension of the main raster file. The parameter is not necessary and may be null if the 
 * <tt>filename</tt> does not refer to a zip file.
 * </p>
 * <p>
 * The <tt>props</tt> parameter is used to define custom properties for the layer. See the 
 * {@link LayerProperty} class for supported properties. 
 * </p>
 * @param qName The name of the raster layer.
 * @param filename The name of the file containing the raster, to be loaded from the classpath.
 * @param extension The file extension (without a ".") of the main raster file. This parameter
 *   my be <code>null</code> only if <tt>filename</tt> does not refer to a zip file.
 * @param props Custom properties to assign to the created raster layer.
 * @param scope The class from which to load the <tt>filename</tt> resource from. 
 *
 */
public void addRasterLayer(QName qName, String filename, String extension, Map<LayerProperty, Object> props,
        Class scope, Catalog catalog) throws IOException {

    String prefix = qName.getPrefix();
    String name = qName.getLocalPart();

    //setup the data
    File dir = new File(data, name);
    dir.mkdirs();

    File file = new File(dir, filename);
    catalog.getResourceLoader().copyFromClassPath(filename, file, scope);

    String ext = FilenameUtils.getExtension(filename);
    if ("zip".equalsIgnoreCase(ext)) {

        //unpack the archive
        IOUtils.decompress(file, dir);

        //delete archive
        file.delete();

        if (extension == null) {
            //zip with no extension, we just the directory as the file
            file = dir;
        } else {
            //files may have been top level, or one directory level deep
            file = new File(dir, FilenameUtils.getBaseName(filename) + "." + extension);
            if (!file.exists()) {
                File file2 = new File(new File(dir, dir.getName()), file.getName());
                if (file2.exists()) {
                    file = file2;
                }
            }
        }

        if (!file.exists()) {
            throw new FileNotFoundException(file.getPath());
        }
    }

    //load the format/reader
    AbstractGridFormat format = (AbstractGridFormat) GridFormatFinder.findFormat(file);
    if (format == null) {
        throw new RuntimeException("No format for " + file.getCanonicalPath());
    }
    GridCoverage2DReader reader = null;
    try {
        reader = (GridCoverage2DReader) format.getReader(file);
        if (reader == null) {
            throw new RuntimeException(
                    "No reader for " + file.getCanonicalPath() + " with format " + format.getName());
        }

        //configure workspace if it doesn;t already exist
        if (catalog.getWorkspaceByName(prefix) == null) {
            addWorkspace(prefix, qName.getNamespaceURI(), catalog);
        }
        //create the store
        CoverageStoreInfo store = catalog.getCoverageStoreByName(prefix, name);
        if (store == null) {
            store = catalog.getFactory().createCoverageStore();
        }

        store.setName(name);
        store.setWorkspace(catalog.getWorkspaceByName(prefix));
        store.setEnabled(true);
        store.setURL(DataUtilities.fileToURL(file).toString());
        store.setType(format.getName());

        if (store.getId() == null) {
            catalog.add(store);
        } else {
            catalog.save(store);
        }

        //create the coverage
        CatalogBuilder builder = new CatalogBuilder(catalog);
        builder.setStore(store);

        CoverageInfo coverage = null;

        try {

            coverage = builder.buildCoverage(reader, null);
            // coverage read params
            if (format instanceof ImageMosaicFormat) {
                //  make sure we work in immediate mode
                coverage.getParameters().put(AbstractGridFormat.USE_JAI_IMAGEREAD.getName().getCode(),
                        Boolean.FALSE);
            }
        } catch (Exception e) {
            throw new IOException(e);
        }

        coverage.setName(name);
        coverage.setTitle(name);
        coverage.setDescription(name);
        coverage.setEnabled(true);

        CoverageInfo cov = catalog.getCoverageByCoverageStore(store, name);
        if (cov == null) {
            catalog.add(coverage);
        } else {
            builder.updateCoverage(cov, coverage);
            catalog.save(cov);
            coverage = cov;
        }

        LayerInfo layer = catalog.getLayerByName(new NameImpl(qName));
        if (layer == null) {
            layer = catalog.getFactory().createLayer();
        }
        layer.setResource(coverage);

        layer.setDefaultStyle(catalog.getStyleByName(LayerProperty.STYLE.get(props, DEFAULT_RASTER_STYLE)));
        layer.setType(LayerInfo.Type.RASTER);
        layer.setEnabled(true);

        if (layer.getId() == null) {
            catalog.add(layer);
        } else {
            catalog.save(layer);
        }
    } finally {
        if (reader != null) {
            reader.dispose();
        }
    }
}

From source file:org.geoserver.gss.HTTPGSSClient.java

String prefixedName(QName layerName) {
    return layerName.getPrefix() + ":" + layerName.getLocalPart();
}

From source file:org.geoserver.gss.HTTPGSSClient.java

public GetDiffResponseType getDiff(GetDiffType getDiff) throws IOException {
    // prepare the encoder
    Encoder encoder = new Encoder(configuration, configuration.getXSD().getSchema());
    QName layerName = getDiff.getTypeName();
    encoder.getNamespaces().declarePrefix(layerName.getPrefix(), layerName.getNamespaceURI());
    encoder.setEncoding(Charset.forName("UTF-8"));

    // prepare POST request
    PostMethod method = new PostMethod(address.toExternalForm());
    method.setContentChunked(true);/*  w  w  w  . j a  va  2s .  co  m*/
    method.setRequestEntity(new XMLEntity(getDiff, GSS.GetDiff, encoder));

    // execute the request and interpret the response
    Object response = executeMethod(method);
    if (response instanceof GetDiffResponseType) {
        return (GetDiffResponseType) response;
    } else {
        if (response == null) {
            throw new IOException("The response was parsed to a null object");
        }
        throw new IOException("The response was parsed to an unrecognized object type: " + response.getClass());
    }

}