KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > antcontrib > platform > OsFamily


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  * Copyright (c) 2001 Ant-Contrib project. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution, if
19  * any, must include the following acknowlegement:
20  * "This product includes software developed by the
21  * Ant-Contrib project (http://sourceforge.net/projects/ant-contrib)."
22  * Alternately, this acknowlegement may appear in the software itself,
23  * if and wherever such third-party acknowlegements normally appear.
24  *
25  * 4. The name Ant-Contrib must not be used to endorse or promote products
26  * derived from this software without prior written permission. For
27  * written permission, please contact
28  * ant-contrib-developers@lists.sourceforge.net.
29  *
30  * 5. Products derived from this software may not be called "Ant-Contrib"
31  * nor may "Ant-Contrib" appear in their names without prior written
32  * permission of the Ant-Contrib project.
33  *
34  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
35  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
36  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
37  * DISCLAIMED. IN NO EVENT SHALL THE ANT-CONTRIB PROJECT OR ITS
38  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
39  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
40  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
41  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
42  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
43  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
44  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45  * SUCH DAMAGE.
46  * ====================================================================
47  */

48 package net.sf.antcontrib.platform;
49
50 import org.apache.tools.ant.BuildException;
51 import org.apache.tools.ant.Task;
52
53 /***
54  * Task definition for the <code>OsFamily</code> task.
55  * This task sets the property indicated in the "property"
56  * attribute with the string representing the operating
57  * system family. Possible values include "unix", "dos", "mac"
58  * and "windows".
59  *
60  * <pre>
61  *
62  * Task Declaration:
63  *
64  * <code>
65  * &lt;taskdef name="osfamily" classname="net.sf.antcontrib.platform.OsFamily" /&gt;
66  * </code>
67  *
68  * Usage:
69  * <code>
70  * &lt;osfamily property="propname" /&gt;
71  * </code>
72  *
73  * Attributes:
74  * property --> The name of the property to set with the OS family name
75  *
76  * </pre>
77  * @author <a HREF="mailto:mattinger@mindless.com">Matthew Inger</a>
78  */

79 public class OsFamily extends Task
80 {
81     private String JavaDoc property;
82
83     public OsFamily()
84     {
85     }
86
87     public void setProperty(String JavaDoc property)
88     {
89         this.property = property;
90     }
91
92     public void execute()
93         throws BuildException
94     {
95         if (property == null)
96             throw new BuildException("The attribute 'property' is required " +
97                                      "for the OsFamily task.");
98
99         String JavaDoc osName = System.getProperty("os.name").toLowerCase();
100         String JavaDoc pathSep = System.getProperty("path.separator");
101         String JavaDoc family = null;
102
103         if (osName.indexOf("windows") != -1)
104         {
105             family = "windows";
106         }
107         else if (pathSep.equals(";"))
108         {
109             family = "dos";
110         }
111         else if (osName.indexOf("mac") != -1)
112         {
113             if (osName.endsWith("x"))
114                 family = "unix";
115             else
116                 family = "mac";
117         }
118         else if (pathSep.equals(":"))
119         {
120             family = "unix";
121         }
122
123         if (family != null)
124             getProject().setProperty(property, family);
125     }
126 }
127
Popular Tags