KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > memoire > vainstall > builder > util > XmlUtil


1 /*
2  * $RCSfile: XmlUtil.java,v $
3  * @modification $Date: 2001/09/28 19:41:42 $
4  * @version $Id: XmlUtil.java,v 1.1 2001/09/28 19:41:42 hfalk Exp $
5  *
6  */

7
8 package com.memoire.vainstall.builder.util;
9
10 import com.memoire.vainstall.builder.*;
11
12 import java.io.File JavaDoc;
13 import java.io.IOException JavaDoc;
14
15 import javax.xml.parsers.*;
16 import org.xml.sax.*;
17 import org.xml.sax.helpers.*;
18 import org.w3c.dom.*;
19
20 // ripped VAInstall code
21
import com.ice.jni.registry.*;
22 import com.memoire.vainstall.VAGlobals;
23
24 /**
25  * This class contains static convenient methods for parsing of XML files
26  * and getting data out of the document
27  *
28  * It also has some non-xml methods which has to be removed when
29  * the builder is properly integrated with the VAInstall code.
30  *
31  * @author Henrik Falk
32  * @version $Id: XmlUtil.java,v 1.1 2001/09/28 19:41:42 hfalk Exp $
33  */

34 public class XmlUtil {
35
36     public XmlUtil() {
37         super();
38     }
39
40     /**
41      * Parse an URI validating or non-validating and returns a Document
42      * @param uri The URI of the document
43      * @param dtd The name of the DTD file
44      * @return a XML Document
45      * @throws com.memoire.vainstall.builder.XmlParseException
46      */

47     public static Document parse(String JavaDoc uri, String JavaDoc dtdname) throws XmlParseException {
48
49         Document doc=null;
50
51         try {
52             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
53         DocumentBuilder db = dbf.newDocumentBuilder();
54 // db.setEntityResolver(new DtdResolver());
55
// db.setErrorHandler(new SAXErrorHandler());
56
doc = (Document)db.parse(uri);
57         } catch (SAXParseException exc) {
58             throw new XmlParseException(exc);
59         } catch(SAXException exc) {
60             throw new XmlParseException(exc.getMessage(),(Locator)null);
61         } catch(IOException JavaDoc exc) {
62             throw new XmlParseException(exc.getMessage(),null);
63         } catch(Exception JavaDoc exc) {
64             throw new XmlParseException(exc.getMessage(),null);
65         }
66
67         return doc;
68     }
69
70     /**
71      * Returns the value of an node attribute as string from a NamedNodeMap
72      * @param map a NamedNodeMap
73      * @param attribute a attribute
74      * @return the string value of an attribute for an NamedNodeMap.
75      */

76     public static String JavaDoc getAttribute(NamedNodeMap map, String JavaDoc attribute) {
77
78         if (map == null) {
79             return null;
80         }
81
82         Node node = map.getNamedItem(attribute);
83         if (node == null) {
84             return null;
85         }
86
87         return node.getNodeValue();
88     }
89
90     /**
91      * Returns the value of an node attribute as integer from a NamedNodeMap
92      * @param map a NamedNodeMap
93      * @param attribute a attribute
94      * @return the integer value of an attribute for an NamedNodeMap.
95      */

96     public static int getAttributeAsInt(NamedNodeMap map, String JavaDoc attribute) {
97
98         String JavaDoc number = map.getNamedItem(attribute).getNodeValue();
99         return new Integer JavaDoc(number).intValue();
100     }
101
102     /**
103      * Returns the value of an node attribute as string
104      * @param node a Node
105      * @param attribute a attribute
106      * @return the string value of an attribute for a Node
107      */

108     public static String JavaDoc getAttribute(Node node, String JavaDoc attribute) {
109
110         NamedNodeMap map = node.getAttributes();
111
112         return map.getNamedItem(attribute).getNodeValue();
113     }
114
115 // ****************************************************************
116
// The following code is ripped VAInstall code
117
// ****************************************************************
118

119   public static final boolean IS_WIN=
120     System.getProperty("os.name").startsWith("Win");
121   public static final boolean IS_MAC=
122     System.getProperty("os.name").startsWith("Mac");
123   public static final boolean IS_UNIX=
124     !IS_WIN&&!IS_MAC;
125   public static final boolean IS_ROOT=
126     (IS_UNIX && "root".equals(System.getProperty("user.name"))) ||
127     (IS_WIN && new File JavaDoc("C:\\").canWrite());
128
129
130   public static File JavaDoc findVAISharedDir()
131   {
132     File JavaDoc destPath=null;
133
134     try {
135       if( IS_ROOT ) {
136         if( IS_WIN ) {
137           // chercher dans la Regedit l'emplacement de C:\Program Files\Common Files
138
// ou C:\windows\Application Data
139
// Key: HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\CommonFilesDir
140
try {
141             RegistryKey sharedDirKey=Registry.HKEY_LOCAL_MACHINE.openSubKey(
142               "SOFTWARE\\Microsoft\\Windows\\CurrentVersion");
143             destPath=new File JavaDoc(sharedDirKey.getStringValue("CommonFilesDir"));
144             sharedDirKey.closeKey();
145           } catch( Exception JavaDoc rex ) { destPath=null; }
146           if( (destPath==null)||(!destPath.exists()) ) {
147             System.out.println("Could not find common dir in registry:");
148             System.out.println("using 'C:\\Program Files\\Common Files'");
149
150             destPath=new File JavaDoc("C:\\Program Files\\Common Files");
151             if( !destPath.exists() ) destPath.mkdirs();
152           }
153         } else
154         if( IS_UNIX ) {
155           // on cherche /usr/share
156
destPath=new File JavaDoc("/usr/share");
157           if( !destPath.exists() ) {
158             // on cherche /opt/share
159
destPath=new File JavaDoc("/opt/share");
160           }
161         }
162         if( !destPath.exists() ) {
163           throw new IOException JavaDoc(
164             VAGlobals.i18n("Setup_CouldNotFindVAInstall"));
165         }
166         destPath=new File JavaDoc(destPath, "vainstall");
167         if( !destPath.exists() ) {
168           destPath.mkdirs();
169         }
170       } else { // user mode
171
if( IS_WIN ) {
172           destPath=new File JavaDoc(System.getProperty("user.home")+File.separator+
173                             "vainstall");
174         } else
175         if( IS_UNIX ) {
176           destPath=new File JavaDoc(System.getProperty("user.home")+File.separator+
177                             ".vainstall");
178         }
179         if( !destPath.exists() ) {
180           destPath.mkdirs();
181         }
182       }
183     } catch( IOException JavaDoc e ) {
184       e.printStackTrace();
185     }
186     return destPath;
187   }
188
189 }
190
191
Popular Tags