001/*
002 * (C) Copyright 2006-2007 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Nuxeo - initial API and implementation
016 *
017 * $Id$
018 */
019
020package org.nuxeo.runtime.deployment.preprocessor.install;
021
022import java.io.IOException;
023import java.util.List;
024
025import org.nuxeo.common.utils.FileNamePattern;
026import org.nuxeo.common.utils.Path;
027import org.nuxeo.common.utils.PathFilter;
028import org.nuxeo.common.utils.PathFilterSet;
029import org.nuxeo.common.xmap.DOMSerializer;
030import org.nuxeo.runtime.deployment.preprocessor.install.commands.AppendCommand;
031import org.nuxeo.runtime.deployment.preprocessor.install.commands.CopyCommand;
032import org.nuxeo.runtime.deployment.preprocessor.install.commands.DeleteCommand;
033import org.nuxeo.runtime.deployment.preprocessor.install.commands.MkdirCommand;
034import org.nuxeo.runtime.deployment.preprocessor.install.commands.MkfileCommand;
035import org.nuxeo.runtime.deployment.preprocessor.install.commands.MoveCommand;
036import org.nuxeo.runtime.deployment.preprocessor.install.commands.PropertyCommand;
037import org.nuxeo.runtime.deployment.preprocessor.install.commands.UnzipCommand;
038import org.nuxeo.runtime.deployment.preprocessor.install.commands.ZipCommand;
039import org.nuxeo.runtime.deployment.preprocessor.install.filters.ExcludeFilter;
040import org.nuxeo.runtime.deployment.preprocessor.install.filters.IncludeFilter;
041import org.w3c.dom.DocumentFragment;
042import org.w3c.dom.Element;
043import org.w3c.dom.Node;
044
045/**
046 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
047 */
048public final class DOMCommandsParser {
049
050    // Utility class
051    private DOMCommandsParser() {
052    }
053
054    public static CommandProcessor parse(Node element) throws IOException {
055
056        CommandProcessor cmdp = new CommandProcessorImpl();
057        List<Command> cmds = cmdp.getCommands();
058
059        Node node = element.getFirstChild();
060        while (node != null) {
061            if (node.getNodeType() == Node.ELEMENT_NODE) {
062                String name = node.getNodeName();
063                if (name.equals("copy")) {
064                    cmds.add(parseCopy((Element) node));
065                } else if (name.equals("unzip")) {
066                    cmds.add(parseUnzip((Element) node));
067                } else if (name.equals("mkdir")) {
068                    cmds.add(parseMkdir((Element) node));
069                } else if (name.equals("delete")) {
070                    cmds.add(parseDelete((Element) node));
071                } else if (name.equals("mkfile")) {
072                    cmds.add(parseMkfile((Element) node));
073                } else if (name.equals("append")) {
074                    cmds.add(parseAppend((Element) node));
075                } else if (name.equals("zip")) {
076                    cmds.add(parseZip((Element) node));
077                } else if (name.equals("property")) {
078                    cmds.add(parseProperty((Element) node));
079                }
080            }
081            node = node.getNextSibling();
082        }
083
084        return cmdp;
085    }
086
087    public static PropertyCommand parseProperty(Element element) {
088        String name = element.getAttribute("name");
089        String value = element.getAttribute("value");
090        if (value == null || value.length() == 0) {
091            value = element.getTextContent();
092        }
093        return new PropertyCommand(name, value);
094    }
095
096    public static CopyCommand parseCopy(Element element) {
097        String from = element.getAttribute("from");
098        String to = element.getAttribute("to");
099        PathFilter filter = readPathFilter(element);
100        return new CopyCommand(new Path(from), new Path(to), filter);
101    }
102
103    public static MoveCommand parseMove(Element element) {
104        String from = element.getAttribute("from");
105        String to = element.getAttribute("to");
106        PathFilter filter = readPathFilter(element);
107        return new MoveCommand(new Path(from), new Path(to), filter);
108    }
109
110    public static AppendCommand parseAppend(Element element) {
111        String from = element.getAttribute("from");
112        String to = element.getAttribute("to");
113        boolean addNewLine = false;
114        String addNewLineStr = element.getAttribute("addNewLine").trim();
115        if (addNewLineStr.length() > 0) {
116            addNewLine = Boolean.parseBoolean(addNewLineStr);
117        }
118        FileNamePattern pattern = null;
119        String patternStr = element.getAttribute("pattern").trim();
120        if (patternStr.length() > 0) {
121            pattern = new FileNamePattern(patternStr);
122        }
123        return new AppendCommand(new Path(from), new Path(to), addNewLine, pattern);
124    }
125
126    public static UnzipCommand parseUnzip(Element element) {
127        String from = element.getAttribute("from");
128        String to = element.getAttribute("to");
129        String prefix = element.getAttribute("prefix");
130        if (prefix != null && prefix.trim().length() == 0) {
131            prefix = null;
132        }
133        PathFilter filter = readPathFilter(element);
134        return new UnzipCommand(new Path(from), new Path(to), filter, prefix);
135    }
136
137    public static ZipCommand parseZip(Element element) {
138        String from = element.getAttribute("from");
139        String to = element.getAttribute("to");
140        String prefix = element.getAttribute("prefix");
141        PathFilter filter = readPathFilter(element);
142        return new ZipCommand(new Path(from), new Path(to), prefix, filter);
143    }
144
145    public static MkdirCommand parseMkdir(Element element) {
146        String path = element.getAttribute("path");
147        return new MkdirCommand(new Path(path));
148    }
149
150    public static DeleteCommand parseDelete(Element element) {
151        String path = element.getAttribute("path");
152        return new DeleteCommand(new Path(path));
153    }
154
155    public static MkfileCommand parseMkfile(Element element) throws IOException {
156        String path = element.getAttribute("path");
157        DocumentFragment df = DOMSerializer.getContentAsFragment(element);
158        if (df != null) {
159            String content = DOMSerializer.toString(df);
160            return new MkfileCommand(new Path(path), content.getBytes());
161        }
162        return new MkfileCommand(new Path(path), null);
163    }
164
165    public static PathFilterSet readPathFilter(Element element) {
166        PathFilterSet filters = new PathFilterSet();
167        Node node = element.getFirstChild();
168        while (node != null) {
169            if (node.getNodeType() == Node.ELEMENT_NODE) {
170                String nodeName = node.getNodeName();
171                if (nodeName.equals("include")) {
172                    String value = node.getTextContent();
173                    if (value != null) {
174                        filters.add(new IncludeFilter(new Path(value.trim())));
175                    }
176                } else if (nodeName.equals("exclude")) {
177                    String value = node.getTextContent();
178                    if (value != null) {
179                        filters.add(new ExcludeFilter(new Path(value.trim())));
180                    }
181                }
182            }
183            node = node.getNextSibling();
184        }
185        return filters.isEmpty() ? null : filters;
186    }
187
188}