1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.springframework.roo.addon.surf.util;
20
21 import java.io.InputStream;
22 import java.util.Enumeration;
23 import java.util.Scanner;
24 import java.util.logging.Logger;
25 import java.util.zip.ZipEntry;
26 import java.util.zip.ZipFile;
27
28 import org.springframework.roo.file.monitor.event.FileDetails;
29 import org.springframework.roo.process.manager.FileManager;
30 import org.springframework.roo.support.util.Assert;
31 import org.springframework.roo.support.util.FileCopyUtils;
32 import org.springframework.roo.support.util.XmlUtils;
33 import org.w3c.dom.Document;
34 import org.w3c.dom.Element;
35
36
37
38
39 public class SurfUtils {
40
41 static Logger logger = Logger.getLogger(SurfUtils.class.getName());
42
43 public static String getElementValue (FileManager fileManager,FileDetails xmlFile, String xPath){
44 InputStream docInputStream = fileManager.getInputStream(xmlFile.getCanonicalPath());
45 Document doc;
46 try {
47 doc = XmlUtils.getDocumentBuilder().parse(docInputStream);
48 } catch (Exception ex) {
49 throw new IllegalStateException(ex);
50 }
51 if ( XmlUtils.findFirstElement(xPath, doc.getDocumentElement()) != null ) {
52 return XmlUtils.findFirstElement(xPath, doc.getDocumentElement()).getTextContent().trim();
53 } else {
54 return null;
55 }
56
57 }
58
59
60 public static Document getSurfXMLDoc(FileManager fileManager,FileDetails xmlFile){
61 InputStream docInputStream = fileManager.getInputStream(xmlFile.getCanonicalPath());
62 Document doc;
63 try {
64 doc = XmlUtils.getDocumentBuilder().parse(docInputStream);
65 } catch (Exception ex) {
66 throw new IllegalStateException(ex);
67 }
68 return doc;
69 }
70
71
72
73
74
75
76
77 public static boolean checkSurfXMLType (FileManager fileManager, String filePath, String type) {
78 Assert.notNull(fileManager, "File manager required");
79 Assert.hasText(filePath, "File path required");
80 Assert.hasText(type, "Surf type name required");
81
82 InputStream pageInputStream;
83 Document pom;
84 try {
85 pageInputStream = fileManager.getInputStream(filePath);
86 pom = XmlUtils.getDocumentBuilder().parse(pageInputStream);
87 } catch (Exception ex) {
88
89 return false;
90 }
91
92 Element rootElement = pom.getDocumentElement();
93
94 try {
95 pageInputStream.close();
96 } catch (Exception ex) {
97 throw new IllegalStateException(ex);
98 }
99
100 return rootElement.getTagName().equals(type);
101 }
102
103
104
105
106
107
108 public static void unzip (FileManager fileManager, String zipFilePath, String descPath) {
109 try {
110 ZipFile zipfile = new ZipFile(zipFilePath);
111 Enumeration<? extends ZipEntry> e = zipfile.entries();
112 while(e.hasMoreElements()) {
113 ZipEntry entry = (ZipEntry) e.nextElement();
114 if ( !entry.isDirectory()){
115
116 String path = descPath+"/"+entry.getName();
117 if (fileManager.exists(path)){
118 FileCopyUtils.copy(zipfile.getInputStream(entry),fileManager.updateFile(path).getOutputStream());
119 } else {
120 FileCopyUtils.copy(zipfile.getInputStream(entry),fileManager.createFile(path).getOutputStream());
121 }
122 }
123 }
124 zipfile.close();
125 } catch (Exception ex) {
126 throw new IllegalStateException(ex);
127 }
128 }
129
130
131
132
133
134
135
136 public static boolean fileContainsString(InputStream inputStream, String str) {
137 Scanner templateScanner = new Scanner(inputStream);
138 String matchStr= templateScanner.findWithinHorizon(str, 0);
139 templateScanner.close();
140 if ( matchStr != null ) {
141 return true;
142 } else {
143 return false;
144 }
145 }
146
147
148
149
150
151 public static String getNameFromPath(String path) {
152
153 if (path==null || path.equals("")) return "";
154 String name = path;
155 if (path.lastIndexOf("/") != -1) {
156 name = path.substring(path.lastIndexOf("/")+1);
157 } else if (path.lastIndexOf("\\") != -1) {
158 name = path.substring(path.lastIndexOf("\\")+1);
159 }
160 return name;
161 }
162
163
164
165
166
167 public static String getRelativePath(String path) {
168
169 if (path==null || path.equals("")) return "";
170 if (path.lastIndexOf("/") != -1) {
171 return path.substring(0,path.lastIndexOf("/"));
172 } else if (path.lastIndexOf("\\") != -1) {
173 return path.substring(0,path.lastIndexOf("\\"));
174 }
175 return "";
176 }
177 }