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 Schema
023 *
024 * @since 7.4
025 */
026public class SchemaRemover implements DocumentTransformer {
027
028    protected final String docType;
029
030    protected final String schema;
031
032    public SchemaRemover(String docType, String schema) {
033        this.docType = docType;
034        this.schema = schema;
035    }
036
037    @Override
038    public boolean transform(ExportedDocument xdoc) throws IOException {
039        if (docType == null || xdoc.getType().equals(docType)) {
040            Element root = xdoc.getDocument().getRootElement();
041            for (Object f : root.elements("schema")) {
042                if (schema.equals(((Element) f).attribute("name").getText())) {
043                    ((Element) f).detach();
044                }
045            }
046        }
047        return true;
048    }
049}