public static boolean isElementWithEmbeddedType(XmlTagImpl xml, final String typeName, final String typeNsPrefix) { final String localName = xml.getLocalName(); if (! (XmlUtil.XML_SCHEMA_URI.equals(xml.getNamespace()) && "element".equals(localName))) { return false; } final XmlAttribute nameAttr = getNameAttr(xml); if (nameAttr == null || nameAttr.getValue() == null) return false; final String localTypeName = XmlUtil.findLocalNameByQualifiedName(nameAttr.getValue()); final String prefix = XmlUtil.findPrefixByQualifiedName(nameAttr.getValue()); if (! typeName.equals(localTypeName) || ! typeNsPrefix.equals(prefix)) { return false; } final XmlTag[] tags = xml.getSubTags(); for (XmlTag tag : tags) { if (isTypeElement((XmlTagImpl)tag)) { return true; } } return false; }
public static boolean isElementWithSomeEmbeddedType(XmlTagImpl xml) { final String localName = xml.getLocalName(); if(!(XmlUtil.XML_SCHEMA_URI.equals(xml.getNamespace()) && "element".equals(localName))) { return false; } final XmlTag[] tags = xml.getSubTags(); for(XmlTag tag : tags) { if(isTypeElement((XmlTagImpl) tag)) { return true; } } return false; }
private boolean isCertainTypeElement(XmlTagImpl xml, final String typeName, final String nsPrefix) { if(!isTypeElement(xml)) { return false; } final XmlAttribute name = getNameAttr(xml); if(name == null) { return false; } final String value = name.getValue(); if(value == null) { return false; } final String localName = XmlUtil.findLocalNameByQualifiedName(value); return typeName.equals(localName) && nsPrefix.equals(XmlUtil.findPrefixByQualifiedName(value)); }
/** * Returns the Camel route from a PsiElement * * @param element the element * @return the String route or null if there nothing can be found */ private String findRouteFromElement(PsiElement element) { XmlTag xml = PsiTreeUtil.getParentOfType(element, XmlTag.class); if (xml != null) { return ((XmlTagImpl) element.getParent()).getAttributeValue("uri"); } if (element instanceof PsiLiteralExpressionImpl) { return ((PsiLiteralExpressionImpl) element).getValue() == null ? null : ((PsiLiteralExpressionImpl) element).getValue().toString(); } if (element instanceof PsiIdentifier) { PsiIdentifier id = (PsiIdentifier) element; String text = id.getText(); if (text != null) { return text; } } // Only variables can be resolved? Optional<PsiVariable> variable = resolvedIdentifier(element) .filter(PsiVariable.class::isInstance) .map(PsiVariable.class::cast); if (variable.isPresent()) { // Try to resolve variable and recursive search route return variable.map(PsiVariable::getInitializer) .map(this::findRouteFromElement) .orElse(null); } return null; }
@Override public PsiElementListCellRenderer getRenderer(@NotNull PsiElement element, @NotNull GotoTargetHandler.GotoData gotoData) { if (element instanceof XmlTagImpl) { if (SchemaDefinitionsSearch.isTypeElement((XmlTagImpl)element)) { return new MyRenderer(""); } else if (SchemaDefinitionsSearch.isElementWithSomeEmbeddedType((XmlTagImpl)element)) { return new MyRenderer("xsd:element: "); } } return null; }
public static boolean isElementWithSomeEmbeddedType(XmlTagImpl xml) { final String localName = xml.getLocalName(); if (! (XmlUtil.XML_SCHEMA_URI.equals(xml.getNamespace()) && "element".equals(localName))) { return false; } final XmlTag[] tags = xml.getSubTags(); for (XmlTag tag : tags) { if (isTypeElement((XmlTagImpl)tag)) { return true; } } return false; }
private boolean isCertainTypeElement(XmlTagImpl xml, final String typeName, final String nsPrefix) { if (! isTypeElement(xml)) return false; final XmlAttribute name = getNameAttr(xml); if (name == null) return false; final String value = name.getValue(); if (value == null) return false; final String localName = XmlUtil.findLocalNameByQualifiedName(value); return typeName.equals(localName) && nsPrefix.equals(XmlUtil.findPrefixByQualifiedName(value)); }
@Override public PsiElementListCellRenderer getRenderer(PsiElement element, GotoTargetHandler.GotoData gotoData) { if (element instanceof XmlTagImpl) { if (SchemaDefinitionsSearch.isTypeElement((XmlTagImpl)element)) { return new MyRenderer(""); } else if (SchemaDefinitionsSearch.isElementWithSomeEmbeddedType((XmlTagImpl)element)) { return new MyRenderer("xsd:element: "); } } return null; }
private void givenBlobPropertyXmlTag() { // TODO xmlTag zusammenbauen scheitert :-( xmlTag = new XmlTagImpl(); xmlTag.setName(BlobProperty.PROPERTY_TYPE); xmlTag.setAttribute(BlobProperty.NAME_ATTR, "MP3"); xmlTag.setAttribute(BlobProperty.MIME_TYPE_ATTR, "audio/x-mpeg"); }
public NSDeclTracker(XmlTag rootTag) { myRootTag = (XmlTagImpl) rootTag; myFile = rootTag.getContainingFile(); myNSDecls = getNSDecls(false); myRootCount = myFile.getModificationStamp(); myCount = 0; }
public static boolean isElementWithEmbeddedType(XmlTagImpl xml, final String typeName, final String typeNsPrefix) { final String localName = xml.getLocalName(); if(!(XmlUtil.XML_SCHEMA_URI.equals(xml.getNamespace()) && "element".equals(localName))) { return false; } final XmlAttribute nameAttr = getNameAttr(xml); if(nameAttr == null || nameAttr.getValue() == null) { return false; } final String localTypeName = XmlUtil.findLocalNameByQualifiedName(nameAttr.getValue()); final String prefix = XmlUtil.findPrefixByQualifiedName(nameAttr.getValue()); if(!typeName.equals(localTypeName) || !typeNsPrefix.equals(prefix)) { return false; } final XmlTag[] tags = xml.getSubTags(); for(XmlTag tag : tags) { if(isTypeElement((XmlTagImpl) tag)) { return true; } } return false; }
@Override public String getElementText(XmlTagImpl element) { final XmlAttribute attr = SchemaDefinitionsSearch.getNameAttr(element); return myPrefix + (attr == null || attr.getValue() == null ? element.getName() : attr.getValue()); }
@Override protected String getContainerText(XmlTagImpl element, String name) { final PsiFile file = element.getContainingFile(); return "(" + file.getName() + ")"; }
@Override public boolean execute(@NotNull final PsiElement queryParameters, @NotNull final Processor<PsiElement> consumer) { if (queryParameters instanceof XmlTagImpl) { final XmlTagImpl xml = (XmlTagImpl) queryParameters; if (isTypeElement(xml)) { final Collection<SchemaTypeInfo> infos = ApplicationManager.getApplication().runReadAction(new Computable<Collection<SchemaTypeInfo>>() { @Override public Collection<SchemaTypeInfo> compute() { return gatherInheritors(xml); } }); if (infos != null && ! infos.isEmpty()) { final XmlFile file = XmlUtil.getContainingFile(xml); final Project project = file.getProject(); final Module module = ModuleUtilCore.findModuleForPsiElement(queryParameters); //if (module == null) return false; final VirtualFile vf = file.getVirtualFile(); String thisNs = ApplicationManager.getApplication().runReadAction(new Computable<String>() { @Override public String compute() { return XmlNamespaceIndex.getNamespace(vf, project, file); } }); thisNs = thisNs == null ? getDefaultNs(file) : thisNs; // so thisNs can be null if (thisNs == null) return false; final ArrayList<SchemaTypeInfo> infosLst = new ArrayList<SchemaTypeInfo>(infos); Collections.sort(infosLst); final Map<String, Set<XmlFile>> nsMap = new HashMap<String, Set<XmlFile>>(); for (final SchemaTypeInfo info : infosLst) { Set<XmlFile> targetFiles = nsMap.get(info.getNamespaceUri()); if (targetFiles == null) { targetFiles = new HashSet<XmlFile>(); if (Comparing.equal(info.getNamespaceUri(), thisNs)) { targetFiles.add(file); } final Collection<XmlFile> files = ApplicationManager.getApplication().runReadAction(new Computable<Collection<XmlFile>>() { @Override public Collection<XmlFile> compute() { return XmlUtil.findNSFilesByURI(info.getNamespaceUri(), project, module); } }); if (files != null) { targetFiles.addAll(files); } nsMap.put(info.getNamespaceUri(), targetFiles); } if (! targetFiles.isEmpty()) { for (final XmlFile targetFile : targetFiles) { ApplicationManager.getApplication().runReadAction(new Runnable() { @Override public void run() { final String prefixByURI = XmlUtil.findNamespacePrefixByURI(targetFile, info.getNamespaceUri()); if (prefixByURI == null) return; final PsiElementProcessor processor = new PsiElementProcessor() { @Override public boolean execute(@NotNull PsiElement element) { if (element instanceof XmlTagImpl) { if (isCertainTypeElement((XmlTagImpl)element, info.getTagName(), prefixByURI) || isElementWithEmbeddedType((XmlTagImpl)element, info.getTagName(), prefixByURI)) { consumer.process(element); return false; } } return true; } }; XmlUtil.processXmlElements(targetFile, processor, true); } }); } } } } } } return true; }
public static boolean isTypeElement(XmlTagImpl xml) { final String localName = xml.getLocalName(); return XmlUtil.XML_SCHEMA_URI.equals(xml.getNamespace()) && ("complexType".equals(localName) || "simpleType".equals(localName)); }
private Collection<SchemaTypeInfo> gatherInheritors(XmlTagImpl xml) { XmlAttribute name = getNameAttr(xml); if (name == null || StringUtil.isEmptyOrSpaces(name.getValue())) return null; String localName = name.getValue(); final boolean hasPrefix = localName.contains(":"); localName = hasPrefix ? localName.substring(localName.indexOf(':') + 1) : localName; final String nsPrefix = hasPrefix ? name.getValue().substring(0, name.getValue().indexOf(':')) : null; final XmlFile file = XmlUtil.getContainingFile(xml); if (file == null) return null; final Project project = file.getProject(); if (project == null) return null; final Set<SchemaTypeInfo> result = new HashSet<SchemaTypeInfo>(); final ArrayDeque<SchemaTypeInfo> queue = new ArrayDeque<SchemaTypeInfo>(); String nsUri; if (! hasPrefix) { nsUri = getDefaultNs(file); } else { nsUri = XmlUtil.findNamespaceByPrefix(nsPrefix, file.getRootTag()); } if (nsUri == null) return null; queue.add(new SchemaTypeInfo(localName, true, nsUri)); final PairConvertor<String,String,List<Set<SchemaTypeInfo>>> worker = SchemaTypeInheritanceIndex.getWorker(project, file.getContainingFile().getVirtualFile()); while (! queue.isEmpty()) { final SchemaTypeInfo info = queue.removeFirst(); final List<Set<SchemaTypeInfo>> childrenOfType = worker.convert(info.getNamespaceUri(), info.getTagName()); for (Set<SchemaTypeInfo> infos : childrenOfType) { for (SchemaTypeInfo typeInfo : infos) { if (typeInfo.isIsTypeName()) { queue.add(typeInfo); } result.add(typeInfo); } } } return result; }
public static XmlAttribute getNameAttr(XmlTagImpl xml) { XmlAttribute name = xml.getAttribute("name", XmlUtil.XML_SCHEMA_URI); name = name == null ? xml.getAttribute("name") : name; return name; }
public NSDeclTracker(XmlTag rootTag) { myRootTag = (XmlTagImpl)rootTag; myNSDecls = getNSDecls(false); myRootCount = myRootTag.getModificationCount(); myCount = 0; }
@Override public boolean execute(@NotNull final PsiElement queryParameters, @NotNull final Processor<PsiElement> consumer) { if (queryParameters instanceof XmlTagImpl) { final XmlTagImpl xml = (XmlTagImpl) queryParameters; if (isTypeElement(xml)) { final Collection<SchemaTypeInfo> infos = ApplicationManager.getApplication().runReadAction(new Computable<Collection<SchemaTypeInfo>>() { @Override public Collection<SchemaTypeInfo> compute() { return gatherInheritors(xml); } }); if (infos != null && ! infos.isEmpty()) { final XmlFile file = XmlUtil.getContainingFile(xml); final Project project = file.getProject(); final Module module = ModuleUtil.findModuleForPsiElement(queryParameters); //if (module == null) return false; final VirtualFile vf = file.getVirtualFile(); String thisNs = ApplicationManager.getApplication().runReadAction(new Computable<String>() { @Override public String compute() { return XmlNamespaceIndex.getNamespace(vf, project, file); } }); thisNs = thisNs == null ? getDefaultNs(file) : thisNs; // so thisNs can be null if (thisNs == null) return false; final ArrayList<SchemaTypeInfo> infosLst = new ArrayList<SchemaTypeInfo>(infos); Collections.sort(infosLst); final Map<String, Set<XmlFile>> nsMap = new HashMap<String, Set<XmlFile>>(); for (final SchemaTypeInfo info : infosLst) { Set<XmlFile> targetFiles = nsMap.get(info.getNamespaceUri()); if (targetFiles == null) { targetFiles = new HashSet<XmlFile>(); if (Comparing.equal(info.getNamespaceUri(), thisNs)) { targetFiles.add(file); } final Collection<XmlFile> files = ApplicationManager.getApplication().runReadAction(new Computable<Collection<XmlFile>>() { @Override public Collection<XmlFile> compute() { return XmlUtil.findNSFilesByURI(info.getNamespaceUri(), project, module); } }); if (files != null) { targetFiles.addAll(files); } nsMap.put(info.getNamespaceUri(), targetFiles); } if (! targetFiles.isEmpty()) { for (final XmlFile targetFile : targetFiles) { ApplicationManager.getApplication().runReadAction(new Runnable() { @Override public void run() { final String prefixByURI = XmlUtil.findNamespacePrefixByURI(targetFile, info.getNamespaceUri()); if (prefixByURI == null) return; final PsiElementProcessor processor = new PsiElementProcessor() { @Override public boolean execute(@NotNull PsiElement element) { if (element instanceof XmlTagImpl) { if (isCertainTypeElement((XmlTagImpl)element, info.getTagName(), prefixByURI) || isElementWithEmbeddedType((XmlTagImpl)element, info.getTagName(), prefixByURI)) { consumer.process(element); return false; } } return true; } }; XmlUtil.processXmlElements(targetFile, processor, true); } }); } } } } } } return true; }
private Collection<SchemaTypeInfo> gatherInheritors(XmlTagImpl xml) { XmlAttribute name = getNameAttr(xml); if(name == null || StringUtil.isEmptyOrSpaces(name.getValue())) { return null; } String localName = name.getValue(); final boolean hasPrefix = localName.contains(":"); localName = hasPrefix ? localName.substring(localName.indexOf(':') + 1) : localName; final String nsPrefix = hasPrefix ? name.getValue().substring(0, name.getValue().indexOf(':')) : null; final XmlFile file = XmlUtil.getContainingFile(xml); if(file == null) { return null; } final Project project = file.getProject(); if(project == null) { return null; } final Set<SchemaTypeInfo> result = new HashSet<SchemaTypeInfo>(); final ArrayDeque<SchemaTypeInfo> queue = new ArrayDeque<SchemaTypeInfo>(); String nsUri; if(!hasPrefix) { nsUri = getDefaultNs(file); } else { nsUri = XmlUtil.findNamespaceByPrefix(nsPrefix, file.getRootTag()); } if(nsUri == null) { return null; } queue.add(new SchemaTypeInfo(localName, true, nsUri)); final PairConvertor<String, String, List<Set<SchemaTypeInfo>>> worker = SchemaTypeInheritanceIndex.getWorker(project, file.getContainingFile().getVirtualFile()); while(!queue.isEmpty()) { final SchemaTypeInfo info = queue.removeFirst(); final List<Set<SchemaTypeInfo>> childrenOfType = worker.convert(info.getNamespaceUri(), info.getTagName()); for(Set<SchemaTypeInfo> infos : childrenOfType) { for(SchemaTypeInfo typeInfo : infos) { if(typeInfo.isIsTypeName()) { queue.add(typeInfo); } result.add(typeInfo); } } } return result; }