下面列出了怎么用com.intellij.psi.impl.source.xml.XmlDocumentImpl的API类实例代码及写法,或者点击链接到github查看源代码。
@Nullable
public static PsiElement getXmlRouteNameTarget(@NotNull XmlFile psiFile,@NotNull String routeName) {
XmlDocumentImpl document = PsiTreeUtil.getChildOfType(psiFile, XmlDocumentImpl.class);
if(document == null) {
return null;
}
for(XmlTag xmlTag: PsiTreeUtil.getChildrenOfTypeAsList(psiFile.getFirstChild(), XmlTag.class)) {
if(xmlTag.getName().equals("routes")) {
for(XmlTag routeTag: xmlTag.getSubTags()) {
if(routeTag.getName().equals("route")) {
XmlAttribute xmlAttribute = routeTag.getAttribute("id");
if(xmlAttribute != null) {
String attrValue = xmlAttribute.getValue();
if(routeName.equals(attrValue)) {
return xmlAttribute;
}
}
}
}
}
}
return null;
}
@Nullable
public static PsiElement getLocalParameterName(PsiFile psiFile, String serviceName) {
if(!(psiFile.getFirstChild() instanceof XmlDocumentImpl)) {
return null;
}
XmlTag xmlTags[] = PsiTreeUtil.getChildrenOfType(psiFile.getFirstChild(), XmlTag.class);
if(xmlTags == null) {
return null;
}
for(XmlTag xmlTag: xmlTags) {
if(xmlTag.getName().equals("container")) {
for(XmlTag servicesTag: xmlTag.getSubTags()) {
if(servicesTag.getName().equals("parameters")) {
for(XmlTag serviceTag: servicesTag.getSubTags()) {
XmlAttribute attrValue = serviceTag.getAttribute("key");
if(attrValue != null) {
String serviceNameId = attrValue.getValue();
if(serviceNameId != null && serviceNameId.equals(serviceName)) {
return serviceTag;
}
}
}
}
}
}
}
return null;
}
public static XmlElementDescriptor[] getElementsDescriptors(XmlTag context) {
XmlDocumentImpl xmlDocument = PsiTreeUtil.getParentOfType(context, XmlDocumentImpl.class);
if (xmlDocument == null) return EMPTY_ARRAY;
return ContainerUtil.map2Array(xmlDocument.getRootTagNSDescriptor().getRootElementsDescriptors(xmlDocument),
XmlElementDescriptor.class, descriptor -> wrapInDelegating(descriptor));
}
@Override
public XmlElementDescriptor[] getElementsDescriptors(XmlTag context) {
XmlDocumentImpl xmlDocument = PsiTreeUtil.getParentOfType(context, XmlDocumentImpl.class);
if (xmlDocument == null) return EMPTY_ARRAY;
return xmlDocument.getRootTagNSDescriptor().getRootElementsDescriptors(xmlDocument);
}
@Override
public XmlElementDescriptor[] getElementsDescriptors(XmlTag context) {
XmlDocumentImpl xmlDocument = PsiTreeUtil.getParentOfType(context, XmlDocumentImpl.class);
if (xmlDocument == null) return EMPTY_ARRAY;
return xmlDocument.getRootTagNSDescriptor().getRootElementsDescriptors(xmlDocument);
}
@Override
public XmlElementDescriptor[] getElementsDescriptors(XmlTag context) {
XmlDocumentImpl xmlDocument = PsiTreeUtil.getParentOfType(context, XmlDocumentImpl.class);
if (xmlDocument == null) return EMPTY_ARRAY;
return xmlDocument.getRootTagNSDescriptor().getRootElementsDescriptors(xmlDocument);
}
@Override
public XmlElementDescriptor[] getElementsDescriptors(XmlTag context) {
XmlDocumentImpl xmlDocument = PsiTreeUtil.getParentOfType(context, XmlDocumentImpl.class);
if (xmlDocument == null) return EMPTY_ARRAY;
return xmlDocument.getRootTagNSDescriptor().getRootElementsDescriptors(xmlDocument);
}
@Override
public XmlElementDescriptor[] getElementsDescriptors(XmlTag context) {
XmlDocumentImpl xmlDocument = PsiTreeUtil.getParentOfType(context, XmlDocumentImpl.class);
if (xmlDocument == null) return EMPTY_ARRAY;
return xmlDocument.getRootTagNSDescriptor().getRootElementsDescriptors(xmlDocument);
}
@Override
public XmlElementDescriptor[] getElementsDescriptors(XmlTag context) {
XmlDocumentImpl xmlDocument = PsiTreeUtil.getParentOfType(context, XmlDocumentImpl.class);
if (xmlDocument == null) return EMPTY_ARRAY;
return xmlDocument.getRootTagNSDescriptor().getRootElementsDescriptors(xmlDocument);
}
@Override
public XmlElementDescriptor[] getElementsDescriptors(XmlTag context) {
XmlDocumentImpl xmlDocument = PsiTreeUtil.getParentOfType(context, XmlDocumentImpl.class);
if (xmlDocument == null) return EMPTY_ARRAY;
return xmlDocument.getRootTagNSDescriptor().getRootElementsDescriptors(xmlDocument);
}
public static Map<String, Set<String>> getTags(@NotNull XmlFile psiFile) {
Map<String, Set<String>> map = new HashMap<>();
XmlDocumentImpl document = PsiTreeUtil.getChildOfType(psiFile, XmlDocumentImpl.class);
if(document == null) {
return map;
}
/*
* <services>
* <service id="espend_form.foo_type" class="%espend_form.foo_type.class%">
* <tag name="form.type" alias="foo_type_alias" />
* </service>
* </services>
*/
XmlTag xmlTags[] = PsiTreeUtil.getChildrenOfType(psiFile.getFirstChild(), XmlTag.class);
if(xmlTags == null) {
return map;
}
for(XmlTag xmlTag: xmlTags) {
if(xmlTag.getName().equals("container")) {
for(XmlTag servicesTag: xmlTag.getSubTags()) {
if(servicesTag.getName().equals("services")) {
for(XmlTag serviceTag: servicesTag.getSubTags()) {
XmlAttribute attrValue = serviceTag.getAttribute("id");
if(attrValue != null) {
// <service id="foo.bar" class="Class\Name">
String serviceNameId = attrValue.getValue();
if(serviceNameId != null) {
Set<String> serviceTags = getTags(serviceTag);
if(serviceTags.size() > 0) {
map.put(serviceNameId, serviceTags);
}
}
}
}
}
}
}
}
return map;
}
public static Collection<StubIndexedRoute> getXmlRouteDefinitions(XmlFile psiFile) {
XmlDocumentImpl document = PsiTreeUtil.getChildOfType(psiFile, XmlDocumentImpl.class);
if(document == null) {
return Collections.emptyList();
}
Collection<StubIndexedRoute> indexedRoutes = new ArrayList<>();
/*
* <routes>
* <route id="foo" path="/blog/{slug}" methods="GET">
* <default key="_controller">Foo</default>
* </route>
*
* <route id="foo" path="/blog/{slug}" methods="GET" controller="AppBundle:Blog:list"/>
* </routes>
*/
for(XmlTag xmlTag: PsiTreeUtil.getChildrenOfTypeAsList(psiFile.getFirstChild(), XmlTag.class)) {
if(xmlTag.getName().equals("routes")) {
for(XmlTag servicesTag: xmlTag.getSubTags()) {
if(servicesTag.getName().equals("route")) {
XmlAttribute xmlAttribute = servicesTag.getAttribute("id");
if(xmlAttribute != null) {
String attrValue = xmlAttribute.getValue();
if(attrValue != null && StringUtils.isNotBlank(attrValue)) {
StubIndexedRoute route = new StubIndexedRoute(attrValue);
String pathAttribute = servicesTag.getAttributeValue("path");
if(pathAttribute == null) {
pathAttribute = servicesTag.getAttributeValue("pattern");
}
if(pathAttribute != null && StringUtils.isNotBlank(pathAttribute) ) {
route.setPath(pathAttribute);
}
String methods = servicesTag.getAttributeValue("methods");
if(methods != null && StringUtils.isNotBlank(methods)) {
String[] split = methods.replaceAll(" +", "").toLowerCase().split("\\|");
if(split.length > 0) {
route.addMethod(split);
}
}
// <route><default key="_controller"/></route>
// <route controller="AppBundle:Blog:list"/>
String controller = getXmlController(servicesTag);
if(controller != null) {
route.setController(normalizeRouteController(controller));
}
indexedRoutes.add(route);
}
}
}
}
}
}
return indexedRoutes;
}
public static Map<String, String> getFileParameterMap(XmlFile psiFile) {
Map<String, String> services = new HashMap<>();
if(!(psiFile.getFirstChild() instanceof XmlDocumentImpl)) {
return services;
}
XmlTag xmlTags[] = PsiTreeUtil.getChildrenOfType(psiFile.getFirstChild(), XmlTag.class);
if(xmlTags == null) {
return services;
}
for(XmlTag xmlTag: xmlTags) {
if(xmlTag.getName().equals("container")) {
for(XmlTag servicesTag: xmlTag.getSubTags()) {
if(servicesTag.getName().equals("parameters")) {
for(XmlTag parameterTag: servicesTag.getSubTags()) {
// <parameter key="fos_user.user_manager.class">FOS\UserBundle\Doctrine\UserManager</parameter>
// <parameter key="fos_rest.formats" type="collection">
// <parameter key="json">false</parameter>
// </parameter>
if(parameterTag.getName().equals("parameter")) {
XmlAttribute keyAttr = parameterTag.getAttribute("key");
if(keyAttr != null) {
String parameterName = keyAttr.getValue();
if(parameterName != null && StringUtils.isNotBlank(parameterName)) {
String parameterValue = null;
String typeAttr = parameterTag.getAttributeValue("type");
// get value of parameter if we have a text value
if(!"collection".equals(typeAttr) && parameterTag.getSubTags().length == 0) {
XmlTagValue attrClass = parameterTag.getValue();
String myParameterValue = attrClass.getText();
// dont index long values
if(myParameterValue.length() < 150) {
parameterValue = myParameterValue;
}
}
services.put(parameterName.toLowerCase(), parameterValue);
}
}
}
}
}
}
}
}
return services;
}