001: /*
002: * $RCSfile: XmlUtil.java,v $
003: * @modification $Date: 2001/09/28 19:41:42 $
004: * @version $Id: XmlUtil.java,v 1.1 2001/09/28 19:41:42 hfalk Exp $
005: *
006: */
007:
008: package com.memoire.vainstall.builder.util;
009:
010: import com.memoire.vainstall.builder.*;
011:
012: import java.io.File;
013: import java.io.IOException;
014:
015: import javax.xml.parsers.*;
016: import org.xml.sax.*;
017: import org.xml.sax.helpers.*;
018: import org.w3c.dom.*;
019:
020: // ripped VAInstall code
021: import com.ice.jni.registry.*;
022: import com.memoire.vainstall.VAGlobals;
023:
024: /**
025: * This class contains static convenient methods for parsing of XML files
026: * and getting data out of the document
027: *
028: * It also has some non-xml methods which has to be removed when
029: * the builder is properly integrated with the VAInstall code.
030: *
031: * @author Henrik Falk
032: * @version $Id: XmlUtil.java,v 1.1 2001/09/28 19:41:42 hfalk Exp $
033: */
034: public class XmlUtil {
035:
036: public XmlUtil() {
037: super ();
038: }
039:
040: /**
041: * Parse an URI validating or non-validating and returns a Document
042: * @param uri The URI of the document
043: * @param dtd The name of the DTD file
044: * @return a XML Document
045: * @throws com.memoire.vainstall.builder.XmlParseException
046: */
047: public static Document parse(String uri, String dtdname)
048: throws XmlParseException {
049:
050: Document doc = null;
051:
052: try {
053: DocumentBuilderFactory dbf = DocumentBuilderFactory
054: .newInstance();
055: DocumentBuilder db = dbf.newDocumentBuilder();
056: // db.setEntityResolver(new DtdResolver());
057: // db.setErrorHandler(new SAXErrorHandler());
058: doc = (Document) db.parse(uri);
059: } catch (SAXParseException exc) {
060: throw new XmlParseException(exc);
061: } catch (SAXException exc) {
062: throw new XmlParseException(exc.getMessage(),
063: (Locator) null);
064: } catch (IOException exc) {
065: throw new XmlParseException(exc.getMessage(), null);
066: } catch (Exception exc) {
067: throw new XmlParseException(exc.getMessage(), null);
068: }
069:
070: return doc;
071: }
072:
073: /**
074: * Returns the value of an node attribute as string from a NamedNodeMap
075: * @param map a NamedNodeMap
076: * @param attribute a attribute
077: * @return the string value of an attribute for an NamedNodeMap.
078: */
079: public static String getAttribute(NamedNodeMap map, String attribute) {
080:
081: if (map == null) {
082: return null;
083: }
084:
085: Node node = map.getNamedItem(attribute);
086: if (node == null) {
087: return null;
088: }
089:
090: return node.getNodeValue();
091: }
092:
093: /**
094: * Returns the value of an node attribute as integer from a NamedNodeMap
095: * @param map a NamedNodeMap
096: * @param attribute a attribute
097: * @return the integer value of an attribute for an NamedNodeMap.
098: */
099: public static int getAttributeAsInt(NamedNodeMap map,
100: String attribute) {
101:
102: String number = map.getNamedItem(attribute).getNodeValue();
103: return new Integer(number).intValue();
104: }
105:
106: /**
107: * Returns the value of an node attribute as string
108: * @param node a Node
109: * @param attribute a attribute
110: * @return the string value of an attribute for a Node
111: */
112: public static String getAttribute(Node node, String attribute) {
113:
114: NamedNodeMap map = node.getAttributes();
115:
116: return map.getNamedItem(attribute).getNodeValue();
117: }
118:
119: // ****************************************************************
120: // The following code is ripped VAInstall code
121: // ****************************************************************
122:
123: public static final boolean IS_WIN = System.getProperty("os.name")
124: .startsWith("Win");
125: public static final boolean IS_MAC = System.getProperty("os.name")
126: .startsWith("Mac");
127: public static final boolean IS_UNIX = !IS_WIN && !IS_MAC;
128: public static final boolean IS_ROOT = (IS_UNIX && "root"
129: .equals(System.getProperty("user.name")))
130: || (IS_WIN && new File("C:\\").canWrite());
131:
132: public static File findVAISharedDir() {
133: File destPath = null;
134:
135: try {
136: if (IS_ROOT) {
137: if (IS_WIN) {
138: // chercher dans la Regedit l'emplacement de C:\Program Files\Common Files
139: // ou C:\windows\Application Data
140: // Key: HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\CommonFilesDir
141: try {
142: RegistryKey sharedDirKey = Registry.HKEY_LOCAL_MACHINE
143: .openSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion");
144: destPath = new File(sharedDirKey
145: .getStringValue("CommonFilesDir"));
146: sharedDirKey.closeKey();
147: } catch (Exception rex) {
148: destPath = null;
149: }
150: if ((destPath == null) || (!destPath.exists())) {
151: System.out
152: .println("Could not find common dir in registry:");
153: System.out
154: .println("using 'C:\\Program Files\\Common Files'");
155:
156: destPath = new File(
157: "C:\\Program Files\\Common Files");
158: if (!destPath.exists())
159: destPath.mkdirs();
160: }
161: } else if (IS_UNIX) {
162: // on cherche /usr/share
163: destPath = new File("/usr/share");
164: if (!destPath.exists()) {
165: // on cherche /opt/share
166: destPath = new File("/opt/share");
167: }
168: }
169: if (!destPath.exists()) {
170: throw new IOException(VAGlobals
171: .i18n("Setup_CouldNotFindVAInstall"));
172: }
173: destPath = new File(destPath, "vainstall");
174: if (!destPath.exists()) {
175: destPath.mkdirs();
176: }
177: } else { // user mode
178: if (IS_WIN) {
179: destPath = new File(System.getProperty("user.home")
180: + File.separator + "vainstall");
181: } else if (IS_UNIX) {
182: destPath = new File(System.getProperty("user.home")
183: + File.separator + ".vainstall");
184: }
185: if (!destPath.exists()) {
186: destPath.mkdirs();
187: }
188: }
189: } catch (IOException e) {
190: e.printStackTrace();
191: }
192: return destPath;
193: }
194:
195: }
|