1
|
|
/*
|
2
|
|
* JBind
|
3
|
|
*
|
4
|
|
* Copyright (c) by Stefan Wachter. All rights reserved.
|
5
|
|
*
|
6
|
|
* Usage, modification, and redistribution is subject to license terms that are
|
7
|
|
* available at 'http://www.jbind.org'. The JBind license is like the
|
8
|
|
* 'Apache Software License V 1.1'.
|
9
|
|
*/
|
10
|
|
package org.jbind.xml.schema.compiler;
|
11
|
|
|
12
|
|
import java.io.File;
|
13
|
|
import java.io.FileReader;
|
14
|
|
import java.io.FileWriter;
|
15
|
|
import java.io.IOException;
|
16
|
|
import java.io.Reader;
|
17
|
|
import java.util.Enumeration;
|
18
|
|
import java.util.StringTokenizer;
|
19
|
|
|
20
|
|
import org.jbind.xml.core.cmp.IBinding;
|
21
|
|
import org.jbind.xml.core.cmp.IComponent;
|
22
|
|
import org.jbind.xml.msg.IConstraintViolations;
|
23
|
|
import org.jbind.xml.msg.XmlMessages;
|
24
|
|
|
25
|
|
public class FileInfo implements IFileInfo {
|
26
|
|
|
27
|
|
private IComponent myComponent;
|
28
|
|
private ICartridge myCartridge;
|
29
|
|
private String myGeneratedCode;
|
30
|
|
private String myFileNameExtension;
|
31
|
|
|
32
|
743
|
public FileInfo(IComponent aComponent, ICartridge aCartridge, String aGeneratedCode, String aFileNameExtension) {
|
33
|
743
|
myComponent = aComponent;
|
34
|
743
|
myCartridge = aCartridge;
|
35
|
743
|
myGeneratedCode = aGeneratedCode;
|
36
|
743
|
myFileNameExtension = aFileNameExtension;
|
37
|
|
}
|
38
|
|
|
39
|
8
|
public IComponent getComponent() {
|
40
|
8
|
return myComponent;
|
41
|
|
}
|
42
|
397
|
public String getGeneratedCode() {
|
43
|
397
|
return myGeneratedCode;
|
44
|
|
}
|
45
|
|
|
46
|
413
|
private File getDestinationFileOrDir(File aRootDir, boolean aFileNotDir) {
|
47
|
413
|
File res = aRootDir;
|
48
|
413
|
IBinding b = myComponent.getBindings()[myCartridge.getCartridgeNo()];
|
49
|
413
|
String path = aFileNotDir ? b.getFqName() : b.getPackage();
|
50
|
413
|
if (null != path) {
|
51
|
413
|
for (Enumeration e = new StringTokenizer(path, ".");
|
52
|
1287
|
e.hasMoreElements(); ) {
|
53
|
874
|
String part = (String)e.nextElement();
|
54
|
874
|
if (!e.hasMoreElements() && aFileNotDir) {
|
55
|
405
|
if (null != myFileNameExtension) {
|
56
|
405
|
part += "." + myFileNameExtension;
|
57
|
|
}
|
58
|
|
}
|
59
|
874
|
res = new File(res, part);
|
60
|
|
}
|
61
|
|
}
|
62
|
413
|
return res;
|
63
|
|
}
|
64
|
|
|
65
|
405
|
public File getDestinationFile(File aRootDir) {
|
66
|
405
|
return getDestinationFileOrDir(aRootDir, true);
|
67
|
|
}
|
68
|
8
|
public File getDestinationDir(File aRootDir) {
|
69
|
8
|
return getDestinationFileOrDir(aRootDir, false);
|
70
|
|
}
|
71
|
|
|
72
|
405
|
public final boolean overwrite() {
|
73
|
405
|
return myCartridge.overwrite();
|
74
|
|
}
|
75
|
405
|
public final boolean isUpToDate(Reader aReader) throws Exception {
|
76
|
405
|
return myCartridge.isUpToDate(aReader, this);
|
77
|
|
}
|
78
|
|
|
79
|
0
|
public void output(String aRootDir, IConstraintViolations aViolations) {
|
80
|
0
|
output(new File(aRootDir), aViolations);
|
81
|
|
}
|
82
|
|
|
83
|
8
|
public void output(File aRootDir, IConstraintViolations aViolations) {
|
84
|
8
|
File destinationDir = getDestinationDir(aRootDir);
|
85
|
8
|
File destinationFile = getDestinationFile(aRootDir);
|
86
|
|
|
87
|
8
|
if (!destinationDir.isDirectory()) {
|
88
|
0
|
if (!destinationDir.mkdirs()) {
|
89
|
0
|
aViolations.add(XmlMessages.packageDirError(destinationDir, null));
|
90
|
0
|
return;
|
91
|
|
}
|
92
|
|
}
|
93
|
|
|
94
|
|
// Indicates if the destinationFile must be written
|
95
|
8
|
boolean write = false;
|
96
|
|
|
97
|
8
|
if (destinationFile.exists()) {
|
98
|
8
|
try {
|
99
|
8
|
if (!isUpToDate(new FileReader(destinationFile))) {
|
100
|
|
// The content has changed.
|
101
|
|
// Check if the destinationFile must be overwritten.
|
102
|
0
|
if (!overwrite()) {
|
103
|
0
|
aViolations.add(myCartridge.getEditHint(destinationFile, myComponent));
|
104
|
|
} else {
|
105
|
|
// The previous code is overwritten
|
106
|
0
|
write = true;
|
107
|
|
}
|
108
|
|
}
|
109
|
|
} catch (Exception e) {
|
110
|
0
|
aViolations.add(XmlMessages.readFileException(destinationFile, e, null));
|
111
|
0
|
return;
|
112
|
|
}
|
113
|
|
|
114
|
|
} else {// The destinationFile does not exist -> needs to be written
|
115
|
0
|
write = true;
|
116
|
|
|
117
|
|
}
|
118
|
|
|
119
|
8
|
if (write) {
|
120
|
0
|
try {
|
121
|
0
|
FileWriter fw = new FileWriter(destinationFile);
|
122
|
0
|
fw.write(getGeneratedCode());
|
123
|
0
|
fw.flush();
|
124
|
0
|
fw.close();
|
125
|
|
} catch (IOException e) {
|
126
|
0
|
aViolations.add(XmlMessages.outputFileException(destinationFile, e, null));
|
127
|
|
}
|
128
|
|
}
|
129
|
|
|
130
|
|
}
|
131
|
|
|
132
|
|
}
|
133
|
|
|