001/** 002 * 003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved. 004 * 005 * This library is free software; you can redistribute it and/or 006 * modify it under the terms of the GNU Lesser General Public 007 * License as published by the Free Software Foundation; either 008 * version 2.1 of the License, or (at your option) any later version. 009 * 010 * This library is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013 * Lesser General Public License for more details. 014 * 015 * You should have received a copy of the GNU Lesser General Public 016 * License along with this library. If not, see <http://www.gnu.org/licenses/>. 017 * 018 **/ 019package lucee.runtime.config; 020 021import java.io.IOException; 022import java.io.InputStream; 023import java.util.ArrayList; 024 025import lucee.commons.io.IOUtil; 026import lucee.commons.io.SystemUtil; 027import lucee.commons.io.res.Resource; 028import lucee.commons.lang.ExceptionUtil; 029import lucee.commons.lang.SystemOut; 030import lucee.runtime.Info; 031 032import org.apache.xerces.parsers.DOMParser; 033import org.w3c.dom.Document; 034import org.w3c.dom.Element; 035import org.w3c.dom.Node; 036import org.w3c.dom.NodeList; 037import org.xml.sax.InputSource; 038import org.xml.sax.SAXException; 039 040public abstract class ConfigFactory { 041 static boolean doNew(Resource contextDir) { 042 043 final boolean readonly = false; 044 try { 045 Resource version = contextDir.getRealResource("version"); 046 String v = Info.getVersionAsString() + "-" + Info.getStateAsString() + "-" + Info.getRealeaseTime(); 047 if (!version.exists()) { 048 if (!readonly) { 049 version.createNewFile(); 050 IOUtil.write(version, v, SystemUtil.getCharset(), false); 051 } 052 return true; 053 } 054 else if (!IOUtil.toString(version, SystemUtil.getCharset()).equals(v)) { 055 if (!readonly) 056 IOUtil.write(version, v, SystemUtil.getCharset(), false); 057 058 return true; 059 } 060 } 061 catch (Throwable t) { 062 ExceptionUtil.rethrowIfNecessary(t); 063 } 064 return false; 065 } 066 067 /** 068 * load XML Document from XML File 069 * 070 * @param xmlFile 071 * XML File to read 072 * @return returns the Document 073 * @throws SAXException 074 * @throws IOException 075 */ 076 static Document loadDocument(Resource xmlFile) throws SAXException, IOException { 077 078 InputStream is = null; 079 try { 080 return _loadDocument(is = IOUtil.toBufferedInputStream(xmlFile.getInputStream())); 081 } 082 finally { 083 IOUtil.closeEL(is); 084 } 085 } 086 087 /** 088 * load XML Document from XML File 089 * 090 * @param is 091 * InoutStream to read 092 * @return returns the Document 093 * @throws SAXException 094 * @throws IOException 095 */ 096 private static Document _loadDocument(InputStream is) throws SAXException, IOException { 097 DOMParser parser = new DOMParser(); 098 InputSource source = new InputSource(is); 099 parser.parse(source); 100 is.close(); 101 return parser.getDocument(); 102 } 103 104 105 /** 106 * return first direct child Elements of a Element with given Name 107 * 108 * @param parent 109 * @param nodeName 110 * @return matching children 111 */ 112 public static Element getChildByName(Node parent, String nodeName) { 113 return getChildByName(parent, nodeName, false); 114 } 115 116 public static Element getChildByName(Node parent, String nodeName, boolean insertBefore) { 117 return getChildByName(parent, nodeName, insertBefore, false); 118 } 119 120 public static Element getChildByName(Node parent, String nodeName, boolean insertBefore, boolean doNotCreate) { 121 if (parent == null) 122 return null; 123 NodeList list = parent.getChildNodes(); 124 int len = list.getLength(); 125 126 for (int i = 0; i < len; i++) { 127 Node node = list.item(i); 128 129 if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equalsIgnoreCase(nodeName)) { 130 return (Element) node; 131 } 132 } 133 if (doNotCreate) 134 return null; 135 136 Element newEl = parent.getOwnerDocument().createElement(nodeName); 137 if (insertBefore) 138 parent.insertBefore(newEl, parent.getFirstChild()); 139 else 140 parent.appendChild(newEl); 141 142 return newEl; 143 } 144 145 /** 146 * return all direct child Elements of a Element with given Name 147 * 148 * @param parent 149 * @param nodeName 150 * @return matching children 151 */ 152 public static Element[] getChildren(Node parent, String nodeName) { 153 if (parent == null) 154 return new Element[0]; 155 NodeList list = parent.getChildNodes(); 156 int len = list.getLength(); 157 ArrayList<Element> rtn = new ArrayList<Element>(); 158 159 for (int i = 0; i < len; i++) { 160 Node node = list.item(i); 161 if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equalsIgnoreCase(nodeName)) { 162 rtn.add((Element)node); 163 } 164 } 165 return rtn.toArray(new Element[rtn.size()]); 166 } 167 168 /** 169 * creates a File and his content froma a resurce 170 * 171 * @param resource 172 * @param file 173 * @param password 174 * @throws IOException 175 */ 176 static void createFileFromResource(String resource, Resource file, String password) throws IOException { 177 SystemOut.printDate(SystemUtil.getPrintWriter(SystemUtil.OUT), "write file:" + file); 178 file.createNewFile(); 179 InputStream is = new Info().getClass().getResourceAsStream(resource); 180 if(is==null) throw new IOException("file ["+resource+"] does not exist."); 181 IOUtil.copy(is, file, true); 182 } 183 184 185 /** 186 * creates a File and his content froma a resurce 187 * 188 * @param resource 189 * @param file 190 * @throws IOException 191 */ 192 static void createFileFromResource(String resource, Resource file) throws IOException { 193 createFileFromResource(resource, file, null); 194 } 195 196 public static void createFileFromResourceEL(String resource, Resource file) { 197 try { 198 createFileFromResource(resource, file, null); 199 } 200 catch (Throwable e) { 201 ExceptionUtil.rethrowIfNecessary(e); 202 SystemOut.printDate(ExceptionUtil.getStacktrace(e, true), SystemUtil.ERR); 203 } 204 } 205 206 static void create(String srcPath, String[] names, Resource dir, boolean doNew) { 207 for(int i=0;i<names.length;i++){ 208 create(srcPath, names[i], dir, doNew); 209 } 210 } 211 212 static Resource create(String srcPath, String name, Resource dir, boolean doNew) { 213 if(!dir.exists())dir.mkdirs(); 214 215 Resource f = dir.getRealResource(name); 216 if (!f.exists() || doNew) 217 ConfigWebFactory.createFileFromResourceEL(srcPath+name, f); 218 return f; 219 220 } 221 222 static void delete(Resource dbDir, String[] names) { 223 for(int i=0;i<names.length;i++){ 224 delete(dbDir, names[i]); 225 } 226 } 227 228 static void delete(Resource dbDir, String name) { 229 Resource f = dbDir.getRealResource(name); 230 if (f.exists()) { 231 SystemOut.printDate(SystemUtil.getPrintWriter(SystemUtil.OUT), "delete file:" + f); 232 233 f.delete(); 234 } 235 236 } 237 238}