001/* 002 * (C) Copyright 2006-2007 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Nuxeo - initial API and implementation 018 * 019 * $Id$ 020 */ 021 022package org.nuxeo.runtime.deployment.preprocessor.install; 023 024import java.io.IOException; 025import java.util.List; 026 027import org.nuxeo.common.utils.FileNamePattern; 028import org.nuxeo.common.utils.Path; 029import org.nuxeo.common.utils.PathFilter; 030import org.nuxeo.common.utils.PathFilterSet; 031import org.nuxeo.common.xmap.DOMSerializer; 032import org.nuxeo.runtime.deployment.preprocessor.install.commands.AppendCommand; 033import org.nuxeo.runtime.deployment.preprocessor.install.commands.CopyCommand; 034import org.nuxeo.runtime.deployment.preprocessor.install.commands.DeleteCommand; 035import org.nuxeo.runtime.deployment.preprocessor.install.commands.MkdirCommand; 036import org.nuxeo.runtime.deployment.preprocessor.install.commands.MkfileCommand; 037import org.nuxeo.runtime.deployment.preprocessor.install.commands.MoveCommand; 038import org.nuxeo.runtime.deployment.preprocessor.install.commands.PropertyCommand; 039import org.nuxeo.runtime.deployment.preprocessor.install.commands.UnzipCommand; 040import org.nuxeo.runtime.deployment.preprocessor.install.commands.ZipCommand; 041import org.nuxeo.runtime.deployment.preprocessor.install.filters.ExcludeFilter; 042import org.nuxeo.runtime.deployment.preprocessor.install.filters.IncludeFilter; 043import org.w3c.dom.DocumentFragment; 044import org.w3c.dom.Element; 045import org.w3c.dom.Node; 046 047/** 048 * @author <a href="mailto:[email protected]">Bogdan Stefanescu</a> 049 */ 050public final class DOMCommandsParser { 051 052 // Utility class 053 private DOMCommandsParser() { 054 } 055 056 public static CommandProcessor parse(Node element) throws IOException { 057 058 CommandProcessor cmdp = new CommandProcessorImpl(); 059 List<Command> cmds = cmdp.getCommands(); 060 061 Node node = element.getFirstChild(); 062 while (node != null) { 063 if (node.getNodeType() == Node.ELEMENT_NODE) { 064 String name = node.getNodeName(); 065 if (name.equals("copy")) { 066 cmds.add(parseCopy((Element) node)); 067 } else if (name.equals("unzip")) { 068 cmds.add(parseUnzip((Element) node)); 069 } else if (name.equals("mkdir")) { 070 cmds.add(parseMkdir((Element) node)); 071 } else if (name.equals("delete")) { 072 cmds.add(parseDelete((Element) node)); 073 } else if (name.equals("mkfile")) { 074 cmds.add(parseMkfile((Element) node)); 075 } else if (name.equals("append")) { 076 cmds.add(parseAppend((Element) node)); 077 } else if (name.equals("zip")) { 078 cmds.add(parseZip((Element) node)); 079 } else if (name.equals("property")) { 080 cmds.add(parseProperty((Element) node)); 081 } 082 } 083 node = node.getNextSibling(); 084 } 085 086 return cmdp; 087 } 088 089 public static PropertyCommand parseProperty(Element element) { 090 String name = element.getAttribute("name"); 091 String value = element.getAttribute("value"); 092 if (value == null || value.length() == 0) { 093 value = element.getTextContent(); 094 } 095 return new PropertyCommand(name, value); 096 } 097 098 public static CopyCommand parseCopy(Element element) { 099 String from = element.getAttribute("from"); 100 String to = element.getAttribute("to"); 101 PathFilter filter = readPathFilter(element); 102 return new CopyCommand(new Path(from), new Path(to), filter); 103 } 104 105 public static MoveCommand parseMove(Element element) { 106 String from = element.getAttribute("from"); 107 String to = element.getAttribute("to"); 108 PathFilter filter = readPathFilter(element); 109 return new MoveCommand(new Path(from), new Path(to), filter); 110 } 111 112 public static AppendCommand parseAppend(Element element) { 113 String from = element.getAttribute("from"); 114 String to = element.getAttribute("to"); 115 boolean addNewLine = false; 116 String addNewLineStr = element.getAttribute("addNewLine").trim(); 117 if (addNewLineStr.length() > 0) { 118 addNewLine = Boolean.parseBoolean(addNewLineStr); 119 } 120 FileNamePattern pattern = null; 121 String patternStr = element.getAttribute("pattern").trim(); 122 if (patternStr.length() > 0) { 123 pattern = new FileNamePattern(patternStr); 124 } 125 return new AppendCommand(new Path(from), new Path(to), addNewLine, pattern); 126 } 127 128 public static UnzipCommand parseUnzip(Element element) { 129 String from = element.getAttribute("from"); 130 String to = element.getAttribute("to"); 131 String prefix = element.getAttribute("prefix"); 132 if (prefix != null && prefix.trim().length() == 0) { 133 prefix = null; 134 } 135 PathFilter filter = readPathFilter(element); 136 return new UnzipCommand(new Path(from), new Path(to), filter, prefix); 137 } 138 139 public static ZipCommand parseZip(Element element) { 140 String from = element.getAttribute("from"); 141 String to = element.getAttribute("to"); 142 String prefix = element.getAttribute("prefix"); 143 PathFilter filter = readPathFilter(element); 144 return new ZipCommand(new Path(from), new Path(to), prefix, filter); 145 } 146 147 public static MkdirCommand parseMkdir(Element element) { 148 String path = element.getAttribute("path"); 149 return new MkdirCommand(new Path(path)); 150 } 151 152 public static DeleteCommand parseDelete(Element element) { 153 String path = element.getAttribute("path"); 154 return new DeleteCommand(new Path(path)); 155 } 156 157 public static MkfileCommand parseMkfile(Element element) throws IOException { 158 String path = element.getAttribute("path"); 159 DocumentFragment df = DOMSerializer.getContentAsFragment(element); 160 if (df != null) { 161 String content = DOMSerializer.toString(df); 162 return new MkfileCommand(new Path(path), content.getBytes()); 163 } 164 return new MkfileCommand(new Path(path), null); 165 } 166 167 public static PathFilterSet readPathFilter(Element element) { 168 PathFilterSet filters = new PathFilterSet(); 169 Node node = element.getFirstChild(); 170 while (node != null) { 171 if (node.getNodeType() == Node.ELEMENT_NODE) { 172 String nodeName = node.getNodeName(); 173 if (nodeName.equals("include")) { 174 String value = node.getTextContent(); 175 if (value != null) { 176 filters.add(new IncludeFilter(new Path(value.trim()))); 177 } 178 } else if (nodeName.equals("exclude")) { 179 String value = node.getTextContent(); 180 if (value != null) { 181 filters.add(new ExcludeFilter(new Path(value.trim()))); 182 } 183 } 184 } 185 node = node.getNextSibling(); 186 } 187 return filters.isEmpty() ? null : filters; 188 } 189 190}