下面列出了怎么用com.intellij.psi.xml.XmlDocument的API类实例代码及写法,或者点击链接到github查看源代码。
public boolean isMuleSchema(PsiFile file) {
if (!file.getName().endsWith(".xsd")) {
return false;
}
final PsiElement[] children = file.getChildren();
if (children.length > 0 && children[0] instanceof XmlDocument) {
final XmlTag rootTag = ((XmlDocument) children[0]).getRootTag();
if (rootTag != null) {
final String xmlns = getNamespace(rootTag);
if (xmlns != null && xmlns.startsWith("http://www.mulesoft.org/schema/mule/")) {
return true;
}
}
}
return false;
}
@Nullable
private static String getNamespace(final XmlFile xmlFile, final Project project) {
//Stupid HTTP module XSD weirdo
if (xmlFile.getName().contains("mule-httpn.xsd"))
return "http://www.mulesoft.org/schema/mule/http";
/////
final XmlDocument document = xmlFile.getDocument();
if (document != null) {
final PsiMetaData metaData = document.getMetaData();
if (metaData instanceof XmlNSDescriptorImpl) {
return ((XmlNSDescriptorImpl) metaData).getDefaultNamespace();
}
}
return null;
}
private List<String> getGlobalDefinitions(VirtualFile file) {
List<String> globalDefs = new ArrayList<>();
final DomManager manager = DomManager.getDomManager(project);
final XmlFile xmlFile = (XmlFile) PsiManager.getInstance(project).findFile(file);
final XmlDocument document = xmlFile.getDocument();
final XmlTag rootTag = document.getRootTag();
try {
final XmlTag globalFunctions = rootTag.findFirstSubTag("configuration")
.findFirstSubTag("expression-language")
.findFirstSubTag("global-functions");
String nextFunction = globalFunctions.getValue().getText();
if (nextFunction != null && StringUtils.isNotEmpty(nextFunction)) {
globalDefs.add(nextFunction);
}
} catch (Exception e) {//If the global functions config does not exist, we get NPE - but it's expected :)
//Do nothing for now
}
return globalDefs;
}
public SVGParser(XmlFile svg, String dpi) {
styles = new HashMap<String, String>();
this.svg = svg;
this.dpi = dpi;
parseDimensions();
XmlDocument document = svg.getDocument();
if (document != null) {
XmlTag rootTag = document.getRootTag();
if (rootTag != null) {
XmlTag[] subTags = rootTag.getSubTags();
for (XmlTag tag : subTags) {
getChildAttrs(tag);
}
}
}
}
public static XmlElementDescriptor getElementDescriptor(XmlTag childTag, XmlTag contextTag) {
final XmlDocument document = PsiTreeUtil.getParentOfType(contextTag, XmlDocument.class);
if (document == null) {
return null;
}
final XmlNSDescriptor nsDescriptor = document.getDefaultNSDescriptor(childTag.getNamespace(), true);
if (nsDescriptor instanceof XmlNSDescriptorEx) {
XmlElementDescriptor descriptor = ((XmlNSDescriptorEx) nsDescriptor).getElementDescriptor(childTag.getLocalName(), childTag.getNamespace());
return descriptor != null ? wrapInDelegating(descriptor) : null;
}
return null;
}
@Override
@NotNull
public Set<String> getAvailableNamespaces(@NotNull XmlFile file, @Nullable String tagName) {
final Module module = ModuleUtil.findModuleForPsiElement(file);
Map<String, XmlFile> schemas = getSchemas(module);
Set<String> namespaces = new HashSet<>();
try {
for (XmlFile xsd : schemas.values()) {
final XmlDocument document = xsd.getDocument();
if (document != null) {
final PsiMetaData metaData = document.getMetaData();
if (metaData instanceof XmlNSDescriptorImpl) {
XmlNSDescriptorImpl descriptor = (XmlNSDescriptorImpl) metaData;
String defaultNamespace = descriptor.getDefaultNamespace();
//Stupid HTTP module XSD weirdo
if (xsd.getName().contains("mule-httpn"))
defaultNamespace = "http://www.mulesoft.org/schema/mule/http";
/////
if (StringUtils.isNotEmpty(defaultNamespace)) {
if (StringUtils.isNotEmpty(tagName)) {
XmlElementDescriptor elementDescriptor = descriptor.getElementDescriptor(tagName, defaultNamespace);
if (elementDescriptor != null) {
namespaces.add(defaultNamespace);
}
} else {
namespaces.add(defaultNamespace);
}
}
}
}
}
} catch (Exception e) {
//e.printStackTrace();
}
return namespaces;
}
private static boolean isXSD(final XmlFile xmlFile) {
final XmlDocument document = xmlFile.getDocument();
if (document != null) {
final PsiMetaData metaData = document.getMetaData();
if (metaData instanceof XmlNSDescriptorImpl) {
return true;
}
}
return false;
}
public void transforming(CallBack callBack) {
svgParser = new SVGParser(svg, dpi);
styleParser = new StyleParser(svgParser.getStyles());
Logger.debug(svgParser.toString());
XmlFile dist = getDistXml();
XmlDocument document = dist.getDocument();
if (document != null && document.getRootTag() != null) {
XmlTag rootTag = document.getRootTag();
//set attr to root tag
try {
rootTag.getAttribute("android:width").setValue(svgParser.getWidth());
rootTag.getAttribute("android:height").setValue(svgParser.getHeight());
rootTag.getAttribute("android:viewportWidth").setValue(svgParser.getViewportWidth());
rootTag.getAttribute("android:viewportHeight").setValue(svgParser.getViewportHeight());
if (svgParser.getAlpha().length() > 0) {
rootTag.setAttribute("android:alpha", svgParser.getAlpha());
}
} catch (NullPointerException npe) {
//do nothing, because those attr is exist certainly.
}
//generate group
if (svgParser.hasGroupTag()) {
for (XmlTag g : svgParser.getGroups()) {
parseGroup(g, rootTag);
}
} else {
Logger.warn("Root tag has no subTag named 'group'");
parseShapeNode(svg.getRootTag(), rootTag, null);
}
CodeStyleManager.getInstance(project).reformat(dist, true);
callBack.onComplete(dist);
Logger.debug(dist.toString());
}
}
@Nullable
@Override
public ProblemDescriptor[] checkFile(@NotNull PsiFile file, @NotNull final InspectionManager manager, final boolean isOnTheFly) {
if (!(file instanceof XmlFile) || file.getLanguage() != XMLLanguage.INSTANCE || !file.getName().equals(LatteFileConfiguration.FILE_NAME)) {
return null;
}
final List<ProblemDescriptor> problems = new ArrayList<>();
XmlDocument xmlDocument = ((XmlFile) file).getDocument();
if (xmlDocument == null) {
addError(manager, problems, file, "Root tag <latte> is missing", isOnTheFly);
return problems.toArray(new ProblemDescriptor[0]);
}
if (xmlDocument.getRootTag() == null || !xmlDocument.getRootTag().getName().equals("latte")) {
addError(manager, problems, file, "Root tag <latte> is missing", isOnTheFly);
return problems.toArray(new ProblemDescriptor[0]);
}
XmlTag rootTag = xmlDocument.getRootTag();
if (rootTag.getAttribute("vendor") == null) {
PsiElement psiElement = rootTag.getFirstChild().getNextSibling();
addError(
manager,
problems,
psiElement != null ? psiElement : file,
"Missing required attribute `vendor` on tag <latte>. It must be unique name like composer package name.",
isOnTheFly
);
}
return problems.toArray(new ProblemDescriptor[0]);
}
@Nullable
public static LatteXmlFileData parse(XmlFile file) {
XmlDocument document = file.getDocument();
if (document == null || document.getRootTag() == null) {
return null;
}
XmlTag configuration = document.getRootTag();
if (configuration == null) {
return null;
}
LatteXmlFileData.VendorResult vendor = getVendor(document);
if (vendor == null) {
return null;
}
LatteXmlFileData data = new LatteXmlFileData(vendor);
XmlTag tags = configuration.findFirstSubTag("tags");
if (tags != null) {
loadTags(tags, data);
}
XmlTag filters = configuration.findFirstSubTag("filters");
if (filters != null) {
loadFilters(filters, data);
}
XmlTag variables = configuration.findFirstSubTag("variables");
if (variables != null) {
loadVariables(variables, data);
}
XmlTag functions = configuration.findFirstSubTag("functions");
if (functions != null) {
loadFunctions(functions, data);
}
return data;
}
@Nullable
public static LatteXmlFileData.VendorResult getVendor(@Nullable XmlDocument document) {
if (document == null) {
return null;
}
XmlTag configuration = document.getRootTag();
if (configuration == null) {
return null;
}
return determineVendor(getTextValue(configuration, "vendor"));
}
@Override
public void after(@NotNull List<? extends VFileEvent> events) {
for (VFileEvent event : events) {
VirtualFile file = event.getFile();
if (!(file instanceof XmlFile) || !file.getName().equals("latte-intellij.xml") || file.isValid()) {
continue;
}
XmlDocument document = ((XmlFile) file).getDocument();
if (document == null || document.getRootTag() == null) {
continue;
}
LatteXmlFileData.VendorResult vendorResult = LatteXmlFileDataFactory.getVendor(document);
if (vendorResult == null) {
continue;
}
List<Project> projects = new ArrayList<>();
Project project = ProjectUtil.guessProjectForContentFile(file);
if (project != null) {
projects.add(project);
} else {
Collections.addAll(projects, ProjectManager.getInstance().getOpenProjects());
}
LatteIndexUtil.notifyRemovedFiles(projects);
}
}
/**
* Retrieves the list of dependent haxe libraries from an XML-based
* configuration file.
*
* @param psiFile name of the configuration file to read
* @return a list of dependent libraries; may be empty, won't have duplicates.
*/
@NotNull
public static HaxeLibraryList getHaxelibsFromXmlFile(@NotNull XmlFile psiFile, HaxelibLibraryCache libraryManager) {
List<HaxeLibraryReference> haxelibNewItems = new ArrayList<HaxeLibraryReference>();
XmlFile xmlFile = (XmlFile)psiFile;
XmlDocument document = xmlFile.getDocument();
if (document != null) {
XmlTag rootTag = document.getRootTag();
if (rootTag != null) {
XmlTag[] haxelibTags = rootTag.findSubTags("haxelib");
for (XmlTag haxelibTag : haxelibTags) {
String name = haxelibTag.getAttributeValue("name");
String ver = haxelibTag.getAttributeValue("version");
HaxelibSemVer semver = HaxelibSemVer.create(ver);
if (name != null) {
HaxeLibrary lib = libraryManager.getLibrary(name, semver);
if (lib != null) {
haxelibNewItems.add(lib.createReference(semver));
} else {
LOG.warn("Library specified in XML file is not known to haxelib: " + name);
}
}
}
}
}
return new HaxeLibraryList(libraryManager.getSdk(), haxelibNewItems);
}
@Nullable
@Override
public String[][] getNamespacesFromDocument(XmlDocument parent, boolean declarationsExist) {
return XmlUtil.getDefaultNamespaces(parent);
}
@NotNull
private List<MuleModuleDefinition> getModuleDefinitions(Project project, GlobalSearchScope searchScope) {
final List<MuleModuleDefinition> result = new ArrayList<>();
final Collection<VirtualFile> files = FileTypeIndex.getFiles(XmlFileType.INSTANCE, searchScope);
for (VirtualFile file : files) {
final PsiFile xmlFile = PsiManager.getInstance(project).findFile(file);
if (xmlFile != null && isMuleSchema(xmlFile)) {
// System.out.println("xmlFile = " + xmlFile.getName());
final PsiElement[] children = xmlFile.getChildren();
final XmlTag rootTag = ((XmlDocument) children[0]).getRootTag();
if (rootTag != null) {
final String namespace = getNamespace(rootTag);
final String name = ArrayUtil.getLastElement(namespace.split("/"));
// System.out.println("namespace = " + namespace);
// System.out.println("name = " + name);
final XmlTag[] elements = rootTag.findSubTags("element", MuleSchemaUtils.HTTP_WWW_W3_ORG_2001_XMLSCHEMA);
final List<MuleElementDefinition> definitions = new ArrayList<>();
for (XmlTag element : elements) {
final String elementName = element.getAttributeValue("name");
// System.out.println("name = " + elementName);
final MuleElementType muleElementType = MuleSchemaUtils.getMuleElementTypeFromElement(element);
if (muleElementType != null) {
String description = "";
final XmlTag annotation = ArrayUtil.getFirstElement(element.findSubTags("annotation", MuleSchemaUtils.HTTP_WWW_W3_ORG_2001_XMLSCHEMA));
if (annotation != null) {
final XmlTag documentation = ArrayUtil.getFirstElement(annotation.findSubTags("documentation", MuleSchemaUtils.HTTP_WWW_W3_ORG_2001_XMLSCHEMA));
if (documentation != null) {
description = documentation.getValue().getText();
}
}
definitions.add(new MuleElementDefinition(element, elementName, description, muleElementType));
// System.out.println("muleElementType = " + muleElementType);
// System.out.println("description = " + description);
}
}
result.add(new MuleModuleDefinition(name, StringUtil.capitalize(name), namespace, xmlFile, definitions));
}
}
}
return result;
}
protected void visitRoot(PsiFile psiFile, @NotNull ProblemsHolder holder, String root, String child, String tagName) {
XmlDocument xmlDocument = PsiTreeUtil.getChildOfType(psiFile, XmlDocument.class);
if(xmlDocument == null) {
return;
}
Map<String, XmlAttribute> psiElementMap = new HashMap<>();
Set<XmlAttribute> yamlKeyValues = new HashSet<>();
for(XmlTag xmlTag: PsiTreeUtil.getChildrenOfTypeAsList(psiFile.getFirstChild(), XmlTag.class)) {
if(xmlTag.getName().equals("container")) {
for(XmlTag servicesTag: xmlTag.getSubTags()) {
if(servicesTag.getName().equals(root)) {
for(XmlTag parameterTag: servicesTag.getSubTags()) {
if(parameterTag.getName().equals(child)) {
XmlAttribute keyAttr = parameterTag.getAttribute(tagName);
if(keyAttr != null) {
String parameterName = keyAttr.getValue();
if(parameterName != null && StringUtils.isNotBlank(parameterName)) {
if(psiElementMap.containsKey(parameterName)) {
yamlKeyValues.add(psiElementMap.get(parameterName));
yamlKeyValues.add(keyAttr);
} else {
psiElementMap.put(parameterName, keyAttr);
}
}
}
}
}
}
}
}
}
if(yamlKeyValues.size() > 0) {
for(PsiElement psiElement: yamlKeyValues) {
XmlAttributeValue valueElement = ((XmlAttribute) psiElement).getValueElement();
if(valueElement != null) {
holder.registerProblem(valueElement, "Duplicate Key", ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
}
}
}
}