Search in sources :

Example 1 with XmlBuilderDriver

use of com.intellij.psi.impl.source.parsing.xml.XmlBuilderDriver in project intellij-community by JetBrains.

the class XmlBuilderTest method doTest.

private static void doTest(String xml, String expectedEventSequence, final XmlBuilder.ProcessingOrder tagsAndAttributes) {
    final TestXmlBuilder builder = new TestXmlBuilder(tagsAndAttributes);
    new XmlBuilderDriver(xml).build(builder);
    assertEquals(expectedEventSequence, builder.getResult());
}
Also used : XmlBuilderDriver(com.intellij.psi.impl.source.parsing.xml.XmlBuilderDriver)

Example 2 with XmlBuilderDriver

use of com.intellij.psi.impl.source.parsing.xml.XmlBuilderDriver in project intellij-community by JetBrains.

the class MavenJDOMUtil method doRead.

@Nullable
private static Element doRead(String text, final ErrorHandler handler) {
    final LinkedList<Element> stack = new LinkedList<>();
    final Element[] result = { null };
    XmlBuilderDriver driver = new XmlBuilderDriver(text);
    XmlBuilder builder = new XmlBuilder() {

        public void doctype(@Nullable CharSequence publicId, @Nullable CharSequence systemId, int startOffset, int endOffset) {
        }

        public ProcessingOrder startTag(CharSequence localName, String namespace, int startoffset, int endoffset, int headerEndOffset) {
            String name = localName.toString();
            if (StringUtil.isEmptyOrSpaces(name))
                return ProcessingOrder.TAGS;
            Element newElement;
            try {
                newElement = new Element(name);
            } catch (IllegalNameException e) {
                newElement = new Element("invalidName");
            }
            Element parent = stack.isEmpty() ? null : stack.getLast();
            if (parent == null) {
                result[0] = newElement;
            } else {
                parent.addContent(newElement);
            }
            stack.addLast(newElement);
            return ProcessingOrder.TAGS_AND_TEXTS;
        }

        public void endTag(CharSequence localName, String namespace, int startoffset, int endoffset) {
            String name = localName.toString();
            if (StringUtil.isEmptyOrSpaces(name))
                return;
            for (Iterator<Element> itr = stack.descendingIterator(); itr.hasNext(); ) {
                Element element = itr.next();
                if (element.getName().equals(name)) {
                    while (stack.removeLast() != element) {
                    }
                    break;
                }
            }
        }

        public void textElement(CharSequence text, CharSequence physical, int startoffset, int endoffset) {
            stack.getLast().addContent(JDOMUtil.legalizeText(text.toString()));
        }

        public void attribute(CharSequence name, CharSequence value, int startoffset, int endoffset) {
        }

        public void entityRef(CharSequence ref, int startOffset, int endOffset) {
        }

        public void error(String message, int startOffset, int endOffset) {
            if (handler != null)
                handler.onSyntaxError();
        }
    };
    driver.build(builder);
    return result[0];
}
Also used : XmlBuilderDriver(com.intellij.psi.impl.source.parsing.xml.XmlBuilderDriver) Element(org.jdom.Element) XmlBuilder(com.intellij.psi.impl.source.parsing.xml.XmlBuilder) IllegalNameException(org.jdom.IllegalNameException) Nullable(org.jetbrains.annotations.Nullable) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with XmlBuilderDriver

use of com.intellij.psi.impl.source.parsing.xml.XmlBuilderDriver in project intellij-plugins by JetBrains.

the class SwcCatalogXmlUtil method parseComponentsFromCatalogXml.

private static ComponentFromCatalogXml[] parseComponentsFromCatalogXml(final VirtualFile catalogFile) {
    final Collection<ComponentFromCatalogXml> result = new ArrayList<>();
    final XmlBuilder xmlBuilder = new XmlBuilderAdapter() {

        private static final String COMPONENT_LOCATION = ".swc.components.component";

        private static final String NAME = "name";

        private static final String CLASS_NAME = "className";

        private static final String URI = "uri";

        private static final String ICON = "icon";

        private String myNameAttr = null;

        private String myClassNameAttr = null;

        private String myUriAttr = null;

        private String myIconAttr = null;

        @Override
        public void attribute(CharSequence name, CharSequence value, int start, int end) {
            if (COMPONENT_LOCATION.equals(getLocation())) {
                if (NAME.equals(name)) {
                    myNameAttr = value.toString().trim();
                } else if (CLASS_NAME.equals(name)) {
                    myClassNameAttr = value.toString().trim();
                } else if (URI.equals(name)) {
                    myUriAttr = value.toString().trim();
                } else if (ICON.equals(name)) {
                    myIconAttr = value.toString().trim();
                }
            }
        }

        private final StringInterner myStringInterner = new StringInterner();

        @Override
        public void endTag(CharSequence localName, String namespace, int start, int end) {
            if (COMPONENT_LOCATION.equals(getLocation())) {
                if (StringUtil.isNotEmpty(myNameAttr) && StringUtil.isNotEmpty(myClassNameAttr) && StringUtil.isNotEmpty(myUriAttr)) {
                    result.add(new ComponentFromCatalogXml(new String(myNameAttr), new String(myClassNameAttr.replace(":", ".")), myStringInterner.intern(new String(myUriAttr)), myIconAttr != null ? new String(myIconAttr) : null));
                }
                myNameAttr = null;
                myClassNameAttr = null;
                myUriAttr = null;
                myIconAttr = null;
            }
            super.endTag(localName, namespace, start, end);
        }
    };
    try {
        new XmlBuilderDriver(VfsUtilCore.loadText(catalogFile)).build(xmlBuilder);
    } catch (IOException ignored) {
    /*ignore*/
    }
    return result.toArray(new ComponentFromCatalogXml[result.size()]);
}
Also used : XmlBuilderDriver(com.intellij.psi.impl.source.parsing.xml.XmlBuilderDriver) StringInterner(com.intellij.util.containers.StringInterner) ArrayList(java.util.ArrayList) XmlBuilder(com.intellij.psi.impl.source.parsing.xml.XmlBuilder) IOException(java.io.IOException)

Example 4 with XmlBuilderDriver

use of com.intellij.psi.impl.source.parsing.xml.XmlBuilderDriver in project intellij-plugins by JetBrains.

the class SwcCatalogXmlUtil method parseManifestFile.

private static ComponentFromManifest[] parseManifestFile(final VirtualFile manifestFile) {
    final Collection<ComponentFromManifest> result = new ArrayList<>();
    final XmlBuilder builder = new XmlBuilderAdapter() {

        private static final String COMPONENT = "component";

        private static final String ID = "id";

        private static final String CLASS = "class";

        private String idAttr = null;

        private String classAttr = null;

        @Override
        public void attribute(final CharSequence name, final CharSequence value, final int start, final int end) {
            if (ID.equals(name.toString())) {
                idAttr = value.toString().trim();
            } else if (CLASS.equals(name.toString())) {
                classAttr = value.toString().trim();
            }
        }

        @Override
        public void endTag(final CharSequence localName, final String namespace, final int start, final int end) {
            if (COMPONENT.equals(localName)) {
                if (StringUtil.isNotEmpty(classAttr)) {
                    final String classFqn = classAttr.replace(":", ".");
                    final String componentName = idAttr != null ? idAttr : classAttr.substring(classFqn.lastIndexOf('.') + 1);
                    result.add(new ComponentFromManifest(new String(componentName), new String(classFqn)));
                }
            }
            idAttr = null;
            classAttr = null;
            super.endTag(localName, namespace, start, end);
        }
    };
    try {
        new XmlBuilderDriver(VfsUtilCore.loadText(manifestFile)).build(builder);
    } catch (IOException ignored) {
    /*ignore*/
    }
    return result.toArray(new ComponentFromManifest[result.size()]);
}
Also used : XmlBuilderDriver(com.intellij.psi.impl.source.parsing.xml.XmlBuilderDriver) ArrayList(java.util.ArrayList) XmlBuilder(com.intellij.psi.impl.source.parsing.xml.XmlBuilder) IOException(java.io.IOException)

Aggregations

XmlBuilderDriver (com.intellij.psi.impl.source.parsing.xml.XmlBuilderDriver)4 XmlBuilder (com.intellij.psi.impl.source.parsing.xml.XmlBuilder)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 StringInterner (com.intellij.util.containers.StringInterner)1 Element (org.jdom.Element)1 IllegalNameException (org.jdom.IllegalNameException)1 Nullable (org.jetbrains.annotations.Nullable)1