forked from leonardoanalista/java2word
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.java
More file actions
220 lines (200 loc) · 6.49 KB
/
Copy pathUtils.java
File metadata and controls
220 lines (200 loc) · 6.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package word.utils;
import java.io.BufferedReader;
//import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import word.api.interfaces.IDocument;
//import javax.xml.transform.Source;
//import javax.xml.transform.Transformer;
//import javax.xml.transform.TransformerFactory;
//import javax.xml.transform.stream.StreamResult;
//import javax.xml.transform.stream.StreamSource;
//import org.dom4j.DocumentException;
//import org.dom4j.DocumentHelper;
//import org.dom4j.Node;
public class Utils {
/**
* @return
*
* The root of the web app as String.
*/
public static String getAppRoot() {
File file = new File(".");
try {
return file.getCanonicalPath();
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("Can't get app root directory", e);
}
}
/**
*
* @param file
* : It is the full path to the file
*
* @return String with the content of the file
*/
public static String readFile(String file) {
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
} catch (FileNotFoundException e) {
e.printStackTrace();
throw new RuntimeException("Can't find the file", e);
}
String line = null;
StringBuilder stringBuilder = new StringBuilder();
String ls = System.getProperty("line.separator");
try {
while ((line = reader.readLine()) != null) {
stringBuilder.append(line);
stringBuilder.append(ls);
}
return stringBuilder.toString();
} catch (IOException e) {
e.printStackTrace();
return null;
} finally {
reader = null;
}
}
public static String replaceSpecialCharacters(IDocument myDoc) {
// String cont = myDoc.getContent();
// cont = cont.replace("ì", "ì");
// cont = cont.replace("í", "í");
// cont = cont.replace("î", "î");
// cont = cont.replace("ï", "ï");
System.out.println("#### FFF 02 ####");
System.out.println(myDoc.getContent());
return myDoc.getContent().replace("í", "í");
}
/**
* Full list: http://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references
* @param original: the original string that may contain special characters
* @return a new string that all specials have been replaced with Unicode Code Point(Decimal)
*/
public static String replaceSpecialCharacters(String original) {
Map<String, String> specials = new HashMap<String, String>();
//from 192 to 255
specials.put("À", "À");
specials.put("Á", "Á");
specials.put("Â", "Â");
specials.put("Ã", "Ã");
specials.put("Ä", "Ä");
specials.put("Ä", "Ä");
specials.put("Å", "Å");
specials.put("Æ", "Æ");
specials.put("Ç", "Ç");
specials.put("È", "È");
specials.put("É", "É");
specials.put("Ê", "Ê");
specials.put("Ë", "Ë");
specials.put("Ì", "Ì");
specials.put("Í", "Í");
specials.put("Î", "Î");
specials.put("Ï", "Ï");
specials.put("Ð", "Ð");
specials.put("Ñ", "Ñ");
specials.put("Ò", "Ò");
specials.put("Ó", "Ó");
specials.put("Ô", "Ô");
specials.put("Õ", "Õ");
specials.put("Ö", "Ö");
specials.put("×", "×");
specials.put("Ø", "Ø");
specials.put("Ù", "Ù");
specials.put("Ú", "Ú");
specials.put("Û", "Û");
specials.put("Ü", "Ü");
specials.put("Ý", "Ý");
specials.put("Þ", "Þ");
specials.put("ß", "ß");
specials.put("à", "à");
specials.put("á", "á");
specials.put("â", "â");
specials.put("ã", "ã");
specials.put("ä", "ä");
specials.put("å", "å");
specials.put("æ", "æ");
specials.put("ç", "ç");
specials.put("è", "è");
specials.put("é", "é");
specials.put("ê", "ê");
specials.put("ë", "ë");
specials.put("ì", "ì");
specials.put("í", "í");
specials.put("î", "î");
specials.put("ï", "ï");
specials.put("ð", "ð");
specials.put("ñ", "ñ");
specials.put("ò", "ò");
specials.put("ó", "ó");
specials.put("ô", "ô");
specials.put("õ", "õ");
specials.put("ö", "ö");
specials.put("÷", "÷");
specials.put("ø", "ø");
specials.put("ù", "ù");
specials.put("ú", "ú");
specials.put("û", "û");
specials.put("ü", "ü");
specials.put("ý", "ý");
specials.put("þ", "þ");
specials.put("ÿ", "ÿ");
//from xx to xx, if we need more
for (String key : specials.keySet()) {
original = original.replace(key, specials.get(key));
}
return original;
}
/**
*
* @param xml
* xml to be pretifized
*
* @return pretifized xml
*/
/*
* public static String pretty(String xml) { try { org.dom4j.Document
* document = DocumentHelper.parseText(xml) .getDocument();
*
* String res = formatXml(document, false); return res; } catch
* (DocumentException e) { e.printStackTrace(); throw new
* RuntimeException("Can't parse xml", e); } }
*/
/*
* public static String formatXml(Node node, boolean oneLine) {
*
* try { TransformerFactory tf = TransformerFactory.newInstance();
* Transformer transformer = tf.newTransformer();
*
* transformer.setOutputProperty("omit-xml-declaration", "yes");
* transformer.setOutputProperty("method", "xml");
* transformer.setOutputProperty("encoding", "ISO-8859-1");
* transformer.setOutputProperty(
* "{http://xml.apache.org/xslt}indent-amount", "4");
* transformer.setOutputProperty("indent", "yes"); java.io.StringWriter sw =
* new java.io.StringWriter(); StreamResult sr = new StreamResult(sw); //
* Must strip out new lines and whitespace, because formatter thinks // this
* is content that should be preserved in pretty String xml =
* node.asXML().replaceAll(">\\s*(\\r|\\n)\\s*", ">")
* .replaceAll("\\s*(\\r|\\n)\\s*<", "<");
*
* // String xml = node.asXML(); ByteArrayInputStream inputStream = new
* ByteArrayInputStream(xml .getBytes("UTF-8")); Source domSource = new
* StreamSource(inputStream); transformer.transform(domSource, sr);
*
* String prettyXml = sw.toString();
*
* // Although this looks like the same thing as above, it actually // isn't
* // if (oneLine) { // prettyXml =
* prettyXml.replaceAll(">\\s*(\\r|\\n)\\s*<", "><"); // }
*
* return prettyXml; } catch (Exception e) { throw new RuntimeException(e);
* } }
*/
}