下面列出了com.intellij.psi.xml.XmlFile#getRootTag ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static List<XmlTag> findFlowRefsForFlow(@NotNull XmlTag flow) {
List<XmlTag> flowRefs = new ArrayList<>();
final Project project = flow.getProject();
final String flowName = flow.getAttributeValue(MuleConfigConstants.NAME_ATTRIBUTE);
Collection<VirtualFile> vFiles = FileTypeIndex.getFiles(StdFileTypes.XML, ProjectScope.getContentScope(project));
for (VirtualFile virtualFile : vFiles) {
PsiFile psiFile = PsiManager.getInstance(project).findFile(virtualFile);
if (psiFile != null) {
XmlFile xmlFile = (XmlFile) psiFile;
XmlTag mule = xmlFile.getRootTag();
FlowRefsFinder finder = new FlowRefsFinder(flowName);
mule.accept(finder);
flowRefs.addAll(finder.getFlowRefs());
}
}
return flowRefs;
}
public static void insertSchemaLocationLookup(XmlFile xmlFile, String namespace, String locationLookup) {
final XmlTag rootTag = xmlFile.getRootTag();
if (rootTag == null)
return;
final XmlAttribute attribute = rootTag.getAttribute("xsi:schemaLocation", HTTP_WWW_W3_ORG_2001_XMLSCHEMA);
if (attribute != null) {
final String value = attribute.getValue();
attribute.setValue(value + "\n\t\t\t" + namespace + " " + locationLookup);
} else {
final XmlElementFactory elementFactory = XmlElementFactory.getInstance(xmlFile.getProject());
final XmlAttribute schemaLocation = elementFactory.createXmlAttribute("xsi:schemaLocation", XmlUtil.XML_SCHEMA_INSTANCE_URI);
schemaLocation.setValue(namespace + " " + locationLookup);
rootTag.add(schemaLocation);
}
}
@Override
public boolean isPlaceForEndpointUri(PsiElement location) {
XmlFile file = PsiTreeUtil.getParentOfType(location, XmlFile.class);
if (file == null || file.getRootTag() == null || !isAcceptedNamespace(file.getRootTag().getNamespace())) {
return false;
}
XmlAttributeValue value = PsiTreeUtil.getParentOfType(location, XmlAttributeValue.class, false);
if (value == null) {
return false;
}
XmlAttribute attr = PsiTreeUtil.getParentOfType(location, XmlAttribute.class);
if (attr == null) {
return false;
}
return attr.getLocalName().equals("uri") && isInsideCamelRoute(location, false);
}
/**
* <routes><import resource="FOO" /></routes>
*/
private static void visitXmlFile(@NotNull XmlFile psiFile, @NotNull Consumer<FileResourceConsumer> consumer) {
XmlTag rootTag = psiFile.getRootTag();
if(rootTag == null || !"routes".equals(rootTag.getName())) {
return;
}
for (XmlTag xmlTag : rootTag.findSubTags("import")) {
String resource = xmlTag.getAttributeValue("resource");
if(StringUtils.isBlank(resource)) {
continue;
}
consumer.consume(new FileResourceConsumer(xmlTag, xmlTag, normalize(resource)));
}
}
public static boolean isMuleFile(PsiFile psiFile) {
if (!(psiFile instanceof XmlFile)) {
return false;
}
if (psiFile.getFileType() != StdFileTypes.XML) {
return false;
}
final XmlFile psiFile1 = (XmlFile) psiFile;
final XmlTag rootTag = psiFile1.getRootTag();
return isMuleTag(rootTag);
}
public static boolean isMUnitFile(PsiFile psiFile) {
if (!(psiFile instanceof XmlFile)) {
return false;
}
if (psiFile.getFileType() != StdFileTypes.XML) {
return false;
}
final XmlFile psiFile1 = (XmlFile) psiFile;
final XmlTag rootTag = psiFile1.getRootTag();
if (rootTag == null || !isMuleTag(rootTag)) {
return false;
}
final XmlTag[] munitTags = rootTag.findSubTags(MUNIT_TEST_LOCAL_NAME, rootTag.getNamespaceByPrefix(MUNIT_NAMESPACE));
return munitTags.length > 0;
}
@Nullable
public static XmlTag getXmlTagAt(Project project, XSourcePosition sourcePosition) {
final VirtualFile file = sourcePosition.getFile();
final XmlFile xmlFile = (XmlFile) PsiManager.getInstance(project).findFile(file);
final XmlTag rootTag = xmlFile.getRootTag();
return findXmlTag(sourcePosition, rootTag);
}
private boolean isMuleFile(PsiFile psiFile) {
if (!(psiFile instanceof XmlFile)) {
return false;
}
if (psiFile.getFileType() != StdFileTypes.XML) {
return false;
}
final XmlFile psiFile1 = (XmlFile) psiFile;
final XmlTag rootTag = psiFile1.getRootTag();
return rootTag.getLocalName().equalsIgnoreCase("mule");
}
@Nullable
public static String getPrefixByNamespace(XmlFile file, final String namespace) {
final XmlTag tag = file.getRootTag();
return tag == null ? null : tag.getPrefixByNamespace(namespace);
}
/**
* Extract class and repository from xml meta files
* We support orm and all odm syntax here
*/
@Nullable
private static Collection<Pair<String, String>> getClassRepositoryPair(@NotNull XmlFile xmlFile) {
XmlTag rootTag = xmlFile.getRootTag();
if(rootTag == null || !rootTag.getName().toLowerCase().startsWith("doctrine")) {
return null;
}
Collection<Pair<String, String>> pairs = new ArrayList<>();
for (XmlTag xmlTag : (XmlTag[]) ArrayUtils.addAll(rootTag.findSubTags("document"), rootTag.findSubTags("entity"))) {
XmlAttribute attr = xmlTag.getAttribute("name");
if(attr == null) {
continue;
}
String value = attr.getValue();
if(StringUtils.isBlank(value)) {
continue;
}
// extract repository-class; allow nullable
String repositoryClass = null;
XmlAttribute repositoryClassAttribute = xmlTag.getAttribute("repository-class");
if(repositoryClassAttribute != null) {
String repositoryClassAttributeValue = repositoryClassAttribute.getValue();
if(StringUtils.isNotBlank(repositoryClassAttributeValue)) {
repositoryClass = repositoryClassAttributeValue;
}
}
pairs.add(Pair.create(value, repositoryClass));
}
if(pairs.size() == 0) {
return null;
}
return pairs;
}