Search in sources :

Example 1 with LibraryItem

use of com.oracle.javafx.scenebuilder.kit.library.LibraryItem in project intellij-community by JetBrains.

the class SceneBuilderImpl method loadBuiltinComponents.

@NotNull
private static Map<String, BuiltinComponent> loadBuiltinComponents(Predicate<String> psiClassExists) {
    final Map<String, BuiltinComponent> components = new THashMap<>();
    for (LibraryItem item : BuiltinLibrary.getLibrary().getItems()) {
        final Ref<String> refQualifiedName = new Ref<>();
        final List<String> imports = new ArrayList<>();
        final Map<String, String> attributes = new THashMap<>();
        final Ref<Boolean> rootTagProcessed = new Ref<>(false);
        NanoXmlUtil.parse(new StringReader(item.getFxmlText()), new NanoXmlUtil.IXMLBuilderAdapter() {

            @Override
            public void newProcessingInstruction(String target, Reader reader) throws Exception {
                if ("import".equals(target)) {
                    final String imported = StreamUtil.readTextFrom(reader);
                    imports.add(imported);
                }
            }

            @Override
            public void addAttribute(String key, String nsPrefix, String nsURI, String value, String type) throws Exception {
                if (rootTagProcessed.get())
                    return;
                if (key != null && value != null && StringUtil.isEmpty(nsPrefix)) {
                    attributes.put(key, value);
                }
            }

            @Override
            public void elementAttributesProcessed(String name, String nsPrefix, String nsURI) throws Exception {
                rootTagProcessed.set(true);
            }

            @Override
            public void startElement(String name, String nsPrefix, String nsURI, String systemID, int lineNr) throws Exception {
                if (rootTagProcessed.get())
                    return;
                for (String imported : imports) {
                    if (imported.equals(name) || imported.endsWith("." + name)) {
                        refQualifiedName.set(imported);
                        break;
                    }
                    if (imported.endsWith(".*")) {
                        String className = imported.substring(0, imported.length() - 1) + name;
                        if (psiClassExists.test(className)) {
                            refQualifiedName.set(className);
                            break;
                        }
                    }
                }
            }
        });
        final String qualifiedName = refQualifiedName.get();
        if (!StringUtil.isEmpty(qualifiedName)) {
            final BuiltinComponent previous = components.get(qualifiedName);
            if (previous == null || previous.getAttributes().size() < attributes.size()) {
                components.put(qualifiedName, new BuiltinComponent(attributes));
            }
        }
    }
    return components;
}
Also used : LibraryItem(com.oracle.javafx.scenebuilder.kit.library.LibraryItem) Reader(java.io.Reader) StringReader(java.io.StringReader) NanoXmlUtil(com.intellij.util.xml.NanoXmlUtil) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) Ref(com.intellij.openapi.util.Ref) THashMap(gnu.trove.THashMap) StringReader(java.io.StringReader) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

Ref (com.intellij.openapi.util.Ref)1 NanoXmlUtil (com.intellij.util.xml.NanoXmlUtil)1 LibraryItem (com.oracle.javafx.scenebuilder.kit.library.LibraryItem)1 THashMap (gnu.trove.THashMap)1 IOException (java.io.IOException)1 Reader (java.io.Reader)1 StringReader (java.io.StringReader)1 MalformedURLException (java.net.MalformedURLException)1 NotNull (org.jetbrains.annotations.NotNull)1