19 package com.generalrobotix.ui.util;
21 import java.awt.Dimension;
23 import java.io.FileNotFoundException;
24 import java.io.FileOutputStream;
25 import java.io.IOException;
26 import java.util.regex.Matcher;
27 import java.util.regex.Pattern;
29 import javax.vecmath.Quat4d;
30 import javax.vecmath.Vector3d;
31 import javax.xml.parsers.DocumentBuilder;
32 import javax.xml.parsers.DocumentBuilderFactory;
33 import javax.xml.parsers.ParserConfigurationException;
34 import javax.xml.transform.Transformer;
35 import javax.xml.transform.TransformerConfigurationException;
36 import javax.xml.transform.TransformerException;
37 import javax.xml.transform.TransformerFactory;
38 import javax.xml.transform.dom.DOMSource;
40 import org.w3c.dom.Document;
41 import org.w3c.dom.Element;
42 import org.w3c.dom.Node;
43 import org.w3c.dom.NodeList;
44 import org.xml.sax.SAXException;
51 private static String
fname_ =
"property.xml";
54 if (!fname.equals(fname_)) {
63 DocumentBuilderFactory dbfactory = DocumentBuilderFactory
65 DocumentBuilder builder = dbfactory.newDocumentBuilder();
66 doc_ = builder.parse(
new File(fname_));
67 root_ = doc_.getDocumentElement();
68 }
catch (ParserConfigurationException e) {
70 }
catch (SAXException e) {
72 }
catch (IOException e) {
80 return root_.getElementsByTagName(tag);
83 public static Element
getElement(String tag, String attrName, String attrValue) {
86 return getElement(root_, tag, attrName, attrValue);
89 public static Element
getElement(Element e, String tag, String attrName, String attrValue) {
90 NodeList l = e.getElementsByTagName(tag);
91 for (
int i = 0;
i < l.getLength();
i++) {
92 Element
ret = (Element) l.item(
i);
93 String mode = ret.getAttribute(attrName);
94 if (mode.equals(attrValue)) {
105 for (
int i = 0;
i < path.length;
i++) {
108 NodeList l = e.getElementsByTagName(path[
i]);
110 e = (Element) l.item(0);
121 ret[0] = Double.parseDouble(e.getAttribute(
"x"));
122 ret[1] = Double.parseDouble(e.getAttribute(
"y"));
123 ret[2] = Double.parseDouble(e.getAttribute(
"z"));
124 }
catch (Exception ex) {
134 return new Vector3d(val);
140 if (e !=
null && val.length == 3) {
141 e.setAttribute(
"x", String.valueOf(val[0]));
142 e.setAttribute(
"y", String.valueOf(val[1]));
143 e.setAttribute(
"z", String.valueOf(val[2]));
150 return new Quat4d(val);
160 ret[0] = Double.parseDouble(e.getAttribute(
"q1"));
161 ret[1] = Double.parseDouble(e.getAttribute(
"q2"));
162 ret[2] = Double.parseDouble(e.getAttribute(
"q3"));
163 ret[3] = Double.parseDouble(e.getAttribute(
"q4"));
164 }
catch (Exception ex) {
173 if (e !=
null && val.length == 4) {
174 e.setAttribute(
"q1", String.valueOf(val[0]));
175 e.setAttribute(
"q2", String.valueOf(val[1]));
176 e.setAttribute(
"q3", String.valueOf(val[2]));
177 e.setAttribute(
"q4", String.valueOf(val[3]));
181 public static Double
getDouble(String[] path, String atr) {
188 if (!str.equals(
"")) {
190 return Double.parseDouble(str);
191 }
catch (Exception ex) {
205 e.setAttribute(atr, String.valueOf(val));
208 public static Integer
getInteger(String[] path, String atr,
int defaultValue) {
212 public static Integer
getInteger(Element e, String atr,
int defaultValue) {
215 if (!str.equals(
"")) {
217 return Integer.parseInt(str);
218 }
catch (Exception ex) {
232 e.setAttribute(atr, String.valueOf(val));
236 boolean defaultValue) {
240 public static Boolean
getBoolean(Element e, String atr,
boolean defaultValue) {
243 if (!str.equals(
"")) {
245 return Boolean.parseBoolean(str);
246 }
catch (Exception ex) {
254 public static void setBoolean(String[] path, String atr,
boolean b) {
260 e.setAttribute(atr, String.valueOf(b));
264 public static String
getString(String[] path, String atr,
265 String defaultValue) {
269 public static String
getString(Element e, String atr, String defaultValue) {
273 String str = e.getAttribute(atr);
281 String defaultValue) {
289 String str = e.getAttribute(atr);
299 for (
int i = 0;;
i++) {
301 if ((idx1 = str.indexOf(
"$(")) == -1)
303 if ((idx2 = str.indexOf(
")", idx1)) != -1) {
304 String key = str.substring(idx1 + 2, idx2);
305 String
val = System.getProperty(key);
309 val = System.getenv(key);
314 str = str.replace(
"$(" + key +
")", val);
323 file = f.getCanonicalPath();
324 }
catch (IOException e) {
326 return f.getAbsolutePath();
329 String dir = System.getProperty(
"CURRENT_DIR");
333 dir = dirf.getCanonicalPath();
334 }
catch (IOException e) {
336 return f.getAbsolutePath();
339 Pattern localPattern = Pattern.compile(
"\\\\");
340 Matcher localMatcher = localPattern.matcher(
File.separator);
341 String localStr = localMatcher.replaceAll(
"\\\\\\\\");
343 String[] dirs = dir.split(localStr,-1);
344 String[] files = file.split(localStr,-1);
346 for( ; i<dirs.length; i++){
347 if(!dirs[i].equals(files[i]))
350 String
ret =
"$(CURRENT_DIR)";
351 if(System.getProperty(
"os.name").equals(
"Windows") && i==0)
352 return f.getAbsolutePath();
353 for(
int j=i ; j<dirs.length; j++)
355 for(
int j=i ; j<files.length; j++)
356 ret +=
"/" + files[j];
359 return f.getAbsolutePath();
362 public static void setString(String[] path, String atr, String str) {
366 public static void setString(Element e, String atr, String str) {
368 e.setAttribute(atr, str);
371 public static Dimension
getSize(String[] path, Dimension defaultValue) {
375 public static Dimension
getSize(Element e, Dimension defaultValue) {
377 Dimension
ret =
new Dimension();
378 ret.width =
getInteger(e,
"width", defaultValue.width);
379 ret.height =
getInteger(e,
"height", defaultValue.height);
385 public static void setSize(Element e, Dimension d) {
400 if (!n.hasChildNodes() || n ==
root_) {
402 for (
int i = 0;
i < depth;
i++)
406 n.appendChild(doc.createTextNode(t));
408 Element e = doc.createElement(tagName);
412 for (
int i = 0;
i < depth - 1;
i++)
414 n.appendChild(doc.createTextNode(t));
424 return doc_.createElement(tagName);
431 public static void store(String fname) {
435 public static void store(Document doc, String fname) {
438 TransformerFactory tff = TransformerFactory.newInstance();
440 Transformer tf = tff.newTransformer();
441 javax.xml.transform.dom.DOMSource src =
new DOMSource();
443 javax.xml.transform.stream.StreamResult target =
new javax.xml.transform.stream.StreamResult();
444 target.setOutputStream(
new FileOutputStream(
new File(fname)));
445 tf.transform(src, target);
446 }
catch (TransformerConfigurationException e) {
448 }
catch (FileNotFoundException e) {
450 }
catch (TransformerException e) {
static String getString(Element e, String atr, String defaultValue)
static Element appendNewElement(Document doc, Node n, String tagName, int depth)
static void setString(Element e, String atr, String str)
#define null
our own NULL pointer
static Double getDouble(Element e, String atr)
static NodeList getPropertyElements(String tag)
static double [] getXYZDouble(String[] path)
RTC::ReturnCode_t ret(RTC::Local::ReturnCode_t r)
static Element createElement(String tagName)
static Double getDouble(String[] path, String atr)
static void setSize(Element e, Dimension d)
static void setString(String[] path, String atr, String str)
static Vector3d getVector3d(String[] path)
static void store(String fname)
static void printErr(String s)
static void setBoolean(Element e, String atr, boolean b)
static double [] getQuatDouble(String[] path)
static Activator getDefault()
static void setDouble(String[] path, String atr, double val)
static Dimension getSize(String[] path, Dimension defaultValue)
static void setInteger(Element e, String atr, int val)
static Integer getInteger(Element e, String atr, int defaultValue)
static void setBoolean(String[] path, String atr, boolean b)
static void setDouble(Element e, String atr, double val)
static Element createElement(Document doc, String tagName)
static String getStringNoexpand(String[] path, String atr, String defaultValue)
static String expandEnvVal(String str)
static void initialize(String fname)
static String getStringNoexpand(Element e, String atr, String defaultValue)
static Quat4d getQuat4d(String[] path)
static String getString(String[] path, String atr, String defaultValue)
static void setXYZDouble(String[] path, double[] val)
static Boolean getBoolean(Element e, String atr, boolean defaultValue)
static Element getElement(Element e, String tag, String attrName, String attrValue)
static void setQuatDouble(String[] path, double[] val)
static Dimension getSize(Element e, Dimension defaultValue)
static void store(Document doc, String fname)
static Element getElement(String[] path)
static void setInteger(String[] path, String atr, int val)
static String replaceEnvVal(File f)
static Element getElement(String tag, String attrName, String attrValue)
static Boolean getBoolean(String[] path, String atr, boolean defaultValue)
static Integer getInteger(String[] path, String atr, int defaultValue)
static Element appendNewElement(Node n, String tagName, int depth)