001/*
002 * Copyright (c) 2015 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     Nuxeo - initial API and implementation
011 *
012 */
013package org.nuxeo.ecm.core.io.impl.transformers;
014
015import java.io.IOException;
016
017import org.dom4j.Element;
018import org.nuxeo.ecm.core.io.DocumentTransformer;
019import org.nuxeo.ecm.core.io.ExportedDocument;
020
021/**
022 * Removes a facet
023 *
024 * @since 7.4
025 */
026public class FacetRemover implements DocumentTransformer {
027
028    protected final String docType;
029
030    protected final String facet;
031
032    public FacetRemover(String docType, String facet) {
033        this.docType = docType;
034        this.facet = facet;
035    }
036
037    @Override
038    public boolean transform(ExportedDocument xdoc) throws IOException {
039
040        if (docType == null || xdoc.getType().equals(docType)) {
041            Element root = xdoc.getDocument().getRootElement();
042            Element sys = root.element("system");
043            for (Object f : sys.elements("facet")) {
044                if (facet.equals(((Element) f).getTextTrim())) {
045                    ((Element) f).detach();
046                }
047            }
048        }
049        return true;
050    }
051}