View Javadoc

1   /*******************************************************************************
2    * Copyright (c) 2009 Ordina and committers to Mod4j
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the Eclipse Public License v1.0
5    * which accompanies this distribution, and is available at
6    * http://www.eclipse.org/legal/epl-v10.html
7    *
8    * Contributors:
9    *     Ordina - initial implementation
10   *******************************************************************************/
11  package org.mod4j.dslcommon.generator.helpers;
12  
13  import java.io.File;
14  import java.io.FileInputStream;
15  import java.io.FileNotFoundException;
16  import java.io.IOException;
17  import java.text.DateFormat;
18  import java.util.Date;
19  import java.util.HashMap;
20  import java.util.Map;
21  import java.util.Properties;
22  import java.util.Map.Entry;
23  
24  import org.mod4j.common.generator.admin.FileTrack;
25  import org.mod4j.common.generator.admin.Mod4jTracker;
26  import org.mod4j.common.generator.admin.GeneratedFile;
27  
28  public class ModelHelpers {
29  
30      private static Properties appProps = new Properties();
31  
32      public static boolean equalsIgnoreCase(String one, String two) {
33          return one.equalsIgnoreCase(two);
34      }
35  
36      /**
37       * Read the properties file 'propFilePath' and return the result as a Map<String, String>
38       * 
39       * @param propFilePath
40       *            absolute pathname of the properties files
41       * @return
42       */
43      public static Map<String, String> getProperties(String propFilePath) {
44          Map<String, String> result = new HashMap<String, String>();
45          try {
46              Properties props = new Properties();
47              props.load(new FileInputStream(propFilePath));
48  
49              for (Entry<Object, Object> property : props.entrySet()) {
50                  result.put((String) property.getKey(), (String) property.getValue());
51              }
52          } catch (FileNotFoundException e) {
53              // TODO Auto-generated catch block
54              e.printStackTrace();
55          } catch (IOException e) {
56              // TODO Auto-generated catch block
57              e.printStackTrace();
58          }
59          return result;
60      }
61  
62      public static String getProperty(String key, String propFilePath) {
63  
64          try {
65              appProps.load(new FileInputStream(propFilePath));
66          } catch (FileNotFoundException e) {
67              // TODO Auto-generated catch block
68              e.printStackTrace();
69          } catch (IOException e) {
70              // TODO Auto-generated catch block
71              e.printStackTrace();
72          }
73          return appProps.getProperty(key);
74      }
75  
76      public static String timestamp() {
77          Date now = new Date(System.currentTimeMillis());
78          return now.toString();
79      }
80  
81      public static boolean fileExist(String path) {
82          File f = new File(path);
83          return f.exists();
84      }
85  
86      /**
87       * Determines if the file, given by the <code>absoluteFilePath</code>, should be generated again. If the file is
88       * manually changed by the user, the file may not be regenerated so the user changes will not get lost. The
89       * Mod4jTracker will be consulted, to check if the file has been modified (by the user).
90       * 
91       * @param absoluteFilePath
92       *            The complete path from the root of the file-system including the file name.
93       * @return true if the file can be safely regenerated. Otherwise returns false.
94       */
95      public static boolean shouldRegenerate(String absoluteFilePath) {
96  
97          if (!fileExist(absoluteFilePath)) {
98              // System.err.println("["+ path + "] REGENERATE: ! exists " + path + " ==> true");
99              return true;
100         }
101 
102         // Do not regenerate existing pom.xml files
103         if (absoluteFilePath.endsWith("pom.xml")) {
104             return false;
105         }
106 
107         long modified = new File(absoluteFilePath).lastModified();
108         FileTrack current = Mod4jTracker.getFileTracker().getCurrentTrack();
109         if (current == null) {
110             // System.err.println("["+ path + "] REGENERATE: current == null " + path + " ==> false");
111             return false;
112         }
113         GeneratedFile extension = current.getExtensionFile(absoluteFilePath);
114         if (extension != null) {
115             // System.err.println("REGENERATE: extension != null " + path);
116             long generated = extension.getModifiedDate();
117             System.err.println("[" + absoluteFilePath + "] modified : [" + DateFormat.getInstance().format(modified)
118                     + "] generated [" + DateFormat.getInstance().format(generated) + "]" + " ==> "
119                     + (modified == generated));
120             return (modified == generated);
121         }
122 
123         // System.err.println("["+ path + "] REGENERATE: no track " + path + " ==> false");
124         return false;
125     }
126 
127     /**
128      * @param cls
129      * @return The name of the Java class for name cls
130      */
131     public static String javaClassName(String cls) {
132         return StringHelpers.firstCharToUpper(cls);
133     }
134 
135     public static void print(String m) {
136         System.err.println(m);
137     }
138 }