Java Code Examples for org.openide.filesystems.MIMEResolver

The following examples show how to use org.openide.filesystems.MIMEResolver. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: netbeans   Source File: MIMEResolverProcessor.java    License: Apache License 2.0 6 votes vote down vote up
private void registerExt(Element e, ExtensionRegistration r) throws LayerGenerationException {
    final LayerBuilder b = layer(e);
    File f = b.file("Services/MIMEResolver/" + getName(e).replace('.', '-') + "-Extension" + SUFFIX); // NOI18N
    f.methodvalue("instanceCreate", MIMEResolver.class.getName(), "create"); // NOI18N
    f.stringvalue("instanceClass", MIMEResolver.class.getName()); // NOI18N
    f.stringvalue("mimeType", r.mimeType()); // NOI18N
    int cnt = 0;
    for (String ext : r.extension()) {
        f.stringvalue("ext." + (cnt++), ext); // NOI18N
    }
    f.position(r.position());
    int ccnt = 0;
    for (String chooser : r.showInFileChooser()) {
        f.bundlevalue("fileChooser." + (ccnt++), chooser);
    }
    f.bundlevalue("displayName", r.displayName()); // NOI18N
    f.write();
}
 
Example 2
Source Project: netbeans   Source File: MIMEResolverProcessor.java    License: Apache License 2.0 6 votes vote down vote up
private void registerDefinition(Element e, String relRes, Registration r) throws LayerGenerationException {
    String absRes = LayerBuilder.absolutizeResource(e, relRes);
    final LayerBuilder b = layer(e);
    FileObject fo = b.validateResource(absRes, e, r, null, false);
    File f = b.file("Services/MIMEResolver/" + getName(e).replace('.', '-') + "-Registration" + SUFFIX); // NOI18N
    f.methodvalue("instanceCreate", MIMEResolver.class.getName(), "create"); // NOI18N
    f.stringvalue("instanceClass", MIMEResolver.class.getName()); // NOI18N
    f.serialvalue("bytes", generateInstanceResolver(fo, e, f, r)); // NOI18N
    f.position(r.position());
    int ccnt = 0;
    for (String chooser : r.showInFileChooser()) {
        f.bundlevalue("fileChooser." + (ccnt++), chooser);
    }
    f.bundlevalue("displayName", r.displayName()); // NOI18N
    f.write();
}
 
Example 3
Source Project: netbeans   Source File: MIMEResolverProcessor.java    License: Apache License 2.0 6 votes vote down vote up
private void setFileChooserRelatedAttributes(Registration r,
        MIMEResolver resolver, File f) {

    if (r.showInFileChooser().length > 0) {
        String[] types = MIMEResolverImpl.getMIMETypes(resolver);
        List<String> exts = ((FilterInfo) resolver).getExtensions();
        int cnt = 0;
        for (String ext : unq(exts)) {
            f.stringvalue("ext." + (cnt++), ext);              // NOI18N
        }
        int tcnt = 0;
        for (String type : unq(types)) {
            f.stringvalue("mimeType." + (tcnt++), type);       // NOI18N
        }
        int ncnt = 0;
        for (String name : ((FilterInfo) resolver).getFileNames()) {
            f.stringvalue("fileName." + (ncnt++), name);        //NOI18N
        }
    }
}
 
Example 4
Source Project: netbeans   Source File: MIMEResolverImpl.java    License: Apache License 2.0 6 votes vote down vote up
private static MIMEResolver forXML(FileObject def, 
    String mimeType, List<String> exts, List<String> acceptExts,
    String elem, List<String> namespace, List<String> dtds
) throws IOException {
    FileElement e = new FileElement();
    for (String ext : exts) {
        e.fileCheck.addExt(ext);
    }
    e.rule = new XMLMIMEComponent(elem, namespace, dtds);
    e.setMIME(mimeType);
    if (acceptExts.isEmpty()) {
        return new Impl(def, new FileElement[] { e }, mimeType);
    } else {
        FileElement direct = extensionElem(acceptExts, mimeType);
        return new Impl(def, new FileElement[] { e, direct }, mimeType);
    }
}
 
Example 5
Source Project: netbeans   Source File: MIMEResolverImpl.java    License: Apache License 2.0 6 votes vote down vote up
/** factory method for {@link MIMEResolver.Registration} */
public static MIMEResolver create(FileObject fo) throws IOException {
    byte[] arr = (byte[]) fo.getAttribute("bytes");
    if (arr != null) {
        return forStream(fo, arr);
    }
    String mimeType = (String) fo.getAttribute("mimeType"); // NOI18Ns
    String element = (String) fo.getAttribute("element"); // NOI18N
    List<String> exts = readArray(fo, "ext."); // NOI18N
    if (element != null) {
        List<String> accept = readArray(fo, "accept."); // NOI18N
        List<String> nss = readArray(fo, "ns."); // NOI18N
        List<String> dtds = readArray(fo, "doctype."); // NOI18N
        return forXML(fo, mimeType, exts, accept, element, nss, dtds);
    }
    
    if (!exts.isEmpty()) {
        return forExts(fo, mimeType, exts);
    }
    throw new IllegalArgumentException("" + fo);
}
 
Example 6
Source Project: netbeans   Source File: FileAssociationsModel.java    License: Apache License 2.0 6 votes vote down vote up
/** Creates new model. */
FileAssociationsModel() {
    // the following code is a dirty trick to allow the UIHelpers class
    // to be a nested class (and thus not be visible in the general javadoc)
    // in the openide.filesystems API
    // It does not matter that you suffer reading this code. The important
    // thing is that millions of users of openide.filesystems are not
    // disturbed by presence of UIHelpers class or its methods
    // in javadoc overview.
    new MIMEResolver() {
        @Override
        public String findMIMEType(FileObject fo) {
            return null;
        }
    }.super();

    FileObject resolvers = FileUtil.getConfigFile(MIME_RESOLVERS_PATH);
    if (resolvers != null) {
        resolvers.addFileChangeListener(FileUtil.weakFileChangeListener(mimeResolversListener, resolvers));
    }
}
 
Example 7
Source Project: netbeans   Source File: JadeLanguage.java    License: Apache License 2.0 6 votes vote down vote up
@MIMEResolver.ExtensionRegistration(
    extension={ "jade"},
    displayName="#JadeResolver",
    mimeType=JadeTokenId.JADE_MIME_TYPE,
    position=191
)
@NbBundle.Messages("JadeResolver=Jade Files")
@MultiViewElement.Registration(displayName = "#LBL_JadeEditorTab",
    iconBase = "org/netbeans/modules/javascript2/jade/resources/jade16.png",
    persistenceType = TopComponent.PERSISTENCE_ONLY_OPENED,
    preferredID = "jade.source",
    mimeType = JadeTokenId.JADE_MIME_TYPE,
    position = 2)
public static MultiViewEditorElement createMultiViewEditorElement(Lookup context) {
    return new MultiViewEditorElement(context);
}
 
Example 8
Source Project: netbeans   Source File: JsLanguage.java    License: Apache License 2.0 6 votes vote down vote up
@MIMEResolver.ExtensionRegistration(
    extension={ "js", "sdoc", "jsx" },
    displayName="#JsResolver",
    mimeType=JsTokenId.JAVASCRIPT_MIME_TYPE,
    position=190
)
@NbBundle.Messages("JsResolver=JavaScript Files")
@MultiViewElement.Registration(displayName = "#LBL_JsEditorTab",
    iconBase = "org/netbeans/modules/javascript2/editor/resources/javascript.png",
    persistenceType = TopComponent.PERSISTENCE_ONLY_OPENED,
    preferredID = "javascript.source",
    mimeType = JsTokenId.JAVASCRIPT_MIME_TYPE,
    position = 1)
public static MultiViewEditorElement createMultiViewEditorElement(Lookup context) {
    return new MultiViewEditorElement(context);
}
 
Example 9
Source Project: netbeans   Source File: JsonLanguage.java    License: Apache License 2.0 6 votes vote down vote up
@MIMEResolver.Registration(displayName = "jshintrc", resource = "jshintrc-resolver.xml", position = 124)
@MIMEResolver.ExtensionRegistration(
    extension={ "json" },
    displayName="#JsonResolver",
    mimeType=JsTokenId.JSON_MIME_TYPE,
    position=195
)
@NbBundle.Messages("JsonResolver=JSON Files")
@MultiViewElement.Registration(displayName = "#LBL_JsonEditorTab",
    iconBase = "org/netbeans/modules/javascript2/editor/resources/javascript.png",
    persistenceType = TopComponent.PERSISTENCE_ONLY_OPENED,
    preferredID = "json.source",
    mimeType = JsTokenId.JSON_MIME_TYPE,
    position = 1)
public static MultiViewEditorElement createMultiViewEditorElement(Lookup context) {
    return new MultiViewEditorElement(context);
}
 
Example 10
Source Project: netbeans   Source File: TomcatInstanceNode.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Overrides the compatible XML DO behaviour for files without data objects
 * @param context
 * @return 
 */
@MIMEResolver.Registration(
    displayName="org.netbeans.modules.tomcat5.resources.Bundle#TomcatResolver",
    position=380,
    resource="../../resources/tomcat-mime-resolver.xml"
)
@MultiViewElement.Registration(
    displayName="org.netbeans.modules.tomcat5.ui.nodes.Bundle#CTL_SourceTabCaption",
    iconBase="org/netbeans/modules/tomcat5/resources/tomcat5.gif",
    persistenceType=TopComponent.PERSISTENCE_ONLY_OPENED,
    preferredID="xml.text",
    mimeType="text/tomcat5+xml",
    position=1
)
public static MultiViewEditorElement createMultiViewEditorElement(Lookup context) {
    return new MultiViewEditorElement(context);
}
 
Example 11
Source Project: netbeans   Source File: MIMEResolverProcessor.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Set<String> getSupportedAnnotationTypes() {
    Set<String> set = new HashSet<String>();
    set.add(MIMEResolver.Registration.class.getCanonicalName());
    set.add(MIMEResolver.ExtensionRegistration.class.getCanonicalName());
    set.add(MIMEResolver.NamespaceRegistration.class.getCanonicalName());
    return set;
}
 
Example 12
Source Project: netbeans   Source File: MIMEResolverProcessor.java    License: Apache License 2.0 5 votes vote down vote up
private byte[] generateInstanceResolver(FileObject fo, Element e, File f,
        Registration r) throws LayerGenerationException {
    try {
        InputStream is = fo.openInputStream();
        org.openide.filesystems.FileObject tmp = FileUtil.createMemoryFileSystem().getRoot().createData("resolver.xml");
        OutputStream os = tmp.getOutputStream();
        for (;;) {
            int ch = is.read();
            if (ch == -1) {
                break;
            }
            os.write(ch);
        }
        os.close();
        is.close();
        MIMEResolver resolver = MIMEResolverImpl.forDescriptor(tmp, false);
        setFileChooserRelatedAttributes(r, resolver, f);
        final byte[] almostResult = MIMEResolverImpl.toStream(resolver);
        // XXX: it would be slightly shorter to return the array directly,
        // but the XMLFileSystem insist on deserializing the value, it does
        // not support returning plain byte[]
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(out);
        oos.writeObject(almostResult);
        oos.close();
        return out.toByteArray();
    } catch (IOException ex) {
        final LayerGenerationException le = new LayerGenerationException("Cannot process " + fo, e);
        le.initCause(ex);
        throw le;
    }
}
 
Example 13
Source Project: netbeans   Source File: MIMEResolverImpl.java    License: Apache License 2.0 5 votes vote down vote up
static MIMEResolver forDescriptor(FileObject fo, boolean warn) {
    if (warn && !isUserDefined(fo)) {
        ERR.log(Level.WARNING, "Ineffective registration of resolver {0} use @MIMEResolver.Registration! See bug #191777.", fo.getPath());
        if (ERR.isLoggable(Level.FINE)) {
            try {
                ERR.fine(fo.asText());
            } catch (IOException ex) {
                ERR.log(Level.FINE, null, ex);
            }
        }
    }
    return new Impl(fo);
}
 
Example 14
Source Project: netbeans   Source File: MIMEResolverImpl.java    License: Apache License 2.0 5 votes vote down vote up
static byte[] toStream(MIMEResolver mime) throws IOException {
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(os);
    ((Impl)mime).writeExternal(dos);
    dos.close();
    return os.toByteArray();
}
 
Example 15
Source Project: netbeans   Source File: MIMEResolverImplTest.java    License: Apache License 2.0 5 votes vote down vote up
private String resolve(FileObject fo) {
    for (MIMEResolver r : resolvers) {
        String s = r.findMIMEType(fo);
        if (s != null) return s;
    }
    return null;
}
 
Example 16
Source Project: netbeans   Source File: MIMEResolverImplTest.java    License: Apache License 2.0 5 votes vote down vote up
/** Test pattern element in declarative MIME resolver. */
public void testPatternElement() throws Exception {
    MIMEResolver resolver = createResolver(resolversRoot.getFileObject("php-resolver1.xml"));
    assertMimeType(resolver, "text/x-php5", "php.txt");
    assertMimeType(resolver, null, "not-php.txt");
    assertMimeType(resolver, "text/x-php5", "html-php.txt");

    resolver = createResolver(resolversRoot.getFileObject("php-resolver2.xml"));
    assertMimeType(resolver, null, "php.txt");
    assertMimeType(resolver, "text/x-php5", "html-php.txt");

    resolver = createResolver(resolversRoot.getFileObject("php-resolver3.xml"));
    assertMimeType(resolver, null, "php.txt");
    assertMimeType(resolver, null, "html-php.txt");
}
 
Example 17
Source Project: netbeans   Source File: MIMEResolverImplTest.java    License: Apache License 2.0 5 votes vote down vote up
/** Test name element in declarative MIME resolver. */
public void testNameElement() throws Exception {
    MIMEResolver resolver = createResolver(resolversRoot.getFileObject("makefile-resolver.xml"));
    assertMimeType(resolver, "text/x-make", "makefile", "Makefile", "MaKeFiLe", "mymakefile", "gnumakefile", "makefile1", "makefileRakefile", "makefile.Rakefile");

    assertMimeType(resolver, null, "empty.dtd", "rakefile", "Rakefile");

    resolver = createResolver(resolversRoot.getFileObject("rakefile-resolver.xml"));
    assertMimeType(resolver, "text/x-ruby", "rakefile", "Rakefile");
    assertMimeType(resolver, null, "empty.dtd", "makefile", "makefileRakefile", "makefile.Rakefile");
}
 
Example 18
Source Project: netbeans   Source File: MIMEResolverImplTest.java    License: Apache License 2.0 5 votes vote down vote up
/** Test ns element in xml-rule resolver. */
public void testNSResolver() throws Exception {
    MIMEResolver resolver = createResolver(resolversRoot.getFileObject("ns.xml"));
    assertMimeType(resolver, "ns.xml", "ns.xml");
    // #177443
    resolver = createResolver(resolversRoot.getFileObject("ns1.xml"));
    assertMimeType(resolver, "ns1", "ns.xml");
}
 
Example 19
Source Project: netbeans   Source File: MIMEResolverImplTest.java    License: Apache License 2.0 5 votes vote down vote up
/** Tests concurrent threads accessing MIMEResolverImpl. */
public void testDeadlock163378() throws Exception {
    final MIMEResolver declarativeResolver = createResolver(resolversRoot.getFileObject("pattern-resolver-valid.xml"));
    Handler handler = new Handler() {

        private boolean threadStarted = false;

        @Override
        public void publish(LogRecord record) {
            if (!threadStarted && "findMIMEType - smell.resolve.".equals(record.getMessage())) {
                Thread lockingThread = new Thread(new Runnable() {
                    @Override
                    public void run() {
                        declarativeResolver.findMIMEType(root.getFileObject("empty.dtd"));
                    }
                }, "Locking");
                threadStarted = true;
                lockingThread.start();
                try {
                    lockingThread.join();
                } catch (InterruptedException ex) {
                    Exceptions.printStackTrace(ex);
                }
            }
        }

        @Override
        public void flush() {
        }

        @Override
        public void close() throws SecurityException {
        }
    };
    Logger logger = Logger.getLogger(MIMEResolverImpl.class.getName());
    logger.addHandler(handler);
    logger.setLevel(Level.FINEST);
    declarativeResolver.findMIMEType(root.getFileObject("empty.dtd"));
    logger.removeHandler(handler);
}
 
Example 20
Source Project: netbeans   Source File: MIMEResolverMarshallTest.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected MIMEResolver createResolver(FileObject fo) throws Exception {
    final MIMEResolver orig = super.createResolver(fo);
    byte[] arr;
    try {
        arr = MIMEResolverImpl.toStream(orig);
    } catch (IOException ex) {
        // not serializable
        return orig;
    }
    return MIMEResolverImpl.forStream(fo, arr);
}
 
Example 21
Source Project: netbeans   Source File: ReadJustOnceTest.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected void setUp() throws Exception {
    URL u = this.getClass().getResource ("code-fs.xml");        
    FileSystem fs = new XMLFileSystem(u);
    FileObject resolversRoot = fs.getRoot().getFileObject("root");
    resolversRoot.refresh();
    
    FileObject fos[] = resolversRoot.getChildren();
    List<MIMEResolver> resolvers = new ArrayList<MIMEResolver>();
    for (int i = 0; i<fos.length; i++) {
        resolvers.add(createResolver(fos[i]));
    }
    
    MockLookup.setInstances(resolvers.toArray());
}
 
Example 22
Source Project: netbeans   Source File: IndexerCache.java    License: Apache License 2.0 5 votes vote down vote up
public C() {
    Map<String, Integer> order = null;
    Method getMIMETypesMethod = null;
    try {
        getMIMETypesMethod = MIMEResolver.class.getDeclaredMethod("getMIMETypes"); //NOI18N
    } catch (Exception ex) {
        // ignore
    }

    if (getMIMETypesMethod != null) {
        Collection<? extends MIMEResolver> resolvers = Lookup.getDefault().lookupAll(MIMEResolver.class);
        order = new HashMap<String, Integer>();
        int idx = 0;
        for(MIMEResolver r : resolvers) {
            String [] mimeTypes = null;
            try {
                mimeTypes = (String []) getMIMETypesMethod.invoke(r);
            } catch (Exception e) {
                // ignore;
            }

            if (mimeTypes != null) {
                for(String mimeType : mimeTypes) {
                    order.put(mimeType, idx);
                }
            }

            idx++;
        }
    }

    orderByResolvers = order != null && order.size() > 0 ? order : null;
}
 
Example 23
Source Project: netbeans   Source File: LanguageResolvers.java    License: Apache License 2.0 4 votes vote down vote up
@MIMEResolver.ExtensionRegistration(displayName = "Simple Language", extension = { "sl", "SL" }, mimeType = "application/x-r", position = 2140000100)
public void SimpleLanguage() {}
 
Example 24
Source Project: netbeans   Source File: LanguageResolvers.java    License: Apache License 2.0 4 votes vote down vote up
@MIMEResolver.ExtensionRegistration(displayName = "R", extension = { "r", "R" }, mimeType = "application/x-r", position = 2140000200)
public void R() {}
 
Example 25
Source Project: netbeans   Source File: LanguageResolvers.java    License: Apache License 2.0 4 votes vote down vote up
@MIMEResolver.ExtensionRegistration(displayName = "Ruby", extension = { "rb", "RB", "Rb" }, mimeType = "application/x-ruby", position = 2140000300)
public void Ruby() {}
 
Example 26
Source Project: netbeans   Source File: LanguageResolvers.java    License: Apache License 2.0 4 votes vote down vote up
@MIMEResolver.ExtensionRegistration(displayName = "Python", extension = { "py", "PY", "Py" }, mimeType = "text/x-python", position = 2140000400)
public void Python() {}
 
Example 27
Source Project: netbeans   Source File: NbLoaderPool.java    License: Apache License 2.0 4 votes vote down vote up
private void initListeners() {
    mimeResolvers = Lookup.getDefault().lookupResult(MIMEResolver.class);
    mimeResolvers.addLookupListener(this);
    listenToDeclarativeResolvers();
}
 
Example 28
Source Project: netbeans   Source File: DoFPMIMEType.java    License: Apache License 2.0 4 votes vote down vote up
@MIMEResolver.ExtensionRegistration(displayName = "tt1", extension = "tt1", mimeType = "text/test1",position=1000)
public void tt1() {
}
 
Example 29
Source Project: netbeans   Source File: DoFPMIMEType.java    License: Apache License 2.0 4 votes vote down vote up
@MIMEResolver.ExtensionRegistration(displayName = "tt3", extension = "tt3", mimeType = "text/test3",position=1002)
public void tt3() {
}
 
Example 30
Source Project: netbeans   Source File: DoFPMIMEType.java    License: Apache License 2.0 4 votes vote down vote up
@MIMEResolver.ExtensionRegistration(displayName = "ttm1", extension = "ttm1", mimeType = "text/testm1",position=1003)
public void ttm1() {
}