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