174 |
ilm |
1 |
/*
|
|
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
|
|
3 |
*
|
|
|
4 |
* Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
|
|
|
5 |
*
|
|
|
6 |
* The contents of this file are subject to the terms of the GNU General Public License Version 3
|
|
|
7 |
* only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
|
|
|
8 |
* copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
|
|
|
9 |
* language governing permissions and limitations under the License.
|
|
|
10 |
*
|
|
|
11 |
* When distributing the software, include this License Header Notice in each file.
|
|
|
12 |
*/
|
|
|
13 |
|
|
|
14 |
package org.openconcerto.xml;
|
|
|
15 |
|
|
|
16 |
import java.io.BufferedInputStream;
|
|
|
17 |
import java.io.IOException;
|
|
|
18 |
import java.io.InputStream;
|
|
|
19 |
import java.io.OutputStream;
|
|
|
20 |
import java.io.PrintStream;
|
|
|
21 |
import java.util.Properties;
|
|
|
22 |
|
|
|
23 |
import org.jdom2.Document;
|
|
|
24 |
import org.jdom2.Element;
|
|
|
25 |
import org.jdom2.input.SAXBuilder;
|
|
|
26 |
import org.jdom2.input.sax.XMLReaderSAX2Factory;
|
|
|
27 |
|
|
|
28 |
public class FastXMLProperties {
|
|
|
29 |
|
|
|
30 |
public static void load(Properties props, InputStream in) throws IOException {
|
|
|
31 |
final SAXBuilder builder = new SAXBuilder(new XMLReaderSAX2Factory(false, "com.bluecast.xml.Piccolo"), null, null);
|
|
|
32 |
try {
|
|
|
33 |
final Document document = builder.build(new BufferedInputStream(in));
|
|
|
34 |
for (Element element : document.getRootElement().getChildren()) {
|
|
|
35 |
if (element.getName().equals("entry")) {
|
|
|
36 |
props.setProperty(element.getAttributeValue("key"), element.getText());
|
|
|
37 |
}
|
|
|
38 |
}
|
|
|
39 |
} catch (Exception e) {
|
|
|
40 |
throw new IOException(e);
|
|
|
41 |
}
|
|
|
42 |
|
|
|
43 |
}
|
|
|
44 |
|
|
|
45 |
/**
|
|
|
46 |
* Fast alternative (<1ms) for Properties.storeToXML (30ms)
|
|
|
47 |
*
|
|
|
48 |
*/
|
|
|
49 |
public static void store(Properties props, OutputStream out, String comment) throws IOException {
|
|
|
50 |
PrintStream prt = new PrintStream(out, false, "UTF-8");
|
|
|
51 |
prt.append("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\r\n");
|
|
|
52 |
prt.append("<properties>\\r\n");
|
|
|
53 |
if (comment != null) {
|
|
|
54 |
prt.append("<comment>");
|
|
|
55 |
prt.append(comment);
|
|
|
56 |
prt.append("</comment>\\r\n");
|
|
|
57 |
}
|
|
|
58 |
synchronized (props) {
|
|
|
59 |
for (String k : props.stringPropertyNames()) {
|
|
|
60 |
prt.append("<entry key=\"");
|
|
|
61 |
prt.append(XMLUtils.escapeAttribute(k));
|
|
|
62 |
prt.append("\">");
|
|
|
63 |
prt.append(XMLUtils.escapeText(props.getProperty(k)));
|
|
|
64 |
prt.append("</entry>\\r\n");
|
|
|
65 |
}
|
|
|
66 |
}
|
|
|
67 |
prt.append("</properties>");
|
|
|
68 |
prt.close();
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
}
|