KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > importer > rose > parser > Util


1 /**
2  * <copyright>
3  *
4  * Copyright (c) 2002-2004 IBM Corporation and others.
5  * All rights reserved. This program and the accompanying materials
6  * are made available under the terms of the Eclipse Public License v1.0
7  * which accompanies this distribution, and is available at
8  * http://www.eclipse.org/legal/epl-v10.html
9  *
10  * Contributors:
11  * IBM - Initial API and implementation
12  *
13  * </copyright>
14  *
15  * $Id: Util.java,v 1.2 2005/06/08 06:20:36 nickb Exp $
16  */

17 package org.eclipse.emf.importer.rose.parser;
18
19 import java.io.File JavaDoc;
20 import java.util.StringTokenizer JavaDoc;
21
22
23 /**
24  */

25 public class Util
26 {
27   public final static String JavaDoc QUOTE = "\"";
28   public final static int PATH_NOT_VALID = 0;
29   public final static int DIRECTORY_NOT_FOUND = 1;
30   public final static int PATH_OK = 2;
31   public final static int NOT_A_DIRECTORY = 3;
32
33   public static int isValidPath(String JavaDoc pathname)
34   {
35     File JavaDoc testDirectory;
36     try
37     {
38       testDirectory = new File JavaDoc(pathname);
39     }
40     catch (NullPointerException JavaDoc e)
41     {
42       return PATH_NOT_VALID;
43     }
44     boolean exists = false;
45     try
46     {
47       exists = testDirectory.exists();
48     }
49     catch (SecurityException JavaDoc e)
50     {
51       return PATH_NOT_VALID;
52     }
53     boolean isDirectory = false;
54     try
55     {
56       isDirectory = testDirectory.isDirectory();
57     }
58     catch (SecurityException JavaDoc e)
59     {
60       return PATH_NOT_VALID;
61     }
62     if (!isDirectory)
63     {
64       if (exists)
65       {
66         return NOT_A_DIRECTORY;
67       }
68       else
69       {
70         return DIRECTORY_NOT_FOUND;
71       }
72     }
73     return PATH_OK;
74   }
75
76   static public boolean createDirectory(String JavaDoc dirName)
77   {
78     try
79     {
80       File JavaDoc newDir = new File JavaDoc(dirName);
81       if ((newDir.mkdirs()))
82       {
83         return true;
84       }
85       else
86       {
87         return false;
88       }
89     }
90     catch (NullPointerException JavaDoc e)
91     {
92       return false;
93     }
94     catch (SecurityException JavaDoc e)
95     {
96       return false;
97     }
98   }
99
100   static public String JavaDoc trimQuotes(String JavaDoc str)
101   {
102     int f_ind = str.indexOf(QUOTE);
103     int l_ind = str.lastIndexOf(QUOTE);
104     if (f_ind == -1)
105     {
106       return str;
107     }
108     else if (f_ind == l_ind)
109     {
110       return str.substring(f_ind + 1);
111     }
112     else
113     {
114       return str.substring(f_ind + 1, l_ind);
115     }
116   }
117
118   static public String JavaDoc getType(String JavaDoc str)
119   {
120     return getWord(str, 1);
121   }
122
123   static public String JavaDoc getName(String JavaDoc str)
124   {
125     int ind_end = str.lastIndexOf(QUOTE);
126     if (ind_end == -1)
127     {
128       return null;
129     }
130     String JavaDoc temp = str.substring(0, ind_end);
131     int ind_start = temp.lastIndexOf(QUOTE);
132     if (ind_start == -1)
133     {
134       return null;
135     }
136     String JavaDoc name = str.substring(ind_start + 1, ind_end);
137     if (name.indexOf("$UNNAMED") != -1)
138     {
139       name = "";
140     }
141     return name;
142   }
143
144   static public String JavaDoc getWord(String JavaDoc str, int n)
145   {
146     StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(str);
147     int i = 0;
148     while (st.hasMoreTokens())
149     {
150       i++;
151       String JavaDoc tok = st.nextToken();
152       if (i == n)
153       {
154         return tok;
155       }
156     }
157     return null;
158   }
159
160   static public String JavaDoc updateFileName(String JavaDoc fileName, String JavaDoc dilimiter)
161   {
162     String JavaDoc name = "";
163     while (true)
164     {
165       int ind = fileName.indexOf(dilimiter);
166       if (ind == -1)
167       {
168         name += fileName;
169         break;
170       }
171       name += (fileName.substring(0, ind) + File.separator);
172       fileName = fileName.substring(ind + dilimiter.length());
173     }
174     return name;
175   }
176 }
177
Popular Tags