1
2
3
4
5
6
7
8
9
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
38
39
40
41
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
54 e.printStackTrace();
55 } catch (IOException e) {
56
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
68 e.printStackTrace();
69 } catch (IOException e) {
70
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
88
89
90
91
92
93
94
95 public static boolean shouldRegenerate(String absoluteFilePath) {
96
97 if (!fileExist(absoluteFilePath)) {
98
99 return true;
100 }
101
102
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
111 return false;
112 }
113 GeneratedFile extension = current.getExtensionFile(absoluteFilePath);
114 if (extension != null) {
115
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
124 return false;
125 }
126
127
128
129
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 }