GrxXmlUtil.java
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2008, AIST, the University of Tokyo and General Robotix Inc.
00003  * All rights reserved. This program is made available under the terms of the
00004  * Eclipse Public License v1.0 which accompanies this distribution, and is
00005  * available at http://www.eclipse.org/legal/epl-v10.html
00006  * Contributors:
00007  * General Robotix Inc.
00008  * National Institute of Advanced Industrial Science and Technology (AIST) 
00009  */
00010 /*
00011  *  GrxXmlUtil.java
00012  *
00013  *  Copyright (C) 2007 GeneralRobotix, Inc.
00014  *  All Rights Reserved
00015  *
00016  *  @author Yuichiro kawasumi (GeneralRobotix, Inc)
00017  */
00018 
00019 package com.generalrobotix.ui.util;
00020 
00021 import java.awt.Dimension;
00022 import java.io.File;
00023 import java.io.FileNotFoundException;
00024 import java.io.FileOutputStream;
00025 import java.io.IOException;
00026 import java.util.regex.Matcher;
00027 import java.util.regex.Pattern;
00028 
00029 import javax.vecmath.Quat4d;
00030 import javax.vecmath.Vector3d;
00031 import javax.xml.parsers.DocumentBuilder;
00032 import javax.xml.parsers.DocumentBuilderFactory;
00033 import javax.xml.parsers.ParserConfigurationException;
00034 import javax.xml.transform.Transformer;
00035 import javax.xml.transform.TransformerConfigurationException;
00036 import javax.xml.transform.TransformerException;
00037 import javax.xml.transform.TransformerFactory;
00038 import javax.xml.transform.dom.DOMSource;
00039 
00040 import org.w3c.dom.Document;
00041 import org.w3c.dom.Element;
00042 import org.w3c.dom.Node;
00043 import org.w3c.dom.NodeList;
00044 import org.xml.sax.SAXException;
00045 
00046 import com.generalrobotix.ui.grxui.Activator;
00047 
00048 public class GrxXmlUtil {
00049         public static Document doc_ = null;
00050         public static Element root_ = null;
00051         private static String fname_ = "property.xml";
00052 
00053         public static void initialize(String fname) {
00054                 if (!fname.equals(fname_)) {
00055                         fname_ = fname;
00056                         root_ = null;
00057                         update();
00058                 }
00059         }
00060 
00061         public static void update() {
00062                 try {
00063                         DocumentBuilderFactory dbfactory = DocumentBuilderFactory
00064                                         .newInstance();
00065                         DocumentBuilder builder = dbfactory.newDocumentBuilder();
00066                         doc_ = builder.parse(new File(fname_));
00067                         root_ = doc_.getDocumentElement();
00068                 } catch (ParserConfigurationException e) {
00069                         GrxDebugUtil.printErr("getRoot:", e);
00070                 } catch (SAXException e) {
00071                         GrxDebugUtil.printErr("getRoot:", e);
00072                 } catch (IOException e) {
00073                         GrxDebugUtil.printErr("getRoot:", e);
00074                 }
00075         }
00076 
00077         public static NodeList getPropertyElements(String tag) {
00078                 if (root_ == null)
00079                         update();
00080                 return root_.getElementsByTagName(tag);
00081         }
00082 
00083         public static Element getElement(String tag, String attrName, String attrValue) {
00084                 if (root_ == null)
00085                         update();
00086                 return getElement(root_, tag, attrName, attrValue);
00087         }
00088 
00089         public static Element getElement(Element e, String tag, String attrName, String attrValue) {
00090                 NodeList l = e.getElementsByTagName(tag);
00091                 for (int i = 0; i < l.getLength(); i++) {
00092                         Element ret = (Element) l.item(i);
00093                         String mode = ret.getAttribute(attrName);
00094                         if (mode.equals(attrValue)) {
00095                                 return ret;
00096                         }
00097                 }
00098                 return null;
00099         }
00100 
00101         public static Element getElement(String[] path) {
00102                 if (root_ == null)
00103                         update();
00104                 Element e = root_;
00105                 for (int i = 0; i < path.length; i++) {
00106                         if (e == null)
00107                                 return null;
00108                         NodeList l = e.getElementsByTagName(path[i]);
00109                         if (l != null)
00110                                 e = (Element) l.item(0);
00111                 }
00112                 return e;
00113         }
00114 
00115         public static double[] getXYZDouble(String[] path) {
00116                 double[] ret = null;
00117                 Element e = getElement(path);
00118                 if (e != null) {
00119                         ret = new double[3];
00120                         try {
00121                                 ret[0] = Double.parseDouble(e.getAttribute("x"));
00122                                 ret[1] = Double.parseDouble(e.getAttribute("y"));
00123                                 ret[2] = Double.parseDouble(e.getAttribute("z"));
00124                         } catch (Exception ex) {
00125                                 return null;
00126                         }
00127                 }
00128                 return ret;
00129         }
00130 
00131         public static Vector3d getVector3d(String[] path) {
00132                 double[] val = getXYZDouble(path);
00133                 if (val != null)
00134                         return new Vector3d(val);
00135                 return null;
00136         }
00137 
00138         public static void setXYZDouble(String[] path, double[] val) {
00139                 Element e = getElement(path);
00140                 if (e != null && val.length == 3) {
00141                         e.setAttribute("x", String.valueOf(val[0]));
00142                         e.setAttribute("y", String.valueOf(val[1]));
00143                         e.setAttribute("z", String.valueOf(val[2]));
00144                 }
00145         }
00146 
00147         public static Quat4d getQuat4d(String[] path) {
00148                 double[] val = getQuatDouble(path);
00149                 if (val != null)
00150                         return new Quat4d(val);
00151                 return null;
00152         }
00153 
00154         public static double[] getQuatDouble(String[] path) {
00155                 double[] ret = null;
00156                 Element e = getElement(path);
00157                 if (e != null) {
00158                         ret = new double[4];
00159                         try {
00160                                 ret[0] = Double.parseDouble(e.getAttribute("q1"));
00161                                 ret[1] = Double.parseDouble(e.getAttribute("q2"));
00162                                 ret[2] = Double.parseDouble(e.getAttribute("q3"));
00163                                 ret[3] = Double.parseDouble(e.getAttribute("q4"));
00164                         } catch (Exception ex) {
00165                                 return null;
00166                         }
00167                 }
00168                 return ret;
00169         }
00170 
00171         public static void setQuatDouble(String[] path, double[] val) {
00172                 Element e = getElement(path);
00173                 if (e != null && val.length == 4) {
00174                         e.setAttribute("q1", String.valueOf(val[0]));
00175                         e.setAttribute("q2", String.valueOf(val[1]));
00176                         e.setAttribute("q3", String.valueOf(val[2]));
00177                         e.setAttribute("q4", String.valueOf(val[3]));
00178                 }
00179         }
00180 
00181         public static Double getDouble(String[] path, String atr) {
00182                 return getDouble(getElement(path), atr);
00183         }
00184 
00185         public static Double getDouble(Element e, String atr) {
00186                 if (e != null) {
00187                         String str = expandEnvVal(e.getAttribute(atr));
00188                         if (!str.equals("")) {
00189                                 try {
00190                                         return Double.parseDouble(str);
00191                                 } catch (Exception ex) {
00192                                         return null;
00193                                 }
00194                         }
00195                 }
00196                 return null;
00197         }
00198 
00199         public static void setDouble(String[] path, String atr, double val) {
00200                 setDouble(getElement(path), atr, val);
00201         }
00202 
00203         public static void setDouble(Element e, String atr, double val) {
00204                 if (e != null)
00205                         e.setAttribute(atr, String.valueOf(val));
00206         }
00207 
00208         public static Integer getInteger(String[] path, String atr, int defaultValue) {
00209                 return getInteger(getElement(path), atr, defaultValue);
00210         }
00211 
00212         public static Integer getInteger(Element e, String atr, int defaultValue) {
00213                 if (e != null) {
00214                         String str = expandEnvVal(e.getAttribute(atr));
00215                         if (!str.equals("")) {
00216                                 try {
00217                                         return Integer.parseInt(str);
00218                                 } catch (Exception ex) {
00219                                         return defaultValue;
00220                                 }
00221                         }
00222                 }
00223                 return defaultValue;
00224         }
00225 
00226         public static void setInteger(String[] path, String atr, int val) {
00227                 setInteger(getElement(path), atr, val);
00228         }
00229 
00230         public static void setInteger(Element e, String atr, int val) {
00231                 if (e != null)
00232                         e.setAttribute(atr, String.valueOf(val));
00233         }
00234 
00235         public static Boolean getBoolean(String[] path, String atr,
00236                         boolean defaultValue) {
00237                 return getBoolean(getElement(path), atr, defaultValue);
00238         }
00239 
00240         public static Boolean getBoolean(Element e, String atr, boolean defaultValue) {
00241                 if (e != null) {
00242                         String str = expandEnvVal(e.getAttribute(atr));
00243                         if (!str.equals("")) {
00244                                 try {
00245                                         return Boolean.parseBoolean(str);
00246                                 } catch (Exception ex) {
00247                                         return defaultValue;
00248                                 }
00249                         }
00250                 }
00251                 return defaultValue;
00252         }
00253 
00254         public static void setBoolean(String[] path, String atr, boolean b) {
00255                 setBoolean(getElement(path), atr, b);
00256         }
00257 
00258         public static void setBoolean(Element e, String atr, boolean b) {
00259                 if (e != null) {
00260                         e.setAttribute(atr, String.valueOf(b));
00261                 }
00262         }
00263 
00264         public static String getString(String[] path, String atr,
00265                         String defaultValue) {
00266                 return getString(getElement(path), atr, defaultValue);
00267         }
00268 
00269         public static String getString(Element e, String atr, String defaultValue) {
00270                 if (e == null)
00271                         return defaultValue;
00272 
00273                 String str = e.getAttribute(atr);
00274                 if (str.equals(""))
00275                         return defaultValue;
00276 
00277                 return expandEnvVal(str);
00278         }
00279 
00280         public static String getStringNoexpand(String[] path, String atr,
00281                         String defaultValue) {
00282                 return getStringNoexpand(getElement(path), atr, defaultValue);
00283         }
00284 
00285         public static String getStringNoexpand(Element e, String atr, String defaultValue) {
00286                 if (e == null)
00287                         return defaultValue;
00288 
00289                 String str = e.getAttribute(atr);
00290                 if (str.equals(""))
00291                         return defaultValue;
00292 
00293                 return str;
00294         }
00295 
00296         public static String expandEnvVal(String str) {
00297                 if (str == null)
00298                         return null;
00299                 for (int i = 0;; i++) {
00300                         int idx1, idx2;
00301                         if ((idx1 = str.indexOf("$(")) == -1)
00302                                 break;
00303                         if ((idx2 = str.indexOf(")", idx1)) != -1) {
00304                                 String key = str.substring(idx1 + 2, idx2);
00305                                 String val = System.getProperty(key);
00306                                 if(val==null){
00307                                         val = Activator.getDefault().getPreferenceStore().getString(key);
00308                                         if(val.equals("")){
00309                                                 val = System.getenv(key);                       
00310                                                 if (val == null)
00311                                                         val = "";
00312                                         }
00313                                 }
00314                                 str = str.replace("$(" + key + ")", val);
00315                         }
00316                 }
00317                 return str;
00318         }
00319         
00320         public static String replaceEnvVal(File f) {
00321                 String file=null;
00322                 try {
00323                         file = f.getCanonicalPath();
00324                 } catch (IOException e) {
00325                         e.printStackTrace();
00326                         return f.getAbsolutePath();
00327                 }
00328 
00329                 String dir = System.getProperty("CURRENT_DIR");
00330                 if(dir!=null){
00331                         File dirf = new File(dir);
00332                         try {
00333                                 dir = dirf.getCanonicalPath();
00334                         } catch (IOException e) {
00335                                 e.printStackTrace();
00336                                 return f.getAbsolutePath();
00337                         }
00338                         
00339                         Pattern localPattern = Pattern.compile("\\\\");
00340                         Matcher localMatcher = localPattern.matcher(File.separator);
00341                         String localStr = localMatcher.replaceAll("\\\\\\\\");
00342                         
00343                         String[] dirs = dir.split(localStr,-1);
00344                         String[] files = file.split(localStr,-1);
00345                         int i=0;
00346                         for( ; i<dirs.length; i++){
00347                                 if(!dirs[i].equals(files[i]))
00348                                         break;
00349                         }
00350                         String ret = "$(CURRENT_DIR)";
00351                         if(System.getProperty("os.name").equals("Windows") && i==0)
00352                                 return f.getAbsolutePath();
00353                         for(int j=i ; j<dirs.length; j++)
00354                                 ret += "/..";
00355                         for(int j=i ; j<files.length; j++)
00356                                 ret += "/" + files[j];
00357                         return ret;
00358                 }
00359                 return f.getAbsolutePath();
00360         }
00361 
00362         public static void setString(String[] path, String atr, String str) {
00363                 setString(getElement(path), atr, str);
00364         }
00365 
00366         public static void setString(Element e, String atr, String str) {
00367                 if (e != null)
00368                         e.setAttribute(atr, str);
00369         }
00370 
00371         public static Dimension getSize(String[] path, Dimension defaultValue) {
00372                 return getSize(getElement(path), defaultValue);
00373         }
00374 
00375         public static Dimension getSize(Element e, Dimension defaultValue) {
00376                 if (e != null) {
00377                         Dimension ret = new Dimension();
00378                         ret.width = getInteger(e, "width", defaultValue.width);
00379                         ret.height = getInteger(e, "height", defaultValue.height);
00380                         return ret;
00381                 }
00382                 return defaultValue;
00383         }
00384 
00385         public static void setSize(Element e, Dimension d) {
00386                 if (e != null) {
00387                         setInteger(e, "width", d.width);
00388                         setInteger(e, "height", d.height);
00389                 }
00390         }
00391 
00392         public static Element appendNewElement(Node n, String tagName, int depth) {
00393                 return appendNewElement(doc_, n, tagName, depth);
00394         }
00395         
00396         public static Element appendNewElement(Document doc, Node n, String tagName, int depth) {
00397                 String t = null;
00398                 if (n == null)
00399                         n = root_;
00400                 if (!n.hasChildNodes() || n == root_) {
00401                         t = "\n";
00402                         for (int i = 0; i < depth; i++)
00403                                 t += "    ";
00404                 } else
00405                         t = "    ";
00406                 n.appendChild(doc.createTextNode(t));
00407 
00408                 Element e = doc.createElement(tagName);
00409                 n.appendChild(e);
00410 
00411                 t = "\n";
00412                 for (int i = 0; i < depth - 1; i++)
00413                         t += "    ";
00414                 n.appendChild(doc.createTextNode(t));
00415 
00416                 return e;
00417         }
00418         
00419         public static Element createElement(Document doc, String tagName) {
00420                 return createElement(doc_, tagName);
00421         }
00422 
00423         public static Element createElement(String tagName) {
00424                 return doc_.createElement(tagName);
00425         }
00426 
00427         public static void store() {
00428                 store(fname_);
00429         }
00430         
00431         public static void store(String fname) {
00432                 store(doc_, fname);
00433         }
00434 
00435         public static void store(Document doc, String fname) {
00436                 //File f = new File(fname_);
00437                 //f.renameTo(new File(fname_+"~"));
00438                 TransformerFactory tff = TransformerFactory.newInstance();
00439                 try {
00440                         Transformer tf = tff.newTransformer();
00441                         javax.xml.transform.dom.DOMSource src = new DOMSource();
00442                         src.setNode(doc);
00443                         javax.xml.transform.stream.StreamResult target = new javax.xml.transform.stream.StreamResult();
00444                         target.setOutputStream(new FileOutputStream(new File(fname)));
00445                         tf.transform(src, target);
00446                 } catch (TransformerConfigurationException e) {
00447                         e.printStackTrace();
00448                 } catch (FileNotFoundException e) {
00449                         e.printStackTrace();
00450                 } catch (TransformerException e) {
00451                         e.printStackTrace();
00452                 }
00453         }
00454 }


openhrp3
Author(s): AIST, General Robotix Inc., Nakamura Lab of Dept. of Mechano Informatics at University of Tokyo
autogenerated on Sun Apr 2 2017 03:43:54