public Self withChild(@NonNls @NotNull final String localName, final ElementPattern pattern) { return with(new PatternCondition<T>("withChild") { @Override public boolean accepts(@NotNull final T t, final ProcessingContext context) { for (final AbstractDomChildrenDescription description : t.getGenericInfo().getChildrenDescriptions()) { if (!(description instanceof DomChildrenDescription) || localName.equals(((DomChildrenDescription)description).getXmlElementName())) { for (final DomElement element : description.getValues(t)) { if (localName.equals(element.getXmlElementName()) && pattern.accepts(element, context)) { return true; } } } } return false; } }); }
private JComponent getBoundComponent(final AbstractDomChildrenDescription description) { for (Field field : getClass().getDeclaredFields()) { try { field.setAccessible(true); if (description instanceof DomChildrenDescription) { final DomChildrenDescription childrenDescription = (DomChildrenDescription)description; if (convertFieldName(field.getName(), childrenDescription).equals(childrenDescription.getXmlElementName()) && field.get(this) instanceof JComponent) { return (JComponent)field.get(this); } } } catch (IllegalAccessException e) { LOG.error(e); } } return null; }
public Self withChild(@NonNls @NotNull final String localName, final ElementPattern pattern) { return with(new PatternCondition<T>("withChild") { public boolean accepts(@NotNull final T t, final ProcessingContext context) { for (final AbstractDomChildrenDescription description : t.getGenericInfo().getChildrenDescriptions()) { if (!(description instanceof DomChildrenDescription) || localName.equals(((DomChildrenDescription)description).getXmlElementName())) { for (final DomElement element : description.getValues(t)) { if (localName.equals(element.getXmlElementName()) && pattern.getCondition().accepts(element, context)) { return true; } } } } return false; } }); }
void visitXmlElement(XmlElement element, ElementStub parent, int index) { DomInvocationHandler handler = myManager.getDomHandler(element); if (handler == null || handler.getAnnotation(Stubbed.class) == null && !handler.getChildDescription().isStubbed()) return; AbstractDomChildrenDescription description = handler.getChildDescription(); String nsKey = description instanceof DomChildrenDescription ? ((DomChildrenDescription)description).getXmlName().getNamespaceKey() : ""; if (element instanceof XmlTag) { XmlTag tag = (XmlTag)element; String elementClass = null; if (handler.getAnnotation(StubbedOccurrence.class) != null) { final Type type = description.getType(); elementClass = ((Class)type).getName(); } ElementStub stub = new ElementStub(parent, StringRef.fromString(tag.getName()), StringRef.fromNullableString(nsKey), index, description instanceof CustomDomChildrenDescription, elementClass == null ? null : StringRef.fromNullableString(elementClass), tag.getSubTags().length == 0 ? tag.getValue().getTrimmedText() : ""); for (XmlAttribute attribute : tag.getAttributes()) { visitXmlElement(attribute, stub, 0); } Map<String, Integer> indices = new HashMap<String, Integer>(); for (final XmlTag subTag : tag.getSubTags()) { String name = subTag.getName(); Integer i = indices.get(name); i = i == null ? 0 : i + 1; visitXmlElement(subTag, stub, i); indices.put(name, i); } } else if (element instanceof XmlAttribute) { new AttributeStub(parent, StringRef.fromString(((XmlAttribute)element).getLocalName()), StringRef.fromNullableString(nsKey), ((XmlAttribute)element).getValue()); } }
@Override @NotNull public List<DomElementProblemDescriptor> checkRequired(final DomElement element, final DomElementAnnotationHolder holder) { final Required required = element.getAnnotation(Required.class); if (required != null) { final XmlElement xmlElement = element.getXmlElement(); if (xmlElement == null) { if (required.value()) { final String xmlElementName = element.getXmlElementName(); String namespace = element.getXmlElementNamespace(); if (element instanceof GenericAttributeValue) { return Collections.singletonList(holder.createProblem(element, IdeBundle.message("attribute.0.should.be.defined", xmlElementName), new DefineAttributeQuickFix(xmlElementName, namespace))); } return Collections.singletonList( holder.createProblem( element, HighlightSeverity.ERROR, IdeBundle.message("child.tag.0.should.be.defined", xmlElementName), new AddRequiredSubtagFix(xmlElementName, namespace) ) ); } } else if (element instanceof GenericDomValue) { return ContainerUtil.createMaybeSingletonList(checkRequiredGenericValue((GenericDomValue)element, required, holder)); } } if (DomUtil.hasXml(element)) { final SmartList<DomElementProblemDescriptor> list = new SmartList<DomElementProblemDescriptor>(); final DomGenericInfo info = element.getGenericInfo(); for (final AbstractDomChildrenDescription description : info.getChildrenDescriptions()) { if (description instanceof DomCollectionChildDescription && description.getValues(element).isEmpty()) { final DomCollectionChildDescription childDescription = (DomCollectionChildDescription)description; final Required annotation = description.getAnnotation(Required.class); if (annotation != null && annotation.value()) { list.add(holder.createProblem(element, childDescription, IdeBundle.message("child.tag.0.should.be.defined", ((DomCollectionChildDescription)description).getXmlElementName()))); } } } return list; } return Collections.emptyList(); }
public static <T extends DomElement> DomAnchor<T> createAnchor(@NotNull T t, boolean usePsi) { DomInvocationHandler handler = DomManagerImpl.getNotNullHandler(t); if (handler.getStub() != null) { return new StubAnchor<T>(handler); } if (usePsi) { final XmlElement element = t.getXmlElement(); if (element != null) { return new PsiBasedDomAnchor<T>(PsiAnchor.create(element), element.getProject()); } } final DomElement parent = t.getParent(); if (parent == null) { LOG.error("Parent null: " + t); } if (parent instanceof DomFileElementImpl) { final DomFileElementImpl fileElement = (DomFileElementImpl)parent; //noinspection unchecked return new RootAnchor<T>(fileElement.getFile(), fileElement.getRootElementClass()); } final DomAnchor<DomElement> parentAnchor = createAnchor(parent); final String name = t.getGenericInfo().getElementName(t); final AbstractDomChildrenDescription description = t.getChildDescription(); final List<? extends DomElement> values = description.getValues(parent); if (name != null) { int i = 0; for (DomElement value : values) { if (value.equals(t)) { return new NamedAnchor<T>(parentAnchor, description, name, i); } if (name.equals(value.getGenericInfo().getElementName(value))) { i++; } } } final int index = values.indexOf(t); if (index < 0) { diagnoseNegativeIndex2(t, parent, description, values); } return new IndexedAnchor<T>(parentAnchor, description, index); }
private static <T extends DomElement> void diagnoseNegativeIndex2(T t, DomElement parent, AbstractDomChildrenDescription description, List<? extends DomElement> values) { final XmlTag parentTag = parent.getXmlTag(); StringBuilder diag = new StringBuilder("Index<0: description=" + description + "\nparent=" + parent + "\nt=" + t + "\nvalues=" + values + "\n"); for (int i = 0, size = values.size(); i < size; i++) { DomElement value = values.get(i); if (value.toString().equals(t.toString())) { final XmlElement tElement = t.getXmlElement(); final XmlElement valElement = value.getXmlElement(); diag.append(" hasSame, i=" + i + "; same=" + (value == t) + ", equal=" + value.equals(t) + ", equal2=" + t.equals(value) + ", t.physical=" + (tElement == null ? "null" : String.valueOf(tElement.isPhysical())) + ", value.physical=" + (valElement == null ? "null" : String.valueOf(valElement.isPhysical())) + ", sameElements=" + (tElement == value.getXmlElement()) + "\n"); if (tElement != null && valElement != null) { diag.append(" sameFile=" + (tElement.getContainingFile() == valElement.getContainingFile()) + ", sameParent=" + (tElement.getParent() == valElement.getParent()) + "\n"); } } } if (parentTag != null) { diag.append("Parent tag: ").append(parentTag.getName()).append("\n"); if (t instanceof GenericAttributeValue) { for (XmlAttribute attribute : parentTag.getAttributes()) { diag.append(", attr: ").append(attribute.getName()); } diag.append("\n"); } else { for (XmlTag tag : parentTag.getSubTags()) { diag.append("\n subtag: ").append(tag.getName()); } diag.append("\n"); } } diag.append("Child name: ").append(t.getXmlElementName()).append(";").append(t.getXmlElementNamespaceKey()); LOG.error(diag); }
private NamedAnchor(final DomAnchor parent, final AbstractDomChildrenDescription descr, final String id, int index) { myParent = parent; myDescr = descr; myName = id; myIndex = index; }
private IndexedAnchor(final DomAnchor parent, final AbstractDomChildrenDescription descr, final int index) { myParent = parent; myDescr = descr; myIndex = index; }
@Override @Nullable public AbstractDomChildrenDescription findChildrenDescription(@NotNull final XmlTag tag, @NotNull final DomElement parent) { return findChildrenDescription(tag, getDomInvocationHandler(parent)); }
static AbstractDomChildrenDescription findChildrenDescription(final XmlTag tag, final DomInvocationHandler parent) { final DomGenericInfoEx info = parent.getGenericInfo(); return info.findChildrenDescription(parent, tag.getLocalName(), tag.getNamespace(), false, tag.getName()); }
@Nullable public abstract AbstractDomChildrenDescription findChildrenDescription(@NotNull XmlTag templateChildTag, @NotNull DomElement parent);
@Override @NotNull public AbstractDomChildrenDescription getChildDescription() { throw new UnsupportedOperationException("Method getChildDescription is not yet implemented in " + getClass().getName()); }
@NotNull public List<DomElementProblemDescriptor> checkRequired(final DomElement element, final DomElementAnnotationHolder holder) { final Required required = element.getAnnotation(Required.class); if (required != null) { final XmlElement xmlElement = element.getXmlElement(); if (xmlElement == null) { if (required.value()) { final String xmlElementName = element.getXmlElementName(); if (element instanceof GenericAttributeValue) { return Arrays.asList(holder.createProblem(element, IdeBundle.message("attribute.0.should.be.defined", xmlElementName))); } return Arrays.asList( holder.createProblem( element, HighlightSeverity.ERROR, IdeBundle.message("child.tag.0.should.be.defined", xmlElementName), new AddRequiredSubtagFix(xmlElementName, element.getXmlElementNamespace(), element.getParent().getXmlTag()) ) ); } } else if (element instanceof GenericDomValue) { return ContainerUtil.createMaybeSingletonList(checkRequiredGenericValue((GenericDomValue)element, required, holder)); } } if (DomUtil.hasXml(element)) { final SmartList<DomElementProblemDescriptor> list = new SmartList<DomElementProblemDescriptor>(); final DomGenericInfo info = element.getGenericInfo(); for (final AbstractDomChildrenDescription description : info.getChildrenDescriptions()) { if (description instanceof DomCollectionChildDescription && description.getValues(element).isEmpty()) { final DomCollectionChildDescription childDescription = (DomCollectionChildDescription)description; final Required annotation = description.getAnnotation(Required.class); if (annotation != null && annotation.value()) { list.add(holder.createProblem(element, childDescription, IdeBundle.message("child.tag.0.should.be.defined", ((DomCollectionChildDescription)description).getXmlElementName()))); } } } return list; } return Collections.emptyList(); }
public static <T extends DomElement> DomAnchor<T> createAnchor(@NotNull T t, boolean usePsi) { if (usePsi) { final XmlElement element = t.getXmlElement(); if (element != null) { return new PsiBasedDomAnchor<T>(PsiAnchor.create(element), element.getProject()); } } DomInvocationHandler handler = DomManagerImpl.getNotNullHandler(t); if (handler.getStub() != null) { return new StubAnchor<T>(handler); } final DomElement parent = t.getParent(); if (parent == null) { LOG.error("Parent null: " + t); } if (parent instanceof DomFileElementImpl) { final DomFileElementImpl fileElement = (DomFileElementImpl)parent; //noinspection unchecked return new RootAnchor<T>(fileElement.getFile(), fileElement.getRootElementClass()); } final DomAnchor<DomElement> parentAnchor = createAnchor(parent); final String name = t.getGenericInfo().getElementName(t); final AbstractDomChildrenDescription description = t.getChildDescription(); final List<? extends DomElement> values = description.getValues(parent); if (name != null) { int i = 0; for (DomElement value : values) { if (value.equals(t)) { return new NamedAnchor<T>(parentAnchor, description, name, i); } if (name.equals(value.getGenericInfo().getElementName(value))) { i++; } } } final int index = values.indexOf(t); if (index < 0) { diagnoseNegativeIndex2(t, parent, description, values); } return new IndexedAnchor<T>(parentAnchor, description, index); }
@Nullable public AbstractDomChildrenDescription findChildrenDescription(@NotNull final XmlTag tag, @NotNull final DomElement parent) { return findChildrenDescription(tag, getDomInvocationHandler(parent)); }
void visitXmlElement(XmlElement element, ElementStub parent, int index) { DomInvocationHandler handler = myManager.getDomHandler(element); if(handler == null || handler.getAnnotation(Stubbed.class) == null && !handler.getChildDescription().isStubbed()) { return; } AbstractDomChildrenDescription description = handler.getChildDescription(); String nsKey = description instanceof DomChildrenDescription ? ((DomChildrenDescription) description).getXmlName().getNamespaceKey() : ""; if(element instanceof XmlTag) { XmlTag tag = (XmlTag) element; String elementClass = null; if(handler.getAnnotation(StubbedOccurrence.class) != null) { final Type type = description.getType(); elementClass = ((Class) type).getName(); } ElementStub stub = new ElementStub(parent, StringRef.fromString(tag.getName()), StringRef.fromNullableString(nsKey), index, description instanceof CustomDomChildrenDescription, elementClass == null ? null : StringRef.fromNullableString(elementClass)); for(XmlAttribute attribute : tag.getAttributes()) { visitXmlElement(attribute, stub, 0); } Map<String, Integer> indices = new HashMap<String, Integer>(); for(final XmlTag subTag : tag.getSubTags()) { String name = subTag.getName(); Integer i = indices.get(name); i = i == null ? 0 : i + 1; visitXmlElement(subTag, stub, i); indices.put(name, i); } } else if(element instanceof XmlAttribute) { new AttributeStub(parent, StringRef.fromString(((XmlAttribute) element).getLocalName()), StringRef.fromNullableString(nsKey), ((XmlAttribute) element).getValue()); } }
AbstractDomChildrenDescription getChildDescription();