View Javadoc

1   /**
2    * Copyright (C) 2005-2009 Alfresco Software Limited.
3    *
4    * This file is part of the Spring Surf Extension project.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *  http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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   * @author Yong Qu
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  	 * @param fileManager
73  	 * @param filePath
74  	 * @param type
75  	 * @return
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  			//throw new IllegalStateException(ex);
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 	 * @param fileManager
105 	 * @param zipFilePath
106 	 * @param descPath
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 					//logger.warning(entry.getName());
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 	 * @param inputStream
133 	 * @param str
134 	 * @return
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 	 * @param path
149 	 * @return
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 	 * @param path
165 	 * @return
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 }