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.Util.Parser;
27 import java.io.Reader;
28 import java.io.StringReader;
29 import java.util.Collections;
30 import java.util.Comparator;
31 import java.util.Enumeration;
32 import java.util.Locale;
33 import java.util.Vector;
34 import javax.swing.tree.TreeNode;
35 
36 /**
37  *
38  * @author Ingo Schmidt
39  */
40 public class PackageDescription implements TreeNode {
41 
42     public final static int NOTA_UNIT= 0;
43     public final static int RPM_UNIT = 1;
44     public final static int PKG_UNIT = 2;
45     /* public final static int MSI_UNIT = 3;
46      * public final static int TAR_UNIT = 4;
47      * public final static int TGZ_UNIT = 5;
48      */
49 
50     public final static int DONT_INSTALL      = 0;
51     public final static int INSTALL           = 1;
52     public final static int INSTALL_SOME      = 2;
53     public final static int REMOVE            = 3;
54     public final static int DONT_REMOVE       = 4;
55     public final static int REMOVE_SOME       = 5;
56     public final static int IGNORE            = 6;
57     public final static int DONT_KNOW         = 7;
58 
59     /* hierarchy information */
60     private Vector children           = null;
61     private PackageDescription parent = null;
62 
63     /* did the user select this package */
64     private int     usrSelectionState = DONT_KNOW;
65 
66     /* display information */
67     private String  dpyName            = "";
68     private String  dpyDescription     = "";
69     private String  checkSolaris       = "";
70     private int     instSize    = 0;
71     private int     dpySortKey  = 0;
72     private boolean isDefault   = true;
73     private boolean isHidden    = false;
74     private boolean showInUserInstall = true;
75     private boolean showInUserInstallOnly = false;
76     private boolean isOptional  = true;
77     private boolean dontUninstall = false;
78     private boolean allChildrenHidden = false;
79 
80     /* package information */
81     private String  pkgMD5Sum   = "";
82     private String  pkgFileName = "";
83     private String  pkgFullName = null;
84     private String  pkgVersion = null;
85     private String  pkgRelease = null;
86     private String  pkgRealName = null;
87     private String  pkgSubdir = null;
88     private String  pkgLanguage = null;
89     private int     pkgSize     = 0;
90     private int     pkgOrder    = 0;
91     private int     pkgType     = NOTA_UNIT;
92     private boolean pkgExists = true;  // must be default, especially for uninstallation
93     private boolean isRelocatable = true;
94     private boolean isUpdatePackage = false;
95     private boolean showMultiLingualOnly = false;
96     private boolean isApplicationPackage = false;
97     private boolean isJavaPackage = false;
98     private boolean installCanFail = false;
99     private boolean uninstallCanFail = false;
100     private boolean forceIntoUpdate = false;
101     private boolean useForce = false;
102     private boolean isNewInstalled = false;
103     private boolean wasAlreadyInstalled = false;
104     private boolean ignoreDependsForUninstall = false;
105 
106     /* Saving the default selection state. This is necessary, if the user chooses
107      * the custom installation type, makes changes, and then changes into
108      * the typical installation set. Then all user settings have to be removed again.
109      * On the other hand, if the user then changes to the custom installation type
110      * again, he probably wants to see the settings he has done before.
111      */
112 
113     private int     typicalSelectionState = DONT_KNOW;  // Saving settings for typical installation
114     private int     customSelectionState = DONT_KNOW;   // Saving settings for custom installation
115     private int     startSelectionState = DONT_KNOW;    // Saving settings at start of installation
116 
PackageDescription()117     public PackageDescription() {}
118 
119     /**
120      * construct only with package information to wrap
121      */
PackageDescription(XMLPackageDescription p)122     protected PackageDescription(XMLPackageDescription p) {
123         this(p, (PackageDescription)null);
124     }
125 
PackageDescription(XMLPackageDescription descriptionData, PackageDescription parent)126     private PackageDescription(XMLPackageDescription descriptionData, PackageDescription parent) {
127 
128         this.parent = parent;
129         children = new Vector();
130         parse(descriptionData);
131 
132         /* sort according to display sort key */
133         Collections.sort(children, new PackageComparator());
134      }
135 
136     /**
137      * retrieve information about the package properties
138      */
139 
getName()140     public String getName() {
141         return dpyName;
142     }
143 
setName(String name)144     public void setName(String name) {
145         dpyName = name;
146     }
147 
getDescription()148     public String getDescription() {
149         return dpyDescription;
150     }
151 
getCheckSolaris()152     public String getCheckSolaris() {
153         return checkSolaris;
154     }
155 
getSize()156     public int getSize() {
157         return pkgSize;
158     }
159 
setSize(int size)160     public void setSize(int size) {
161         pkgSize = size;
162     }
163 
getOrder()164     public int getOrder() {
165         return pkgOrder;
166     }
167 
setOrder(int order)168     public void setOrder(int order) {
169         pkgOrder = order;
170     }
171 
172     // public int getAccumulatedSize() {
173     //     int size = getSize();
174     //
175     //     for (Enumeration e = this.children(); e.hasMoreElements(); ) {
176     //         // Should only be accumulated for selected modules
177     //         PackageDescription data = (PackageDescription) e.nextElement();
178     //         size += data.getSize();
179     //     }
180     //
181     //     return size;
182     // }
183 
isOptional()184     public boolean isOptional() {
185         return isOptional;
186     }
187 
isHidden()188     public boolean isHidden() {
189         return isHidden;
190     }
191 
showInUserInstall()192     public boolean showInUserInstall() {
193         return showInUserInstall;
194     }
195 
showInUserInstallOnly()196     public boolean showInUserInstallOnly() {
197         return showInUserInstallOnly;
198     }
199 
dontUninstall()200     public boolean dontUninstall() {
201         return dontUninstall;
202     }
203 
isUpdatePackage()204     public boolean isUpdatePackage() {
205         return isUpdatePackage;
206     }
207 
showMultiLingualOnly()208     public boolean showMultiLingualOnly() {
209         return showMultiLingualOnly;
210     }
211 
isApplicationPackage()212     public boolean isApplicationPackage() {
213         return isApplicationPackage;
214     }
215 
isJavaPackage()216     public boolean isJavaPackage() {
217         return isJavaPackage;
218     }
219 
installCanFail()220     public boolean installCanFail() {
221         return installCanFail;
222     }
223 
uninstallCanFail()224     public boolean uninstallCanFail() {
225         return uninstallCanFail;
226     }
227 
setUninstallCanFail(boolean canFail)228     public void setUninstallCanFail(boolean canFail) {
229         uninstallCanFail = canFail;
230     }
231 
forceIntoUpdate()232     public boolean forceIntoUpdate() {
233         return forceIntoUpdate;
234     }
235 
useForce()236     public boolean useForce() {
237         return useForce;
238     }
239 
setIsNewInstalled(boolean installed)240     public void setIsNewInstalled(boolean installed) {
241         isNewInstalled = installed;
242     }
243 
isNewInstalled()244     public boolean isNewInstalled() {
245         return isNewInstalled;
246     }
247 
setWasAlreadyInstalled(boolean installed)248     public void setWasAlreadyInstalled(boolean installed) {
249         wasAlreadyInstalled = installed;
250     }
251 
wasAlreadyInstalled()252     public boolean wasAlreadyInstalled() {
253         return wasAlreadyInstalled;
254     }
255 
setIgnoreDependsForUninstall(boolean ignore)256     public void setIgnoreDependsForUninstall(boolean ignore) {
257         ignoreDependsForUninstall = ignore;
258     }
259 
ignoreDependsForUninstall()260     public boolean ignoreDependsForUninstall() {
261         return ignoreDependsForUninstall;
262     }
263 
isDefault()264     public boolean isDefault() {
265         return isDefault;
266     }
267 
getMD5()268     public String getMD5() {
269         return pkgMD5Sum;
270     }
271 
isRelocatable()272     public boolean isRelocatable() {
273         return isRelocatable;
274     }
275 
setIsRelocatable(boolean relocatable)276     public void setIsRelocatable(boolean relocatable) {
277         isRelocatable = relocatable;
278     }
279 
getPackageName()280     public String getPackageName() {
281         return pkgFileName;
282     }
283 
setPackageName(String name)284     public void setPackageName(String name) {
285         pkgFileName = name;
286     }
287 
288 
getFullPackageName()289     public String getFullPackageName() {
290         return pkgFullName;
291     }
292 
setFullPackageName(String fullPackageName)293     public void setFullPackageName(String fullPackageName) {
294         pkgFullName = fullPackageName;
295     }
296 
getSelectionState()297     public int getSelectionState() {
298         return usrSelectionState;
299     }
300 
setSelectionState(int state)301     public void setSelectionState(int state) {
302         usrSelectionState = state;
303     }
304 
getCustomSelectionState()305     public int getCustomSelectionState() {
306         return customSelectionState;
307     }
308 
setCustomSelectionState(int state)309     public void setCustomSelectionState(int state) {
310         customSelectionState = state;
311     }
312 
getTypicalSelectionState()313      public int getTypicalSelectionState() {
314         return typicalSelectionState;
315     }
316 
setTypicalSelectionState(int state)317     public void setTypicalSelectionState(int state) {
318         typicalSelectionState = state;
319     }
320 
getStartSelectionState()321     public int getStartSelectionState() {
322         return startSelectionState;
323     }
324 
setStartSelectionState(int state)325     public void setStartSelectionState(int state) {
326         startSelectionState = state;
327     }
328 
isAllChildrenHidden()329     public boolean isAllChildrenHidden() {
330         return allChildrenHidden;
331     }
332 
setAllChildrenHidden(boolean hidden)333     public void setAllChildrenHidden(boolean hidden) {
334         allChildrenHidden = hidden;
335     }
336 
setIsHidden(boolean hidden)337     public void setIsHidden(boolean hidden) {
338         isHidden = hidden;
339     }
340 
pkgExists()341     public boolean pkgExists() {
342         return pkgExists;
343     }
344 
setPkgExists(boolean exists)345     public void setPkgExists(boolean exists) {
346         pkgExists = exists;
347     }
348 
getPkgVersion()349     public String getPkgVersion() {
350         return pkgVersion;
351     }
352 
setPkgVersion(String version)353     public void setPkgVersion(String version) {
354         pkgVersion = version;
355     }
356 
getPkgRelease()357     public String getPkgRelease() {
358         return pkgRelease;
359     }
360 
setPkgRelease(String release)361     public void setPkgRelease(String release) {
362         pkgRelease = release;
363     }
364 
getPkgRealName()365     public String getPkgRealName() {
366         return pkgRealName;
367     }
368 
setPkgRealName(String realName)369     public void setPkgRealName(String realName) {
370         pkgRealName = realName;
371     }
372 
getPkgSubdir()373     public String getPkgSubdir() {
374         return pkgSubdir;
375     }
376 
setPkgSubdir(String subdir)377     public void setPkgSubdir(String subdir) {
378         pkgSubdir = subdir;
379     }
380 
getPkgLanguage()381     public String getPkgLanguage() {
382         return pkgLanguage;
383     }
384 
setPkgLanguage(String language)385     public void setPkgLanguage(String language) {
386         pkgLanguage = language;
387     }
388 
389     /**
390      * extract the name and the description according to the locale
391      */
getLocalizedValue(XMLPackageDescription packageData, String section, Locale l)392     private String getLocalizedValue(XMLPackageDescription packageData, String section, Locale l) {
393         String localizedValue = "";
394 
395         String countryString  = l.getCountry();
396         String languageString = l.getLanguage();
397         String localeString   = languageString + "_" + countryString;
398 
399         XMLPackageDescription subPackage = packageData.getElement(section, "lang", localeString);
400         if (subPackage == null) {
401             subPackage = packageData.getElement(section, "lang", languageString);
402             if (subPackage == null) {
403                 subPackage = packageData.getElement(section, "lang", "en_US");
404             }
405         }
406         if (subPackage != null) {
407             localizedValue = subPackage.getValue();
408         }
409 
410         return localizedValue;
411     }
412 
413     /**
414      * parse the wrapped package description
415      */
416 
parse(XMLPackageDescription data)417     private void parse(XMLPackageDescription data) {
418 
419         XMLPackageDescription section;
420         XMLPackageDescription subSection;
421 
422         /* information about how to display the node */
423         section = data.getElement("display");
424         if (section != null) {
425             /* display types: hidden, visible */
426             String displayType = section.getAttribute("type");
427             if (displayType != null) {
428                 isHidden = displayType.equals("hidden");
429             }
430 
431             /* name and description according to the current locale */
432             Locale locale = Locale.getDefault();
433             dpyName        = getLocalizedValue(section, "name",        locale);
434             dpyDescription = getLocalizedValue(section, "description", locale);
435 
436             subSection = section.getElement("sortkey");
437             if (subSection != null) {
438                 String sort = subSection.getValue();
439                 dpySortKey = Integer.parseInt(sort);
440             }
441 
442             subSection = section.getElement("default");
443             if (subSection != null) {
444                 String defaultValue = subSection.getValue();
445                 isDefault = Parser.parseBoolean(defaultValue);
446                 // isDefault = Boolean.parseBoolean(defaultValue);
447             }
448 
449             subSection = section.getElement("showinuserinstall");
450             if (subSection != null) {
451                 String showInUserInstallValue = subSection.getValue();
452                 showInUserInstall = Parser.parseBoolean(showInUserInstallValue);
453                 // showInUserInstall = Boolean.parseBoolean(showInUserInstallValue);
454             }
455 
456             subSection = section.getElement("showinuserinstallonly");
457             if (subSection != null) {
458                 String showInUserInstallValueOnly = subSection.getValue();
459                 showInUserInstallOnly = Parser.parseBoolean(showInUserInstallValueOnly);
460             }
461 
462             subSection = section.getElement("dontuninstall");
463             if (subSection != null) {
464                 String dontUninstallValue = subSection.getValue();
465                 dontUninstall = Parser.parseBoolean(dontUninstallValue);
466                 // dontUninstall = Boolean.parseBoolean(dontUninstallValue);
467             }
468 
469             subSection = section.getElement("checksolaris");
470             if (subSection != null) {
471                 checkSolaris = subSection.getValue();
472             }
473 
474             subSection = section.getElement("isupdatepackage");
475             if (subSection != null) {
476                 String isUpdatePackageValue = subSection.getValue();
477                 isUpdatePackage = Parser.parseBoolean(isUpdatePackageValue);
478                 // isUpdatePackage = Boolean.parseBoolean(isUpdatePackageValue);
479             }
480 
481             subSection = section.getElement("showmultilingualonly");
482             if (subSection != null) {
483                 String showMultiLingualOnlyValue = subSection.getValue();
484                 showMultiLingualOnly = Parser.parseBoolean(showMultiLingualOnlyValue);
485             }
486 
487             subSection = section.getElement("applicationmodule");
488             if (subSection != null) {
489                 String isApplicationPackageValue = subSection.getValue();
490                 isApplicationPackage = Parser.parseBoolean(isApplicationPackageValue);
491             }
492 
493             subSection = section.getElement("isjavapackage");
494             if (subSection != null) {
495                 String isJavaPackageValue = subSection.getValue();
496                 isJavaPackage = Parser.parseBoolean(isJavaPackageValue);
497                 // isJavaPackage = Boolean.parseBoolean(isJavaPackageValue);
498             }
499 
500             subSection = section.getElement("installcanfail");
501             if (subSection != null) {
502                 String installCanFailValue = subSection.getValue();
503                 installCanFail = Parser.parseBoolean(installCanFailValue);
504             }
505 
506             subSection = section.getElement("forceintoupdate");
507             if (subSection != null) {
508                 String forceIntoUpdateValue = subSection.getValue();
509                 forceIntoUpdate = Parser.parseBoolean(forceIntoUpdateValue);
510             }
511 
512             subSection = section.getElement("useforce");
513             if (subSection != null) {
514                 String useForceValue = subSection.getValue();
515                 useForce = Parser.parseBoolean(useForceValue);
516             }
517 
518         }
519 
520         /* query information about the physical (rpm/pkg/msi...) package itself */
521         section = data.getElement("installunit");
522         if (section != null) {
523             String pkgTypeName = section.getAttribute("type");
524             if (pkgTypeName != null) {
525                 if (pkgTypeName.equals("rpm")) {
526                     pkgType = RPM_UNIT;
527                 } else if (pkgTypeName.equals("pkg")) {
528                     pkgType = PKG_UNIT;
529                 }
530             }
531 
532             subSection = section.getElement("size");
533             if (subSection != null) {
534                 String sz = subSection.getValue();
535                 pkgSize = Integer.parseInt(sz);
536             }
537             subSection = section.getElement("installorder");
538             if (subSection != null) {
539                 String order = subSection.getValue();
540                 pkgOrder = Integer.parseInt(order);
541             } else {
542             	// Setting the default for packages without order
543                 pkgOrder = 1000;
544             }
545             subSection = section.getElement("md5");
546             if (subSection != null) {
547                 pkgMD5Sum = subSection.getValue();
548             }
549             subSection = section.getElement("name");
550             if (subSection != null) {
551                 pkgFileName = subSection.getValue();
552             }
553             subSection = section.getElement("fullpkgname");
554             if (subSection != null) {
555                 pkgFullName = subSection.getValue();
556             }
557             subSection = section.getElement("pkgversion");
558             if (subSection != null) {
559                 pkgVersion = subSection.getValue();
560             }
561             subSection = section.getElement("subdir");
562             if (subSection != null) {
563                 pkgSubdir = subSection.getValue();
564             }
565             subSection = section.getElement("relocatable");
566             if (subSection != null) {
567                 String relocatableValue = subSection.getValue();
568                 isRelocatable = Parser.parseBoolean(relocatableValue);
569                 // isRelocatable = Boolean.parseBoolean(relocatableValue);
570             }
571             subSection = section.getElement("solarislanguage");
572             if (subSection != null) {
573                 pkgLanguage = subSection.getValue();
574             }
575 
576         }
577 
578         /* line up the subpackages */
579         for (Enumeration enumPackages = data.elements(); enumPackages.hasMoreElements(); ) {
580             XMLPackageDescription p = (XMLPackageDescription) enumPackages.nextElement();
581             if (p.getKey().equals("package")) {
582                 children.add(new PackageDescription(p, this));
583             }
584         }
585     }
586 
587     /**
588      * sort according to the display sortkey
589      */
590 
591     private class PackageComparator implements Comparator {
compare(Object w1, Object w2)592         public int compare(Object w1, Object w2) {
593             return ((PackageDescription) w1).dpySortKey - ((PackageDescription) w2).dpySortKey;
594         }
595     }
596 
597     /**
598      * implement a TreeNode interface for convenient travelling through the data
599      */
600 
601     private class PackageEnumeration implements Enumeration {
602 
603         Enumeration e;
604 
PackageEnumeration()605         protected PackageEnumeration() {
606             e = children.elements();
607         }
hasMoreElements()608         public boolean hasMoreElements() {
609             return e.hasMoreElements();
610         }
nextElement()611         public Object nextElement() {
612             return e.nextElement();
613         }
614     }
615 
616     /**
617      * TreeNode interface
618      */
children()619     public Enumeration children() {
620         return new PackageEnumeration();
621     }
622 
getAllowsChildren()623     public boolean getAllowsChildren() {
624         return true;
625     }
626 
627 //    public PackageDescription getChildAt(int childIndex) {
628 //        return (PackageDescription) children.elementAt(childIndex);
629 //    }
630 
getChildAt(int childIndex)631     public TreeNode getChildAt(int childIndex) {
632         return (TreeNode)children.elementAt(childIndex);
633     }
634 
getChildCount()635     public int getChildCount() {
636         return children.size();
637     }
638 
getIndex(TreeNode node)639     public int getIndex(TreeNode node) {
640         return children.indexOf(node);
641     }
642 
643 //    public PackageDescription getParent() {
644 //        return parent;
645 //    }
646 
getParent()647     public TreeNode getParent() {
648         return (TreeNode)parent;
649     }
650 
isLeaf()651     public boolean isLeaf() {
652         return children.size() == 0;
653     }
654 
655 }
656