Example usage for java.io File toURL

List of usage examples for java.io File toURL

Introduction

In this page you can find the example usage for java.io File toURL.

Prototype

@Deprecated
public URL toURL() throws MalformedURLException 

Source Link

Document

Converts this abstract pathname into a file: URL.

Usage

From source file:org.geoserver.wfs.response.Ogr2OgrOutputFormat.java

private File writeShapefile(File tempDir, SimpleFeatureCollection collection) {
    SimpleFeatureType schema = collection.getSchema();

    SimpleFeatureStore fstore = null;// w ww.  j  ava2  s  . c  om
    DataStore dstore = null;
    File file = null;
    try {
        file = new File(tempDir, schema.getTypeName() + ".shp");
        dstore = new ShapefileDataStore(file.toURL());
        dstore.createSchema(schema);

        fstore = (SimpleFeatureStore) dstore.getFeatureSource(schema.getTypeName());
        fstore.addFeatures(collection);
    } catch (IOException ioe) {
        LOGGER.log(Level.WARNING,
                "Error while writing featuretype '" + schema.getTypeName() + "' to shapefile.", ioe);
        throw new ServiceException(ioe);
    } finally {
        if (dstore != null) {
            dstore.dispose();
        }
    }

    return file;
}

From source file:org.codehaus.enunciate.modules.jaxws.TestJAXWSSupportDeploymentModule.java

/**
 * Create the wrapper class for the given web messgae.
 *
 * @param message The web message./*from ww w .  ja  v a  2  s  .c o  m*/
 * @param templateURL The url for the template to use to create the wrapper class.
 * @param beanFQN The fqn of the bean.
 * @return The wrapper class.
 */
protected Class createWrapperClass(WebMessage message, URL templateURL, String beanFQN) throws Exception {
    setupDefaultModel();

    //generate the java source file.
    Enunciate enunciate = new Enunciate(new String[0]);
    File genDir = enunciate.createTempDir();
    enunciate.setGenerateDir(genDir);
    JAXWSSupportDeploymentModule module = new JAXWSSupportDeploymentModule() {
        @Override
        protected EnunciateFreemarkerModel getModelInternal() {
            return (EnunciateFreemarkerModel) FreemarkerModel.get();
        }
    };
    module.init(enunciate);
    EnunciateFreemarkerModel model = module.getModel();
    model.put("file", new SpecifiedOutputDirectoryFileTransform(genDir));
    model.put("message", message);
    model.put("Introspector",
            BeansWrapper.getDefaultInstance().getStaticModels().get("java.beans.Introspector"));
    module.processTemplate(templateURL, model);

    Collection<String> srcFiles = enunciate.getJavaFiles(genDir);
    assertEquals("The wrapper bean should have been generated.", 1, srcFiles.size());
    srcFiles.addAll(getAllJavaFiles(getSamplesDir()));
    File buildDir = enunciate.createTempDir();
    enunciate.invokeJavac(getInAPTClasspath(), buildDir, srcFiles.toArray(new String[srcFiles.size()]));
    URLClassLoader loader = new URLClassLoader(new URL[] { buildDir.toURL() }, getClass().getClassLoader());
    Class generatedClass = Class.forName(beanFQN, true, loader);
    assertNotNull(generatedClass);

    return generatedClass;
}

From source file:org.codehaus.mojo.cobertura.tasks.AbstractTask.java

private String getLog4jConfigFile() {
    String resourceName = "cobertura-plugin/log4j-info.properties";
    if (getLog().isDebugEnabled()) {
        resourceName = "cobertura-plugin/log4j-debug.properties";
    }/*w ww. ja  va 2 s .  c  o  m*/
    if (quiet) {
        resourceName = "cobertura-plugin/log4j-error.properties";
    }

    String path = null;
    try {
        File log4jconfigFile = File.createTempFile("log4j", "config.properties");
        URL log4jurl = this.getClass().getClassLoader().getResource(resourceName);
        FileUtils.copyURLToFile(log4jurl, log4jconfigFile);
        log4jconfigFile.deleteOnExit();
        path = log4jconfigFile.toURL().toExternalForm();
    } catch (MalformedURLException e) {
        // ignore
    } catch (IOException e) {
        // ignore
    }
    return path;
}

From source file:org.apache.cocoon.generation.XPathDirectoryGenerator.java

/**
 * Performs an XPath query on the file.//from ww  w  . jav a  2 s  .com
 *
 * @param xmlFile the File the XPath is performed on.
 *
 * @throws SAXException if something goes wrong while adding the XML snippet.
 */
protected void performXPathQuery(File xmlFile) throws SAXException {
    this.doc = null;

    Source source = null;

    try {
        source = resolver.resolveURI(xmlFile.toURL().toExternalForm());
        this.doc = this.parser.parseDocument(SourceUtil.getInputSource(source));
    } catch (SAXException e) {
        getLogger().error("Warning:" + xmlFile.getName() + " is not a valid XML file. Ignoring.", e);
    } catch (ProcessingException e) {
        getLogger().error("Warning: Problem while reading the file " + xmlFile.getName() + ". Ignoring.", e);
    } catch (IOException e) {
        getLogger().error("Warning: Problem while reading the file " + xmlFile.getName() + ". Ignoring.", e);
    } finally {
        resolver.release(source);
    }

    if (doc != null) {
        NodeList nl = (null == this.prefixResolver)
                ? this.processor.selectNodeList(this.doc.getDocumentElement(), this.xpath)
                : this.processor.selectNodeList(this.doc.getDocumentElement(), this.xpath, this.prefixResolver);
        AttributesImpl attributes = new AttributesImpl();
        attributes.addAttribute("", QUERY_ATTR_NAME, QUERY_ATTR_NAME, "CDATA", xpath);
        super.contentHandler.startElement(URI, XPATH_NODE_NAME, PREFIX + ":" + XPATH_NODE_NAME, attributes);

        DOMStreamer ds = new DOMStreamer(super.xmlConsumer);

        for (int i = 0; i < nl.getLength(); i++) {
            ds.stream(nl.item(i));
        }

        super.contentHandler.endElement(URI, XPATH_NODE_NAME, PREFIX + ":" + XPATH_NODE_NAME);
    }
}

From source file:org.onecmdb.utils.wsdl.OneCMDBTransform.java

private IBeanProvider getInstanceProvider() throws Exception {
    if (this.transform == null) {
        throw new IllegalArgumentException("No transform is set!");
    }/*from   w  ww  . jav  a2s.c o m*/

    IBeanProvider provider = null;

    if ("simple".equalsIgnoreCase(transformType)) {
        SimpleTransformProvider simpleProvider = new SimpleTransformProvider();
        simpleProvider.setInput(this.transform);
        simpleProvider.setType(getSourceType());
        provider = simpleProvider;
    } else {
        XmlParser parser = new XmlParser();
        File f = new File(this.transform);
        parser.addURL(f.toURL().toExternalForm());

        if (this.model != null) {
            f = new File(this.model);
            parser.addURL(f.toURL().toExternalForm());
        }
        provider = parser;
    }
    return (provider);
}

From source file:org.fusesource.meshkeeper.distribution.PluginClassLoader.java

public void loadArtifact(String artifactId) throws IOException, Exception {
    List<File> resolved = null;
    synchronized (RESOLVED_PLUGINS) {
        if (!RESOLVED_PLUGINS.containsKey(artifactId)) {
            LOG.info("Resolving plugin: " + artifactId);
            resolved = getPluginResolver().resolvePlugin(artifactId);
            RESOLVED_PLUGINS.put(artifactId, resolved);
            LOG.info("Resolved plugin: " + artifactId);
        } else {//from   w  w  w.j  a  v a2  s .co m
            resolved = RESOLVED_PLUGINS.get(artifactId);
        }
    }

    for (File f : resolved) {
        if (resolvedFiles.add(f.getCanonicalPath())) {
            LOG.debug("Adding plugin dependency: " + f.getCanonicalPath());
            addUrl(f.toURL());
        }
    }

}

From source file:servletunit.ServletContextSimulator.java

/**
 * TODO: add appropriate comments/*w  w  w.  ja v a  2 s.c o  m*/
 */
public URL getResource(String path) throws MalformedURLException {
    try {
        File file = getResourceAsFile(path);

        if (file.exists()) {
            return file.toURL();
        } else {
            if (!path.startsWith("/")) {
                path = "/" + path;
            }
            return this.getClass().getResource(path);
        }
    } catch (Exception e) {
        return null;
    }
}

From source file:com.sshtools.j2ssh.util.ExtensionClassLoader.java

private URL findResourceInDirectory(File dir, String name) {
    // Name of resources are always separated by /
    String fileName = name.replace('/', File.separatorChar);
    File resFile = new File(dir, fileName);

    if (resFile.exists()) {
        try {//from  w ww .  j  a va 2s  .com
            return resFile.toURL();
        } catch (MalformedURLException ex) {
            return null;
        }
    } else {
        return null;
    }

}

From source file:org.gitools.matrix.format.TdmMatrixFormat.java

private MTabixIndex readMtabixIndex(IResourceLocator resourceLocator, IProgressMonitor progressMonitor)
        throws IOException, URISyntaxException {

    // Check if we are using mtabix
    URL dataURL = resourceLocator.getURL();

    URL indexURL = null;//from www . ja  v  a2s.c  o m

    if (!dataURL.getPath().endsWith("zip")) {
        IResourceLocator mtabix = resourceLocator.getReferenceLocator(resourceLocator.getName() + ".gz.mtabix");
        indexURL = mtabix.getURL();
    } else {
        //ZipFile zipFile = new ZipFile(new File(dataURL.toURI()));
        ZipFile zipFile = new ZipFile(resourceLocator.getReadFile());
        ZipEntry entry = zipFile.getEntry(resourceLocator.getName() + ".gz.mtabix");

        if (entry == null) {
            return null;
        }

        // Copy index to a temporal file
        File indexFile = File.createTempFile("gitools-cache-", "zip_mtabix");
        indexFile.deleteOnExit();
        IOUtils.copy(zipFile.getInputStream(entry), new FileOutputStream(indexFile));
        indexURL = indexFile.toURL();

        // Copy data to a temporal file
        File dataFile = File.createTempFile("gitools-cache-", "zip_bgz");
        dataFile.deleteOnExit();

        InputStream dataStream = resourceLocator.getParentLocator().openInputStream(progressMonitor);

        IOUtils.copy(dataStream, new FileOutputStream(dataFile));
        dataURL = dataFile.toURL();

    }

    File dataFile = new File(dataURL.toURI());
    File indexFile = new File(indexURL.toURI());

    if (!indexFile.exists()) {
        return null;
    }

    MTabixConfig mtabixConfig = new MTabixConfig(dataFile, indexFile, new DefaultKeyParser(1, 0));
    MTabixIndex index = new MTabixIndex(mtabixConfig);
    index.loadIndex();

    return index;

}

From source file:org.apache.xml.security.test.interop.InteropTest.java

/**
 * Method verifyHMAC/*from ww w  .  j av  a 2 s. c  om*/
 *
 * @param filename
 * @param resolver
 * @param hmacKey
 *
 * @throws Exception
 */
public boolean verifyHMAC(String filename, ResourceResolverSpi resolver, boolean followManifests,
        byte[] hmacKey) throws Exception {

    File f = new File(filename);
    javax.xml.parsers.DocumentBuilder db = dbf.newDocumentBuilder();
    org.w3c.dom.Document doc = db.parse(new java.io.FileInputStream(f));
    Element nscontext = TestUtils.createDSctx(doc, "ds", Constants.SignatureSpecNS);
    Element sigElement = (Element) XPathAPI.selectSingleNode(doc, "//ds:Signature[1]", nscontext);
    XMLSignature signature = new XMLSignature(sigElement, f.toURL().toString());

    if (resolver != null) {
        signature.addResourceResolver(resolver);
    }
    signature.setFollowNestedManifests(followManifests);

    byte keybytes[] = hmacKey;
    javax.crypto.SecretKey sk = signature.createSecretKey(keybytes);

    return signature.checkSignatureValue(sk);
}