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.common.generator.admin;
12  
13  import java.io.File;
14  import java.util.ArrayList;
15  import java.util.Collection;
16  import java.util.Collections;
17  import java.util.HashMap;
18  import java.util.List;
19  import java.util.Map;
20  
21  import org.jdom.Document;
22  import org.jdom.Element;
23  import org.mod4j.dslcommon.xml.XmlUtil;
24  
25  public class ProjectTrack {
26  
27  //    private List<FileTrack> trackedFiles = new ArrayList<FileTrack>();
28  
29      private String projectPath = null;
30      private FileTrack currentTrack = null;
31      private Map<String, FileTrack> tracks = new HashMap<String, FileTrack>();
32  
33  
34      /**
35  	 * @return the currentFileTrack
36  	 */
37  	public FileTrack getCurrentFileTrack() {
38  		return currentTrack;
39  	}
40  
41  	/**
42  	 * @param currentFileTrack the currentFileTrack to set
43  	 */
44  	public void setCurrentFileTrack(FileTrack currentFileTrack) {
45  		this.currentTrack = currentFileTrack;
46  	}
47  
48  	public String getProjectPath() {
49          return projectPath;
50      }
51  
52  	public void setProjectPath(String path) {
53          projectPath = path;
54      }
55  
56      public ProjectTrack() {
57          tracks = new HashMap<String, FileTrack>();
58      }
59      
60      public ProjectTrack(String projectPath) {
61          tracks = new HashMap<String, FileTrack>();
62          this.projectPath = projectPath;
63      }
64  
65      private String applicationPath;
66  
67      public String getApplicationPath() {
68          return applicationPath;
69      }
70  
71      public void setApplicationPath(String applicationPath) {
72          this.applicationPath = applicationPath;
73      }
74  
75      public FileTrack getTrack(String resource) {
76  //    	System.err.println("GetTrack() ["+ resource + "]" ); 
77          String localResource = resource;
78          if (resource.startsWith("file:/")) {
79              localResource = resource.substring(6);
80          }
81          if (localResource.startsWith(projectPath)) {
82              localResource = localResource.substring(projectPath.length());
83          }
84          FileTrack result = tracks.get(localResource);
85          if (result == null) {
86  //        	System.err.println("      null" );
87              result = new FileTrack(localResource);
88              result.setProject(this);
89              tracks.put(localResource, result);
90          } else {
91  //        	System.err.println("      found" );
92  //            result.clear();
93          }
94          return result;
95      }
96  
97      public Collection<FileTrack> getTracks() {
98          return Collections.unmodifiableCollection(tracks.values());
99      }
100 
101     /* The XML tag names for readong and writing as XML file
102      * 
103      */
104 	private static final String SOURCE_PATH = "sourcePath";
105 
106 	private static final String MODIFIED_DATE = "modifiedDate";
107 
108 	private static final String EXTENSION_POINT = "extensionPoint";
109 
110 	private static final String RESOURCE = "resource";
111 
112 	private static final String APPLICATION_PATH = "ApplicationPath";
113 
114 	private static final String PROJECT_PATH = "ProjectPath";
115 
116 	private static final String GENERATED_FILE = "GeneratedFile";
117 
118 	private static final String FILE_TRACK = "FileTrack";
119 
120 	private static final String PROJECT_TRACK = "ProjectTrack";
121 
122 	private static final String FILE_TRACKER = "FileTracker";
123 
124 
125 	public void writeProjectTrack(String filename) {
126 		System.err.println("writeProjectTracker [" + filename + "]");
127 		Document doc = new Document();
128 		Element root = new Element(PROJECT_TRACK);
129 		root.setAttribute(PROJECT_PATH, getProjectPath());
130 		root.setAttribute(APPLICATION_PATH, getApplicationPath());
131 		doc.setRootElement(root);
132 
133 		for (FileTrack fileTrack : getTracks()) {
134 			Element model = new Element(FILE_TRACK);
135 			model.setAttribute(RESOURCE, fileTrack.getResource());
136 			root.addContent(model);
137 			for (GeneratedFile file : fileTrack.getGeneratedFiles()) {
138 				Element gen = new Element(GENERATED_FILE);
139 				gen.setAttribute(EXTENSION_POINT, Boolean.toString(file
140 						.isExtensionPoint()));
141 				gen.setAttribute(MODIFIED_DATE, Long.toString(file
142 						.getModifiedDate()));
143 				gen.setAttribute(SOURCE_PATH, file.getSourcePath());
144 				// gen.setAttribute("moduleName", file.getModuleName());
145 				model.addContent(gen);
146 			}
147 			for (GeneratedFile file : fileTrack.getExtensionFiles()) {
148 				Element gen = new Element(GENERATED_FILE);
149 				gen.setAttribute(EXTENSION_POINT, "true");
150 				gen.setAttribute(MODIFIED_DATE, Long.toString(file
151 						.getModifiedDate()));
152 				gen.setAttribute(SOURCE_PATH, file.getSourcePath());
153 				// gen.setAttribute("moduleName", file.getModuleName());
154 				model.addContent(gen);
155 			}
156 		}
157 		XmlUtil.writeDocument(doc, filename, true, "    ");
158 	}
159 
160 	public void readProjectTracker(String filename) {
161 		File file = new File(filename);
162 		if( ! file.exists() ){
163 			System.err.println("readProjectTracker [" + filename + "] does not exist");
164 			return;
165 		}
166 		System.err.println("readProjectTracker [" + filename + "]");
167 		Document doc = XmlUtil.readXmlDocument(file, true);
168 
169 		// unpack
170 		Element root = doc.getRootElement();
171 		assert root.getName().equals(PROJECT_TRACK) : "ProjectTrack XML root element incorrect";
172 
173 //		for (Object projectTrackNode : root.getChildren()) {
174 //			Element node = (Element) projectTrackNode;
175 //		currentProject = findProject(node.getAttributeValue(PROJECT_PATH));
176 		setApplicationPath(root.getAttributeValue(APPLICATION_PATH));
177 		setProjectPath(root.getAttributeValue(PROJECT_PATH));
178 
179 		for (Object fileTrackNode : root.getChildren()) {
180 			Element fileNode = (Element) fileTrackNode;
181 			assert fileNode.getName().equals(FILE_TRACK) : "FileTrack XML Filetrack element incorrect";
182 
183 			setCurrentFileTrack( getTrack(fileNode.getAttributeValue(RESOURCE)) );
184 			for (Object genFile : fileNode.getChildren()) {
185 				Element genFileNode = (Element) genFile;
186 				assert genFileNode.getName().equals(GENERATED_FILE) : "FileTrack XML GeneratedFile element incorrect";
187 				GeneratedFile generatedFile = new GeneratedFile(genFileNode
188 						.getAttributeValue(SOURCE_PATH), FileType.TEXT,
189 						currentTrack, Boolean.parseBoolean(genFileNode
190 								.getAttributeValue(EXTENSION_POINT)));
191 				generatedFile.setModifiedDate(Long.parseLong(genFileNode
192 						.getAttributeValue(MODIFIED_DATE)));
193 				if (generatedFile.isExtensionPoint()) {
194 					currentTrack.getExtensionFiles().add(generatedFile);
195 				} else {
196 					currentTrack.getGeneratedFiles().add(generatedFile);
197 				}
198 			}
199 		}
200 	}
201 
202 }