public static void removeDuplicates(@NotNull CompositePackagingElement<?> parent) { List<PackagingElement<?>> prevChildren = new ArrayList<PackagingElement<?>>(); List<PackagingElement<?>> toRemove = new ArrayList<PackagingElement<?>>(); for (PackagingElement<?> child : parent.getChildren()) { if (child instanceof CompositePackagingElement<?>) { removeDuplicates((CompositePackagingElement<?>) child); } boolean merged = false; for (PackagingElement<?> prevChild : prevChildren) { if (child.isEqualTo(prevChild)) { if (child instanceof CompositePackagingElement<?>) { for (PackagingElement<?> childElement : ((CompositePackagingElement<?>) child).getChildren()) { ((CompositePackagingElement<?>) prevChild).addOrFindChild(childElement); } } merged = true; break; } } if (merged) { toRemove.add(child); } else { prevChildren.add(child); } } for (PackagingElement<?> child : toRemove) { parent.removeChild(child); } }
@NotNull private static <S> PackagingElement<S> copyElement( @NotNull PackagingElement<S> element, @NotNull Project project) { //noinspection unchecked final PackagingElement<S> copy = (PackagingElement<S>) element.getType().createEmpty(project); copy.loadState(element.getState()); return copy; }
private static Element serializePackagingElement(PackagingElement<?> packagingElement) { Element element = new Element(PACKAGING_ELEMENT_NAME); element.setAttribute(TYPE_ID_ATTRIBUTE, packagingElement.getType().getId()); final Object bean = packagingElement.getState(); if (bean != null) { XmlSerializer.serializeInto(bean, element, new SkipDefaultValuesSerializationFilters()); } if (packagingElement instanceof CompositePackagingElement) { for (PackagingElement<?> child : ((CompositePackagingElement<?>) packagingElement).getChildren()) { element.addContent(serializePackagingElement(child)); } } return element; }
private <T> PackagingElement<T> deserializeElement(Element element) throws UnknownPackagingElementTypeException { final String id = element.getAttributeValue(TYPE_ID_ATTRIBUTE); PackagingElementType<?> type = PackagingElementFactory.getInstance().findElementType(id); if (type == null) { throw new UnknownPackagingElementTypeException(id); } PackagingElement<T> packagingElement = (PackagingElement<T>) type.createEmpty(myProject); T state = packagingElement.getState(); if (state != null) { XmlSerializer.deserializeInto(state, element); packagingElement.loadState(state); } final List children = element.getChildren(PACKAGING_ELEMENT_NAME); //noinspection unchecked for (Element child : (List<? extends Element>) children) { ((CompositePackagingElement<?>) packagingElement).addOrFindChild(deserializeElement(child)); } return packagingElement; }
private static <E extends PackagingElement<?>> boolean processElementRecursively( @NotNull PackagingElement<?> element, @Nullable PackagingElementType<E> type, @NotNull PackagingElementProcessor<? super E> processor, @NotNull PackagingElementResolvingContext resolvingContext, final boolean processSubstitutions, ArtifactType artifactType, @NotNull PackagingElementPath path, Set<PackagingElement<?>> processed) { if (!processor.shouldProcess(element) || !processed.add(element)) { return true; } if (type == null || element.getType().equals(type)) { if (!processor.process((E) element, path)) { return false; } } if (element instanceof CompositePackagingElement<?>) { final CompositePackagingElement<?> composite = (CompositePackagingElement<?>) element; return processElementsRecursively( composite.getChildren(), type, processor, resolvingContext, processSubstitutions, artifactType, path.appendComposite(composite), processed); } else if (element instanceof ComplexPackagingElement<?> && processSubstitutions) { final ComplexPackagingElement<?> complexElement = (ComplexPackagingElement<?>) element; if (processor.shouldProcessSubstitution(complexElement)) { final List<? extends PackagingElement<?>> substitution = complexElement.getSubstitution(resolvingContext, artifactType); if (substitution != null) { return processElementsRecursively( substitution, type, processor, resolvingContext, processSubstitutions, artifactType, path.appendComplex(complexElement), processed); } } } return true; }