GrxXmlUtil.java
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008, AIST, the University of Tokyo and General Robotix Inc.
3  * All rights reserved. This program is made available under the terms of the
4  * Eclipse Public License v1.0 which accompanies this distribution, and is
5  * available at http://www.eclipse.org/legal/epl-v10.html
6  * Contributors:
7  * General Robotix Inc.
8  * National Institute of Advanced Industrial Science and Technology (AIST)
9  */
10 /*
11  * GrxXmlUtil.java
12  *
13  * Copyright (C) 2007 GeneralRobotix, Inc.
14  * All Rights Reserved
15  *
16  * @author Yuichiro kawasumi (GeneralRobotix, Inc)
17  */
18 
19 package com.generalrobotix.ui.util;
20 
21 import java.awt.Dimension;
22 import java.io.File;
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;
28 
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;
39 
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;
45 
47 
48 public class GrxXmlUtil {
49  public static Document doc_ = null;
50  public static Element root_ = null;
51  private static String fname_ = "property.xml";
52 
53  public static void initialize(String fname) {
54  if (!fname.equals(fname_)) {
55  fname_ = fname;
56  root_ = null;
57  update();
58  }
59  }
60 
61  public static void update() {
62  try {
63  DocumentBuilderFactory dbfactory = DocumentBuilderFactory
64  .newInstance();
65  DocumentBuilder builder = dbfactory.newDocumentBuilder();
66  doc_ = builder.parse(new File(fname_));
67  root_ = doc_.getDocumentElement();
68  } catch (ParserConfigurationException e) {
69  GrxDebugUtil.printErr("getRoot:", e);
70  } catch (SAXException e) {
71  GrxDebugUtil.printErr("getRoot:", e);
72  } catch (IOException e) {
73  GrxDebugUtil.printErr("getRoot:", e);
74  }
75  }
76 
77  public static NodeList getPropertyElements(String tag) {
78  if (root_ == null)
79  update();
80  return root_.getElementsByTagName(tag);
81  }
82 
83  public static Element getElement(String tag, String attrName, String attrValue) {
84  if (root_ == null)
85  update();
86  return getElement(root_, tag, attrName, attrValue);
87  }
88 
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)) {
95  return ret;
96  }
97  }
98  return null;
99  }
100 
101  public static Element getElement(String[] path) {
102  if (root_ == null)
103  update();
104  Element e = root_;
105  for (int i = 0; i < path.length; i++) {
106  if (e == null)
107  return null;
108  NodeList l = e.getElementsByTagName(path[i]);
109  if (l != null)
110  e = (Element) l.item(0);
111  }
112  return e;
113  }
114 
115  public static double[] getXYZDouble(String[] path) {
116  double[] ret = null;
117  Element e = getElement(path);
118  if (e != null) {
119  ret = new double[3];
120  try {
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) {
125  return null;
126  }
127  }
128  return ret;
129  }
130 
131  public static Vector3d getVector3d(String[] path) {
132  double[] val = getXYZDouble(path);
133  if (val != null)
134  return new Vector3d(val);
135  return null;
136  }
137 
138  public static void setXYZDouble(String[] path, double[] val) {
139  Element e = getElement(path);
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]));
144  }
145  }
146 
147  public static Quat4d getQuat4d(String[] path) {
148  double[] val = getQuatDouble(path);
149  if (val != null)
150  return new Quat4d(val);
151  return null;
152  }
153 
154  public static double[] getQuatDouble(String[] path) {
155  double[] ret = null;
156  Element e = getElement(path);
157  if (e != null) {
158  ret = new double[4];
159  try {
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) {
165  return null;
166  }
167  }
168  return ret;
169  }
170 
171  public static void setQuatDouble(String[] path, double[] val) {
172  Element e = getElement(path);
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]));
178  }
179  }
180 
181  public static Double getDouble(String[] path, String atr) {
182  return getDouble(getElement(path), atr);
183  }
184 
185  public static Double getDouble(Element e, String atr) {
186  if (e != null) {
187  String str = expandEnvVal(e.getAttribute(atr));
188  if (!str.equals("")) {
189  try {
190  return Double.parseDouble(str);
191  } catch (Exception ex) {
192  return null;
193  }
194  }
195  }
196  return null;
197  }
198 
199  public static void setDouble(String[] path, String atr, double val) {
200  setDouble(getElement(path), atr, val);
201  }
202 
203  public static void setDouble(Element e, String atr, double val) {
204  if (e != null)
205  e.setAttribute(atr, String.valueOf(val));
206  }
207 
208  public static Integer getInteger(String[] path, String atr, int defaultValue) {
209  return getInteger(getElement(path), atr, defaultValue);
210  }
211 
212  public static Integer getInteger(Element e, String atr, int defaultValue) {
213  if (e != null) {
214  String str = expandEnvVal(e.getAttribute(atr));
215  if (!str.equals("")) {
216  try {
217  return Integer.parseInt(str);
218  } catch (Exception ex) {
219  return defaultValue;
220  }
221  }
222  }
223  return defaultValue;
224  }
225 
226  public static void setInteger(String[] path, String atr, int val) {
227  setInteger(getElement(path), atr, val);
228  }
229 
230  public static void setInteger(Element e, String atr, int val) {
231  if (e != null)
232  e.setAttribute(atr, String.valueOf(val));
233  }
234 
235  public static Boolean getBoolean(String[] path, String atr,
236  boolean defaultValue) {
237  return getBoolean(getElement(path), atr, defaultValue);
238  }
239 
240  public static Boolean getBoolean(Element e, String atr, boolean defaultValue) {
241  if (e != null) {
242  String str = expandEnvVal(e.getAttribute(atr));
243  if (!str.equals("")) {
244  try {
245  return Boolean.parseBoolean(str);
246  } catch (Exception ex) {
247  return defaultValue;
248  }
249  }
250  }
251  return defaultValue;
252  }
253 
254  public static void setBoolean(String[] path, String atr, boolean b) {
255  setBoolean(getElement(path), atr, b);
256  }
257 
258  public static void setBoolean(Element e, String atr, boolean b) {
259  if (e != null) {
260  e.setAttribute(atr, String.valueOf(b));
261  }
262  }
263 
264  public static String getString(String[] path, String atr,
265  String defaultValue) {
266  return getString(getElement(path), atr, defaultValue);
267  }
268 
269  public static String getString(Element e, String atr, String defaultValue) {
270  if (e == null)
271  return defaultValue;
272 
273  String str = e.getAttribute(atr);
274  if (str.equals(""))
275  return defaultValue;
276 
277  return expandEnvVal(str);
278  }
279 
280  public static String getStringNoexpand(String[] path, String atr,
281  String defaultValue) {
282  return getStringNoexpand(getElement(path), atr, defaultValue);
283  }
284 
285  public static String getStringNoexpand(Element e, String atr, String defaultValue) {
286  if (e == null)
287  return defaultValue;
288 
289  String str = e.getAttribute(atr);
290  if (str.equals(""))
291  return defaultValue;
292 
293  return str;
294  }
295 
296  public static String expandEnvVal(String str) {
297  if (str == null)
298  return null;
299  for (int i = 0;; i++) {
300  int idx1, idx2;
301  if ((idx1 = str.indexOf("$(")) == -1)
302  break;
303  if ((idx2 = str.indexOf(")", idx1)) != -1) {
304  String key = str.substring(idx1 + 2, idx2);
305  String val = System.getProperty(key);
306  if(val==null){
307  val = Activator.getDefault().getPreferenceStore().getString(key);
308  if(val.equals("")){
309  val = System.getenv(key);
310  if (val == null)
311  val = "";
312  }
313  }
314  str = str.replace("$(" + key + ")", val);
315  }
316  }
317  return str;
318  }
319 
320  public static String replaceEnvVal(File f) {
321  String file=null;
322  try {
323  file = f.getCanonicalPath();
324  } catch (IOException e) {
325  e.printStackTrace();
326  return f.getAbsolutePath();
327  }
328 
329  String dir = System.getProperty("CURRENT_DIR");
330  if(dir!=null){
331  File dirf = new File(dir);
332  try {
333  dir = dirf.getCanonicalPath();
334  } catch (IOException e) {
335  e.printStackTrace();
336  return f.getAbsolutePath();
337  }
338 
339  Pattern localPattern = Pattern.compile("\\\\");
340  Matcher localMatcher = localPattern.matcher(File.separator);
341  String localStr = localMatcher.replaceAll("\\\\\\\\");
342 
343  String[] dirs = dir.split(localStr,-1);
344  String[] files = file.split(localStr,-1);
345  int i=0;
346  for( ; i<dirs.length; i++){
347  if(!dirs[i].equals(files[i]))
348  break;
349  }
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++)
354  ret += "/..";
355  for(int j=i ; j<files.length; j++)
356  ret += "/" + files[j];
357  return ret;
358  }
359  return f.getAbsolutePath();
360  }
361 
362  public static void setString(String[] path, String atr, String str) {
363  setString(getElement(path), atr, str);
364  }
365 
366  public static void setString(Element e, String atr, String str) {
367  if (e != null)
368  e.setAttribute(atr, str);
369  }
370 
371  public static Dimension getSize(String[] path, Dimension defaultValue) {
372  return getSize(getElement(path), defaultValue);
373  }
374 
375  public static Dimension getSize(Element e, Dimension defaultValue) {
376  if (e != null) {
377  Dimension ret = new Dimension();
378  ret.width = getInteger(e, "width", defaultValue.width);
379  ret.height = getInteger(e, "height", defaultValue.height);
380  return ret;
381  }
382  return defaultValue;
383  }
384 
385  public static void setSize(Element e, Dimension d) {
386  if (e != null) {
387  setInteger(e, "width", d.width);
388  setInteger(e, "height", d.height);
389  }
390  }
391 
392  public static Element appendNewElement(Node n, String tagName, int depth) {
393  return appendNewElement(doc_, n, tagName, depth);
394  }
395 
396  public static Element appendNewElement(Document doc, Node n, String tagName, int depth) {
397  String t = null;
398  if (n == null)
399  n = root_;
400  if (!n.hasChildNodes() || n == root_) {
401  t = "\n";
402  for (int i = 0; i < depth; i++)
403  t += " ";
404  } else
405  t = " ";
406  n.appendChild(doc.createTextNode(t));
407 
408  Element e = doc.createElement(tagName);
409  n.appendChild(e);
410 
411  t = "\n";
412  for (int i = 0; i < depth - 1; i++)
413  t += " ";
414  n.appendChild(doc.createTextNode(t));
415 
416  return e;
417  }
418 
419  public static Element createElement(Document doc, String tagName) {
420  return createElement(doc_, tagName);
421  }
422 
423  public static Element createElement(String tagName) {
424  return doc_.createElement(tagName);
425  }
426 
427  public static void store() {
428  store(fname_);
429  }
430 
431  public static void store(String fname) {
432  store(doc_, fname);
433  }
434 
435  public static void store(Document doc, String fname) {
436  //File f = new File(fname_);
437  //f.renameTo(new File(fname_+"~"));
438  TransformerFactory tff = TransformerFactory.newInstance();
439  try {
440  Transformer tf = tff.newTransformer();
441  javax.xml.transform.dom.DOMSource src = new DOMSource();
442  src.setNode(doc);
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) {
447  e.printStackTrace();
448  } catch (FileNotFoundException e) {
449  e.printStackTrace();
450  } catch (TransformerException e) {
451  e.printStackTrace();
452  }
453  }
454 }
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
Definition: IceTypes.h:57
static Double getDouble(Element e, String atr)
static NodeList getPropertyElements(String tag)
Definition: GrxXmlUtil.java:77
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)
png_uint_32 i
Definition: png.h:2735
static void setSize(Element e, Dimension d)
long b
Definition: jpegint.h:371
static void setString(String[] path, String atr, String str)
static Vector3d getVector3d(String[] path)
static void store(String fname)
static void setBoolean(Element e, String atr, boolean b)
static double [] getQuatDouble(String[] path)
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)
int val
Definition: jpeglib.h:956
t
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)
Definition: GrxXmlUtil.java:53
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)
Definition: GrxXmlUtil.java:89
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)
org
static void setInteger(String[] path, String atr, int val)
static String replaceEnvVal(File f)
static Element getElement(String tag, String attrName, String attrValue)
Definition: GrxXmlUtil.java:83
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)


openhrp3
Author(s): AIST, General Robotix Inc., Nakamura Lab of Dept. of Mechano Informatics at University of Tokyo
autogenerated on Thu Sep 8 2022 02:24:03