Search in sources :

Example 1 with XmlBuilder

use of com.intellij.psi.impl.source.parsing.xml.XmlBuilder 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 2 with XmlBuilder

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

the class HtmlUtil method detectCharsetFromMetaTag.

public static Charset detectCharsetFromMetaTag(@NotNull CharSequence content) {
    // check for <meta http-equiv="charset=CharsetName" > or <meta charset="CharsetName"> and return Charset
    // because we will lightly parse and explicit charset isn't used very often do quick check for applicability
    int charPrefix = StringUtil.indexOf(content, CHARSET);
    do {
        if (charPrefix == -1)
            return null;
        int charsetPrefixEnd = charPrefix + CHARSET.length();
        while (charsetPrefixEnd < content.length() && Character.isWhitespace(content.charAt(charsetPrefixEnd))) ++charsetPrefixEnd;
        if (charsetPrefixEnd < content.length() && content.charAt(charsetPrefixEnd) == '=')
            break;
        charPrefix = StringUtil.indexOf(content, CHARSET, charsetPrefixEnd);
    } while (true);
    final Ref<String> charsetNameRef = new Ref<>();
    try {
        new HtmlBuilderDriver(content).build(new XmlBuilder() {

            @NonNls
            final Set<String> inTag = new THashSet<>();

            boolean metHttpEquiv = false;

            boolean metHttml5Charset = false;

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

            @Override
            public ProcessingOrder startTag(final CharSequence localName, final String namespace, final int startoffset, final int endoffset, final int headerEndOffset) {
                @NonNls String name = localName.toString().toLowerCase();
                inTag.add(name);
                if (!inTag.contains("head") && !"html".equals(name))
                    terminate();
                return ProcessingOrder.TAGS_AND_ATTRIBUTES;
            }

            private void terminate() {
                throw TerminateException.INSTANCE;
            }

            @Override
            public void endTag(final CharSequence localName, final String namespace, final int startoffset, final int endoffset) {
                @NonNls final String name = localName.toString().toLowerCase();
                if ("meta".equals(name) && (metHttpEquiv || metHttml5Charset) && contentAttributeValue != null) {
                    String charsetName;
                    if (metHttpEquiv) {
                        int start = contentAttributeValue.indexOf(CHARSET_PREFIX);
                        if (start == -1)
                            return;
                        start += CHARSET_PREFIX.length();
                        int end = contentAttributeValue.indexOf(';', start);
                        if (end == -1)
                            end = contentAttributeValue.length();
                        charsetName = contentAttributeValue.substring(start, end);
                    } else /*if (metHttml5Charset) */
                    {
                        charsetName = StringUtil.stripQuotesAroundValue(contentAttributeValue);
                    }
                    charsetNameRef.set(charsetName);
                    terminate();
                }
                if ("head".equals(name)) {
                    terminate();
                }
                inTag.remove(name);
                metHttpEquiv = false;
                metHttml5Charset = false;
                contentAttributeValue = null;
            }

            private String contentAttributeValue;

            @Override
            public void attribute(final CharSequence localName, final CharSequence v, final int startoffset, final int endoffset) {
                @NonNls final String name = localName.toString().toLowerCase();
                if (inTag.contains("meta")) {
                    @NonNls String value = v.toString().toLowerCase();
                    if (name.equals("http-equiv")) {
                        metHttpEquiv |= value.equals("content-type");
                    } else if (name.equals(CHARSET)) {
                        metHttml5Charset = true;
                        contentAttributeValue = value;
                    }
                    if (name.equals("content")) {
                        contentAttributeValue = value;
                    }
                }
            }

            @Override
            public void textElement(final CharSequence display, final CharSequence physical, final int startoffset, final int endoffset) {
            }

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

            @Override
            public void error(String message, int startOffset, int endOffset) {
            }
        });
    } catch (TerminateException ignored) {
    //ignore
    } catch (Exception ignored) {
    // some weird things can happen, like unbalanaced tree
    }
    String name = charsetNameRef.get();
    return CharsetToolkit.forName(name);
}
Also used : THashSet(gnu.trove.THashSet) HtmlBuilderDriver(com.intellij.psi.impl.source.parsing.xml.HtmlBuilderDriver) Ref(com.intellij.openapi.util.Ref) XmlBuilder(com.intellij.psi.impl.source.parsing.xml.XmlBuilder)

Example 3 with XmlBuilder

use of com.intellij.psi.impl.source.parsing.xml.XmlBuilder 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 XmlBuilder

use of com.intellij.psi.impl.source.parsing.xml.XmlBuilder 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

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