KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > ecm > taskdefs > OSHelper


1 // ====================================================================
2
//
3
// ECM: The Extensible Container Model
4
// Copyright (C) 2004 THALES
5
// Contact: openccm-ecm@objectweb.org
6
//
7
// This library is free software; you can redistribute it and/or
8
// modify it under the terms of the GNU Lesser General Public
9
// License as published by the Free Software Foundation; either
10
// version 2.1 of the License, or any later version.
11
//
12
// This library is distributed in the hope that it will be useful,
13
// but WITHOUT ANY WARRANTY; without even the implied warranty of
14
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
// Lesser General Public License for more details.
16
//
17
// You should have received a copy of the GNU Lesser General Public
18
// License along with this library; if not, write to the Free Software
19
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20
// USA
21
//
22
// Initial developer(s): Mathieu Vadet.
23
// Initial Funding: IST COACH European project (IST-2001-34445)
24
// http://www.ist-coach.org
25
//
26
// ====================================================================
27

28
29
30 package org.objectweb.ecm.taskdefs;
31
32 /**
33  ** <p>Set of static operations to ease OS specific manipulations for the Ant tasks.</p>
34  **/

35 public class OSHelper
36 {
37     //
38
static private String JavaDoc _ospath;
39     static private boolean _isunix;
40     static private boolean _iswindows;
41     static private String JavaDoc _osfamily;
42
43     //
44
static {
45         _ospath = null;
46         _isunix = org.apache.tools.ant.taskdefs.condition.Os.isFamily("unix");
47         _iswindows = org.apache.tools.ant.taskdefs.condition.Os.isFamily("windows");
48
49         if (_isunix) {
50             _osfamily = "unix";
51         }
52         else if (_iswindows) {
53             _osfamily = "windows";
54         }
55     }
56
57     //
58
//
59
//
60

61     static public boolean
62     isUnix()
63     {
64         return _isunix;
65     }
66
67     static public boolean
68     isWindows()
69     {
70         return _iswindows;
71     }
72
73     static public String JavaDoc
74     osFamily()
75     {
76         return _osfamily;
77     }
78
79     static public String JavaDoc
80     osPath()
81     {
82         if (_ospath==null) {
83             java.util.Vector JavaDoc procenv = org.apache.tools.ant.taskdefs.Execute.getProcEnvironment();
84             String JavaDoc vname = null;
85             if (isUnix()) {
86                 vname = "PATH";
87             }
88             else if (isWindows()) {
89                 vname = "Path";
90             }
91
92             String JavaDoc[] vars = (String JavaDoc[])procenv.toArray(new String JavaDoc[0]);
93             for (int i=0;i<vars.length;i++) {
94                 if (vars[i].startsWith(vname)) {
95                     int idx = vars[i].indexOf('=');
96                     _ospath = vars[i].substring(idx+1);
97                     break;
98                 }
99             }
100
101         }
102
103         // if still null, then error (how to report this?)
104

105         return _ospath;
106     }
107
108     static public String JavaDoc
109     envVar(String JavaDoc vname)
110     {
111         java.util.Vector JavaDoc procenv = org.apache.tools.ant.taskdefs.Execute.getProcEnvironment();
112         String JavaDoc[] vars = (String JavaDoc[])procenv.toArray(new String JavaDoc[0]);
113         for (int i=0;i<vars.length;i++) {
114             if (vars[i].startsWith(vname)) {
115                 int idx = vars[i].indexOf('=');
116                 return vars[i].substring(idx+1);
117             }
118         }
119
120         return null;
121     }
122
123     static public void
124     pathConvert(org.apache.tools.ant.Project project,
125                 String JavaDoc prop, String JavaDoc spath)
126     {
127         org.apache.tools.ant.taskdefs.PathConvert pconvert = null;
128         org.apache.tools.ant.types.Path path = null;
129         pconvert = (org.apache.tools.ant.taskdefs.PathConvert)project.createTask("pathconvert");
130         pconvert.setTargetos(osFamily());
131         pconvert.setProperty(prop);
132         path = pconvert.createPath();
133
134         // try to use spath as a property name
135
String JavaDoc rpath = project.getProperty(spath);
136         if (rpath!=null) {
137             path.setPath(rpath);
138         }
139         // use spath as a path
140
else {
141             path.setPath(spath);
142         }
143
144         pconvert.execute();
145     }
146 }
147
Popular Tags