KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > ant > util > Util


1 /**
2  * <copyright>
3  *
4  * Copyright (c) 2004-2005 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.4 2005/06/08 06:17:17 nickb Exp $
16  */

17 package org.eclipse.emf.ant.util;
18
19 import java.io.BufferedWriter JavaDoc;
20 import java.io.File JavaDoc;
21 import java.io.FileWriter JavaDoc;
22 import java.io.IOException JavaDoc;
23
24
25 /**
26  * Utility classes with generic methods that may be used by more than one task.
27  * @since 2.1.0
28  */

29 public class Util
30 {
31   /**
32    * Removes the version number of all the subdirectories of a given directory.
33    * The expected format of the directories name is <dirName>_<version> where version
34    * has at least 2 groups (2.1 for example).
35    *
36    * @param parentDir the parent of the directories that will have the version removed
37    * @return the number of changed directories
38    */

39   public static int removeVersion(File JavaDoc parentDir)
40   {
41     if (parentDir == null || !parentDir.isDirectory())
42     {
43       return 0;
44     }
45
46     int counter = 0;
47     File JavaDoc[] dirs = parentDir.listFiles();
48     for (int i = 0; i < dirs.length; i++)
49     {
50       if (dirs[i].isDirectory())
51       {
52         String JavaDoc name = dirs[i].getName().replaceAll("_(\\d+\\.)+\\d$", "");
53         if (!name.equals(dirs[i].getName()) && dirs[i].renameTo(new File JavaDoc(parentDir, name)))
54         {
55           counter++;
56         }
57       }
58     }
59     return counter;
60   }
61
62   /**
63    * Writes the given content to the specifed file.
64    * @param file The file to be written or overwritten.
65    * @param content
66    * @throws IOException
67    */

68   public static void writeFile(File JavaDoc file, String JavaDoc content) throws IOException JavaDoc
69   {
70     if (!file.getParentFile().isDirectory())
71     {
72       file.getParentFile().mkdirs();
73     }
74     
75     BufferedWriter JavaDoc out = null;
76     try
77     {
78       out = new BufferedWriter JavaDoc(new FileWriter JavaDoc(file));
79       out.write(content);
80     }
81     finally
82     {
83       if (out != null)
84       {
85         out.close();
86       }
87     }
88   }
89 }
Popular Tags