1 /* |
|
2 * Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 * Property file management engine |
|
17 * |
|
18 */ |
|
19 package com.nokia.tracecompiler.engine.propertyfile; |
|
20 |
|
21 import java.io.File; |
|
22 |
|
23 import javax.xml.parsers.DocumentBuilder; |
|
24 import javax.xml.parsers.DocumentBuilderFactory; |
|
25 import javax.xml.parsers.ParserConfigurationException; |
|
26 |
|
27 import com.nokia.tracecompiler.engine.TraceCompilerEngineBase; |
|
28 import com.nokia.tracecompiler.engine.TraceCompilerEngineGlobals; |
|
29 import com.nokia.tracecompiler.engine.project.ProjectEngine; |
|
30 import com.nokia.tracecompiler.file.FileUtils; |
|
31 import com.nokia.tracecompiler.model.TraceCompilerException; |
|
32 import com.nokia.tracecompiler.model.TraceModel; |
|
33 import com.nokia.tracecompiler.project.ProjectUtils; |
|
34 import com.nokia.tracecompiler.source.SourceConstants; |
|
35 |
|
36 /** |
|
37 * Property file management engine |
|
38 * |
|
39 */ |
|
40 public final class PropertyFileEngine extends TraceCompilerEngineBase { |
|
41 |
|
42 /** |
|
43 * Trace model |
|
44 */ |
|
45 private TraceModel model; |
|
46 |
|
47 /** |
|
48 * Constructor |
|
49 * |
|
50 * @param model |
|
51 * the trace model |
|
52 */ |
|
53 public PropertyFileEngine(TraceModel model) { |
|
54 this.model = model; |
|
55 } |
|
56 |
|
57 /* |
|
58 * (non-Javadoc) |
|
59 * |
|
60 * @see com.nokia.tracecompiler.engine.TraceCompilerEngine#projectOpened() |
|
61 */ |
|
62 @Override |
|
63 public void projectOpened() throws TraceCompilerException { |
|
64 TracePropertyFile propertyFile = model |
|
65 .getExtension(TracePropertyFile.class); |
|
66 if (propertyFile == null) { |
|
67 String fileName = null; |
|
68 try { |
|
69 fileName = ProjectUtils.getLocationForFile(model, |
|
70 ProjectEngine.traceFolderName, |
|
71 PropertyFileConstants.PROPERTY_FILE_NAME, false); |
|
72 } catch (TraceCompilerException e) { |
|
73 // Model should always be open when traceProjectOpened is |
|
74 // called |
|
75 } |
|
76 if (fileName != null) { |
|
77 propertyFile = parsePropertyFile(fileName); |
|
78 } |
|
79 } |
|
80 if (propertyFile == null) { |
|
81 String msg = Messages |
|
82 .getString("PropertyFileEngine.FailedToAttachFile"); //$NON-NLS-1$ |
|
83 TraceCompilerEngineGlobals.getEvents().postErrorMessage(msg, null, true); |
|
84 } |
|
85 } |
|
86 |
|
87 /* |
|
88 * (non-Javadoc) |
|
89 * |
|
90 * @see com.nokia.tracecompiler.engine.TraceCompilerEngine#projectClosing() |
|
91 */ |
|
92 @Override |
|
93 public void projectClosed() { |
|
94 model.removeExtensions(TracePropertyFile.class); |
|
95 } |
|
96 |
|
97 /* |
|
98 * (non-Javadoc) |
|
99 * |
|
100 * @see com.nokia.tracecompiler.engine.TraceCompilerEngine#exportProject() |
|
101 */ |
|
102 @Override |
|
103 public void exportProject() { |
|
104 } |
|
105 |
|
106 /** |
|
107 * Parses the property file |
|
108 * |
|
109 * @param fileName |
|
110 * the file path |
|
111 * @return the property file |
|
112 */ |
|
113 private TracePropertyFile parsePropertyFile(String fileName) { |
|
114 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
|
115 factory.setValidating(false); |
|
116 DocumentBuilder builder; |
|
117 TracePropertyFile propertyFile = null; |
|
118 try { |
|
119 builder = factory.newDocumentBuilder(); |
|
120 File file = new File(fileName); |
|
121 if (file.exists()) { |
|
122 try { |
|
123 PropertyFileParser parser = new PropertyFileParser(model, |
|
124 fileName, builder); |
|
125 parser.parse(); |
|
126 propertyFile = new TracePropertyFile(file.getParent(), |
|
127 parser.getDocument()); |
|
128 } catch (TraceCompilerException e) { |
|
129 // Problem parsing document -> Backup and create new |
|
130 TraceCompilerEngineGlobals.getEvents().postError(e); |
|
131 createBackup(fileName); |
|
132 } |
|
133 } |
|
134 if (propertyFile == null) { |
|
135 propertyFile = new TracePropertyFile(file.getParent(), builder |
|
136 .newDocument()); |
|
137 } |
|
138 model.addExtension(propertyFile); |
|
139 } catch (ParserConfigurationException e) { |
|
140 } |
|
141 return propertyFile; |
|
142 } |
|
143 |
|
144 /** |
|
145 * Creates a backup of the property file |
|
146 * |
|
147 * @param path |
|
148 * the file to be backed up |
|
149 */ |
|
150 private void createBackup(String path) { |
|
151 boolean backup = false; |
|
152 File file = new File(path); |
|
153 if (file.exists()) { |
|
154 boolean allexist = true; |
|
155 // Checks the existing backup files and renames the old file to |
|
156 // largest available number starting from 0 |
|
157 File f = null; |
|
158 for (int i = 0; i < PropertyFileConstants.BACKUP_COUNT && allexist; i++) { |
|
159 f = new File(path + i + PropertyFileConstants.BACKUP_EXTENSION); |
|
160 if (!f.exists()) { |
|
161 backup = FileUtils.copyFile(file, f); |
|
162 allexist = false; |
|
163 } |
|
164 } |
|
165 if (allexist) { |
|
166 // If all backups from 0 to 9 exist, the old ones are moved |
|
167 // back 1 step. The new file is renamed to *9.h |
|
168 for (int i = 0; i < PropertyFileConstants.BACKUP_COUNT; i++) { |
|
169 f = new File(path + i |
|
170 + PropertyFileConstants.BACKUP_EXTENSION); |
|
171 if (i == 0) { |
|
172 f.delete(); |
|
173 } else { |
|
174 f.renameTo(new File(path + (i - 1) |
|
175 + PropertyFileConstants.BACKUP_EXTENSION)); |
|
176 } |
|
177 } |
|
178 f = new File(path + PropertyFileConstants.LAST_BACKUP); |
|
179 backup = FileUtils.copyFile(file, f); |
|
180 } |
|
181 if (f != null && backup) { |
|
182 String msg = Messages |
|
183 .getString("PropertyFileEngine.PropertyFileBackUpPrefix") //$NON-NLS-1$ |
|
184 + FileUtils.convertSeparators( |
|
185 SourceConstants.FORWARD_SLASH_CHAR, f |
|
186 .getAbsolutePath(), false); |
|
187 TraceCompilerEngineGlobals.getEvents().postInfoMessage(msg, null); |
|
188 } |
|
189 } |
|
190 } |
|
191 |
|
192 } |
|