Skip to content

Commit

Permalink
Issue #19 - Small refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
ligasgr committed Jun 2, 2015
1 parent 7321958 commit 5cfa9f3
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ public enum XQueryPredeclaredNamespace {
MATH("math", "http://www.w3.org/2005/xpath-functions/math"),
MAP("map", "http://www.w3.org/2005/xpath-functions/map"),
ERR("err", "http://www.w3.org/2005/xqt-errors"),
LOCAL("local", "http://www.w3.org/2005/xquery-local-functions");
LOCAL("local", "http://www.w3.org/2005/xquery-local-functions"),
XMLNS("xmlns", "");

private static final Map<String, String> prefixToNamespaceMap = new HashMap<String, String>();
private String prefix;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.intellij.psi.ResolveState;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.IncorrectOperationException;
import org.intellij.xquery.psi.XQueryAttrLocalName;
import org.intellij.xquery.psi.XQueryDirAttributeList;
import org.intellij.xquery.psi.XQueryDirAttributeName;
import org.intellij.xquery.psi.XQueryElementFactory;
Expand All @@ -36,40 +37,74 @@
import java.util.Collections;
import java.util.List;

import static org.intellij.xquery.reference.namespace.XQueryPredeclaredNamespace.XMLNS;

public class XQueryXmlTagNamespaceReference extends XQueryPrefixReference<XQueryXmlTagNamespace> {

public XQueryXmlTagNamespaceReference(XQueryXmlTagNamespace element, TextRange textRange) {
super(element, textRange);
}

@Override
protected Collection<ResolveResult> getPrimaryReferences() {
XQueryAttrLocalName matchingNamespaceDeclaration = getMatchingInlineNamespaceDeclaration();
if (matchingNamespaceDeclaration == null) {
matchingNamespaceDeclaration = getReferenceFromXmlUpInTheHierarchy();
}
if (matchingNamespaceDeclaration != null) {
return convertToResolveResults(matchingNamespaceDeclaration);
} else {
return Collections.emptyList();
}
}

private XQueryAttrLocalName getMatchingInlineNamespaceDeclaration() {
List<XQueryDirAttributeName> attributeNameList = getAttributeNames();
XQueryAttrLocalName matchingAttribute = null;
for (XQueryDirAttributeName attributeName : attributeNameList) {
if (isInlineNamespaceDeclaration(attributeName, myElement.getText())) {
matchingAttribute = attributeName.getAttrLocalName();
break;
}
}
return matchingAttribute;
}

private List<XQueryDirAttributeName> getAttributeNames() {
XQueryDirAttributeList attributeList = getAttributeList();
return attributeList.getDirAttributeNameList();
}

private XQueryDirAttributeList getAttributeList() {
PsiElement grandParent = myElement.getParent().getParent();
XQueryDirAttributeList attributeList;
if (grandParent instanceof XQueryXmlFullTag) {
attributeList = ((XQueryXmlFullTag) grandParent).getDirAttributeList();
} else {
attributeList = ((XQueryXmlEmptyTag) grandParent).getDirAttributeList();
}
return attributeList;
}

List<XQueryDirAttributeName> attributeNameList = attributeList.getDirAttributeNameList();
for (XQueryDirAttributeName attributeName : attributeNameList) {
if (attributeName.getAttrNamespace() != null
&& "xmlns".equals(attributeName.getAttrNamespace().getText())
&& myElement.getText().equals(attributeName.getAttrLocalName().getText())) {
Collection<ResolveResult> result = new ArrayList<ResolveResult>();
result.add(new PsiElementResolveResult(attributeName.getAttrLocalName()));
return result;
}
}
private boolean isInlineNamespaceDeclaration(XQueryDirAttributeName attributeName, String namespaceName) {
return attributeName.getAttrNamespace() != null
&& XMLNS.getPrefix().equals(attributeName.getAttrNamespace().getText())
&& namespaceName.equals(attributeName.getAttrLocalName().getText());
}

private Collection<ResolveResult> convertToResolveResults(XQueryAttrLocalName localName) {
Collection<ResolveResult> result = new ArrayList<ResolveResult>();
result.add(new PsiElementResolveResult(localName));
return result;
}

private XQueryAttrLocalName getReferenceFromXmlUpInTheHierarchy() {
XmlTagNamespaceReferenceScopeProcessor processor = new XmlTagNamespaceReferenceScopeProcessor(myElement);
PsiTreeUtil.treeWalkUp(processor, myElement, null, ResolveState.initial());
if (processor.getResult() != null) {
Collection<ResolveResult> result = new ArrayList<ResolveResult>();
result.add(new PsiElementResolveResult(processor.getResult()));
return result;
return processor.getResult();
} else {
return Collections.emptyList();
return null;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import org.intellij.xquery.psi.XQueryXmlTagNamespace;
import org.jetbrains.annotations.NotNull;

import static org.intellij.xquery.reference.namespace.XQueryPredeclaredNamespace.XMLNS;

public class XmlTagNamespaceReferenceScopeProcessor extends BaseScopeProcessor {
private XQueryAttrLocalName result;
private XQueryXmlTagNamespace myElement;
Expand All @@ -41,13 +43,16 @@ public XQueryAttrLocalName getResult() {
public boolean execute(@NotNull PsiElement element, ResolveState state) {
if (element instanceof XQueryDirAttributeName) {
XQueryDirAttributeName attributeName = (XQueryDirAttributeName) element;
if (attributeName.getAttrNamespace() != null
&& "xmlns".equals(attributeName.getAttrNamespace().getText())
&& myElement.getText().equals(attributeName.getAttrLocalName().getText())) {
if (isInlineNamespaceDeclaration(attributeName, myElement.getText())) {
result = attributeName.getAttrLocalName();
return false;
}
}
return true;
}
private boolean isInlineNamespaceDeclaration(XQueryDirAttributeName attributeName, String namespaceName) {
return attributeName.getAttrNamespace() != null
&& XMLNS.getPrefix().equals(attributeName.getAttrNamespace().getText())
&& namespaceName.equals(attributeName.getAttrLocalName().getText());
}
}

0 comments on commit 5cfa9f3

Please sign in to comment.