1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 24 package org.openoffice.setup.SetupData; 25 26 import org.openoffice.setup.InstallData; 27 import org.openoffice.setup.Util.Parser; 28 import java.util.Enumeration; 29 import java.util.HashMap; 30 import java.util.Iterator; 31 import java.util.Vector; 32 import java.util.regex.Matcher; 33 import java.util.regex.Pattern; 34 /** 35 * 36 * @author Christof Pintaske 37 */ 38 public class ProductDescription { 39 40 private class Pair { 41 public Pattern search; 42 public String replacement; 43 Pair(Pattern key, String value)44 public Pair(Pattern key, String value) { 45 search = key; 46 replacement = value; 47 } 48 } 49 50 private Vector macro; /* macro list with precompiled regex patterns */ 51 private HashMap map; /* conventional key-value pairs */ 52 private String backslashText = "THIS_IS_A_BACKSLASH"; 53 54 /** 55 * read properties from package description data 56 */ 57 ProductDescription(XMLPackageDescription descriptionData)58 protected ProductDescription(XMLPackageDescription descriptionData) { 59 macro = new Vector(); 60 map = new HashMap(); 61 parse(descriptionData); 62 } 63 64 /** 65 * retrieve information about general installation properties 66 */ 67 setNewMacro(String key, String value)68 public void setNewMacro(String key, String value) { 69 if ((key != null) && (value != null)) { 70 String match = "\\$\\{" + key + "\\}"; 71 Pattern pattern = Pattern.compile(match, Pattern.CASE_INSENSITIVE); 72 73 map.put(key, value); 74 Iterator m = map.entrySet().iterator(); 75 // while ( m.hasNext() ) { 76 // Map.Entry entry = (Map.Entry) m.next(); 77 // System.out.println( "MAP:" + entry.getKey() + ":" + entry.getValue() ); 78 // } 79 80 // Does the pair with the same pattern already exist? Then it has to be removed 81 for (int i = 0; i < macro.size(); i++) { 82 Pair pair = (Pair) macro.get(i); 83 if ( pair.search.pattern().equals(match) ) { 84 macro.remove(i); 85 } 86 } 87 88 macro.add(new Pair(pattern, value)); 89 90 // Inserting new Pair at the beginning of the macro vector. Then it will 91 // be used instead of older version during macro replacement. 92 // For example ${DIR} in the select dir dialog, if this is set more than once. 93 // macro.add(0, new Pair(pattern, value)); 94 } 95 } 96 dumpMacros()97 public void dumpMacros() { 98 for (int i = 0; i < macro.size(); i++) { 99 Pair pair = (Pair) macro.get(i); 100 System.out.println("Key: " + pair.search.pattern() + " Value: " + pair.replacement ); 101 } 102 } 103 get(String key)104 public String get(String key) { 105 return (String) map.get(key); 106 } 107 doMaskBackslash(String[] arr)108 private boolean doMaskBackslash(String[] arr) { 109 boolean changed = false; 110 111 int index = arr[0].indexOf('\\'); 112 if ( index >= 0 ) { 113 arr[0] = arr[0].replaceAll("\\", backslashText); 114 // arr[0] = arr[0].replace("\\", backslashText); 115 changed = true; 116 } 117 118 return changed; 119 } 120 doUnmaskBackslash(String s)121 private String doUnmaskBackslash(String s) { 122 s = s.replaceAll(backslashText, "\\"); 123 // s = s.replace(backslashText, "\\"); 124 return s; 125 } 126 replaceMacros(String s)127 public String replaceMacros(String s) { 128 129 String result = s; 130 131 for (int i = 0; i < macro.size(); i++) { 132 Pair pair = (Pair) macro.get(i); 133 Pattern pattern = pair.search; 134 135 Matcher matcher = pattern.matcher(result); 136 137 String replace = pair.replacement; 138 result = matcher.replaceAll(replace); 139 140 // masquerading backslashes in String replace (important for Windows paths) 141 // String[] arr1 = { replace }; 142 // boolean masked = doMaskBackslash(arr1); 143 // result = matcher.replaceAll(arr1[0]); 144 // if (masked) { 145 // result = doUnmaskBackslash(result); 146 // } 147 } 148 149 return result; 150 } 151 152 /** 153 * parse the wrapped package description 154 */ 155 parse(XMLPackageDescription data)156 private void parse(XMLPackageDescription data) { 157 158 XMLPackageDescription section; 159 160 /* product description is a leaf at the root */ 161 if (!data.getKey().equals("product")) { 162 section = data.getElement("product"); 163 if (section != null) { 164 parse(section); 165 } 166 } else { 167 InstallData installData = InstallData.getInstance(); 168 169 /* check for a default installation directory */ 170 section = data.getElement("defaultdir"); 171 if (section != null) { 172 String value = section.getValue(); 173 if (value != null) { 174 installData.setDefaultDir(value); 175 // installData.setInstallDir(value); 176 } 177 } 178 179 /* check for a default product directory */ 180 section = data.getElement("productdir"); 181 if (section != null) { 182 String value = section.getValue(); 183 if (value != null) { 184 installData.setProductDir(value); 185 } 186 } 187 188 /* check for the package format of this installation set */ 189 section = data.getElement("packageformat"); 190 if (section != null) { 191 String value = section.getValue(); 192 if (value != null) { 193 installData.setPackageFormat(value); 194 } 195 } 196 197 /* check for the package directory of this installation set */ 198 section = data.getElement("packagedirectory"); 199 if (section != null) { 200 String value = section.getValue(); 201 if ((value != null) && (! value.equals(""))) { 202 installData.setPackageSubdir(value); 203 } 204 } 205 206 /* check for the architecture of this installation set */ 207 section = data.getElement("architecture"); 208 if (section != null) { 209 String value = section.getValue(); 210 if ((value != null) && (! value.equals(""))) { 211 installData.setArchitecture(value); 212 } 213 } 214 215 section = data.getElement("multilingual"); 216 if (section != null) { 217 String value = section.getValue(); 218 if ((value != null) && (! value.equals(""))) { 219 boolean multilingualValue = Parser.parseBoolean(value); 220 installData.setIsMultiLingual(multilingualValue); 221 } 222 } 223 224 /* check for the update behaviour of this installation set */ 225 section = data.getElement("dontupdate"); 226 if (section != null) { 227 String value = section.getValue(); 228 boolean dontupdate = false; 229 if ((value != null) && (! value.equals(""))) { 230 dontupdate = Parser.parseBoolean(value); 231 } 232 installData.setDontUpdate(dontupdate); 233 } 234 235 /* check for the Product Minor of this installation set */ 236 section = data.getElement("productminor"); 237 if (section != null) { 238 String value = section.getValue(); 239 if (value != null) { 240 int intValue = Integer.parseInt(value); 241 installData.setProductMinor(intValue); 242 } 243 } 244 245 section = data.getElement("hideeula"); 246 if (section != null) { 247 String value = section.getValue(); 248 if ((value != null) && (! value.equals(""))) { 249 boolean hideeulaValue = Parser.parseBoolean(value); 250 installData.setHideEula(hideeulaValue); 251 } 252 } 253 254 /* check for any macro definitions */ 255 for (Enumeration e = data.elements(); e.hasMoreElements(); ) { 256 XMLPackageDescription p = (XMLPackageDescription) e.nextElement(); 257 if (p.getKey().equals("macro")) { 258 String key = p.getAttribute("key"); 259 String value = p.getValue(); 260 261 if ((key != null) && (value != null)) { 262 String match = "\\$\\{" + key + "\\}"; 263 Pattern pattern = Pattern.compile(match, Pattern.CASE_INSENSITIVE); 264 265 map.put(key, value); 266 macro.add(new Pair(pattern, value)); 267 } 268 } 269 } 270 } 271 } 272 273 }