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());
}
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];
}
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()]);
}
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()]);
}
Aggregations