Example usage for java.lang Class getResource

List of usage examples for java.lang Class getResource

Introduction

In this page you can find the example usage for java.lang Class getResource.

Prototype

@CallerSensitive
public URL getResource(String name) 

Source Link

Document

Finds a resource with a given name.

Usage

From source file:org.devproof.portal.core.module.modulemgmt.service.ModuleServiceImpl.java

private String getLocation(Class<?> clazz) {
    URL url = clazz.getResource("");
    if (!"jar".equals(url.getProtocol())) {
        return "WAR";
    } else {/*from   ww  w. j  a  va  2 s  . c  om*/
        String path = url.getPath();
        String strs[] = StringUtils.split(path, '/');
        for (String str : strs) {
            if (str.endsWith("!")) {
                return StringUtils.removeEnd(str, "!");
            }
        }
        return "unknown";
    }
}

From source file:org.dishevelled.piccolo.sprite.statemachine.AbstractStateMachineSprite.java

/**
 * Load the state machine resource with the specified name, if any.  Any exceptions thrown
 * will be ignored.//  w ww.j a v a  2  s.  c om
 *
 * @param cls class
 * @param name name
 * @return the state machine resource with the specified name, or <code>null</code>
 *    if no such resource exists
 */
protected static final <T> SCXML loadStateMachine(final Class<T> cls, final String name) {
    SCXML stateMachine = null;
    try {
        stateMachine = SCXMLParser.parse(cls.getResource(name), new SimpleErrorHandler());
    } catch (IOException e) {
        // ignore
    } catch (SAXException e) {
        // ignore
    } catch (ModelException e) {
        // ignore
    }
    return stateMachine;
}

From source file:org.dishevelled.piccolo.sprite.statemachine.AbstractStateMachineSprite.java

/**
 * Load the image resource with the specified name, if any.  Any exceptions thrown will be
 * ignored.//from   ww  w  . j  a  v a2  s  . c o  m
 *
 * @param cls class
 * @param name name
 * @return the image resource with the specified name, or <code>null</code> if no such
 *    resource exists
 */
protected static final <T> BufferedImage loadImage(final Class<T> cls, final String name) {
    BufferedImage image = null;
    try {
        image = ImageIO.read(cls.getResource(name));
    } catch (IOException e) {
        // ignore
    }
    return image;
}

From source file:org.dkpro.tc.core.util.SaveModelUtils.java

private static void persistsFeatureClassObject(TaskContext aContext, TcFeature f, File aOutputFolder)
        throws Exception {
    ExternalResourceDescription feDesc = f.getActualValue();

    String implName;/*from  w  w w .j  a v a 2 s.  c  o  m*/
    if (feDesc.getResourceSpecifier() instanceof CustomResourceSpecifier) {
        implName = ((CustomResourceSpecifier) feDesc.getResourceSpecifier()).getResourceClassName();
    } else {
        implName = feDesc.getImplementationName();
    }

    Class<?> feature = Class.forName(implName);

    InputStream inStream = feature.getResource("/" + implName.replace(".", "/") + ".class").openStream();

    OutputStream outStream = buildOutputStream(aOutputFolder, implName);

    IOUtils.copy(inStream, outStream);
    outStream.close();
    inStream.close();
}

From source file:org.dkpro.tc.core.util.SaveModelUtils.java

public static void writeFeatureClassFiles(File modelFolder, List<TcFeature> featureSet) throws Exception {
    for (TcFeature f : featureSet) {
        String featureString = f.getFeatureName();
        Class<?> feature = Class.forName(featureString);
        InputStream inStream = feature.getResource("/" + featureString.replace(".", "/") + ".class")
                .openStream();/*  www. j av a2  s  .c o  m*/

        OutputStream outStream = buildOutputStream(modelFolder, featureString);

        IOUtils.copy(inStream, outStream);
        outStream.close();
        inStream.close();

    }

}

From source file:org.dkpro.tc.core.util.SaveModelUtils.java

private static String getCurrentTcVersionFromWorkspace() throws Exception {
    Class<?> contextClass = SaveModelUtils.class;

    // Try to determine the location of the POM file belonging to the context object
    URL url = contextClass.getResource(contextClass.getSimpleName() + ".class");
    String classPart = contextClass.getName().replace(".", "/") + ".class";
    String base = url.toString();
    base = base.substring(0, base.length() - classPart.length());
    base = base.substring(0, base.length() - "target/classes/".length());
    File pomFile = new File(new File(URI.create(base)), "pom.xml");

    MavenXpp3Reader reader = new MavenXpp3Reader();
    Model model;//from w w  w .  ja  v a  2s  . c  o m
    model = reader.read(new FileInputStream(pomFile));
    String version = model.getParent().getVersion();

    return version;
}

From source file:org.ebayopensource.turmeric.runtime.tests.config.CompareUtils.java

public static String writeOutputFile(Class<?> clazz, StringBuffer output, String name) throws Exception {
    String compareFilename = name + ".compare.txt";
    String outputFilename = name + ".output.txt";
    URL compareURL = clazz.getResource(compareFilename);
    if (compareURL == null) {
        // Skip writeOutputFile as its not possible to find the file.
        System.out.println("## Resource Not Found: " + compareFilename);
        return null;
    }/*from  www  .  j av  a  2s.co  m*/
    if (compareURL.getProtocol().equals("file") == false) {
        // Can't write to non-file protocols anyway.
        System.out.println("## Not file protocol: " + compareURL.toExternalForm());
        return null;
    }
    BufferedWriter bw = null;
    try {
        File compareFile = new File(compareURL.toURI());
        File outputDir = compareFile.getParentFile();
        File outputFile = new File(outputDir, outputFilename);
        bw = new BufferedWriter(new FileWriter(outputFile));
        List<String> lines = StringUtils.splitStr(output.toString(), '\n', false);
        for (String line : lines) {
            bw.write(line);
            bw.newLine();
        }
        bw.close();
        return outputFile.getAbsolutePath();
    } catch (IllegalArgumentException e) {
        // We write the *.output.txt file for convenience in the local (Eclipse)
        // testing case.  When doing testing from a jar file under ICE, we do not need
        // this capability and it won't work to try to write output data, so skip this
        // logic.
    } finally {
        IOUtils.closeQuietly(bw);
    }
    return null;
}

From source file:org.echocat.velma.Resources.java

@Nonnull
protected List<URL> loadIconUrls(@Nonnull Class<?> inRelationTo) {
    final List<URL> result = new ArrayList<>();
    for (int size = 1; size <= 256; size++) {
        final URL resource = inRelationTo.getResource("icon." + size + "x" + size + ".png");
        if (resource != null) {
            result.add(resource);/*  ww  w. ja v a2  s  . c  om*/
        }
    }
    return unmodifiableList(result);
}

From source file:org.eclipse.buckminster.jnlp.MaterializationUtils.java

public static Image getImage(String imageName) {
    Class<?> myClass = MaterializationUtils.class;
    String imageResource = "/icons/" + imageName; //$NON-NLS-1$
    URL imageUrl = myClass.getResource(imageResource);
    return ImageDescriptor.createFromURL(imageUrl).createImage();
}

From source file:org.eclipse.buckminster.jnlp.p2.MaterializationUtils.java

public static Image getImage(String imageName) {
    Class<?> myClass = MaterializationUtils.class;
    String imageResource = "/icons/" + imageName;
    URL imageUrl = myClass.getResource(imageResource);
    return ImageDescriptor.createFromURL(imageUrl).createImage();
}