-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathLoader.java
executable file
·96 lines (87 loc) · 3.09 KB
/
Loader.java
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
package xml_ini;
import java.io.*;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import javax.xml.xpath.XPathExpression;
import org.w3c.dom.Document;
/** ôàéë, êîòîðûé çàãðóæàåò ïåðåìåííûå èç óêàçàííûõ ïóòåé âèäà XPath èç ïðî÷èòàííîãî XML ôàéëà*/
public class Loader {
/** âûâîä îòëàäî÷íîé èíôîðìàöèè */
private static void debug(String information){
System.out.print("Loader");
System.out.print(" DEBUG ");
System.out.println(information);
}
/** âûâîä îøèáî÷íîé èíôîðìàöèè */
private static void error(String information){
System.out.print("Loader");
System.out.print(" ERROR ");
System.out.println(information);
}
private Document field_document;
private XPath field_xpath;
/** ôàéë, êîòîðûé çàãðóæàåò ïåðåìåííûå èç óêàçàííûõ ïóòåé âèäà XPath èç ïðî÷èòàííîãî XML ôàéëà*/
public Loader(String path_to_xml) throws Exception {
// ÷òåíèå ôàéëà
File file=new File(path_to_xml);
if(!file.exists()){
throw new Exception();
}
// ñîçäàíèå äîêóìåíòà XML
this.field_document=getXmlByPath(path_to_xml);
// ñîçäàíèå îáúåêòà XPath
XPathFactory factory=XPathFactory.newInstance();
this.field_xpath=factory.newXPath();
}
/** ïîëó÷èòü XML ôàéë èç óêàçàííîãî àáñîëþòíîãî ïóòè */
private Document getXmlByPath(String path_to_xml) throws Exception{
Document return_value=null;
javax.xml.parsers.DocumentBuilderFactory document_builder_factory=javax.xml.parsers.DocumentBuilderFactory.newInstance();
// óñòàíîâèòü íåïðîâåðÿåìîå ïîðîæäåíèå Parser-îâ
document_builder_factory.setValidating(false);
try {
// ïîëó÷åíèå àíàëèçàòîðà
javax.xml.parsers.DocumentBuilder parser=document_builder_factory.newDocumentBuilder();
return_value=parser.parse(new File(path_to_xml));
}catch(Exception ex){
error("XmlExchange.java ERROR");
throw ex;
}
return return_value;
}
/** ïîëó÷èòü çíà÷åíèå èç XML ôàéëà ñîãëàñíî çàäàííîìó XPath ïóòè <br>
* @return âîçâðàùàåò ëèáî ïóñòóþ ñòðîêó, ëèáî çíà÷åíèå
* */
public String getValue(String path){
String return_value="";
try{
XPathExpression expression=this.field_xpath.compile(path);
return_value=(String) expression.evaluate(this.field_document,XPathConstants.STRING);
}catch(Exception ex){
error("getValue: Exception:"+ex.getMessage());
}
return return_value;
}
/** ïîëó÷èòü çíà÷åíèå èç XML ôàéëà ñîãëàñíî çàäàííîìó XPath ïóòè <br>
* @return âîçâðàùàåò ëèáî default_value
* */
public int getInteger(String path,int default_value){
int return_value=default_value;
try{
return_value=Integer.parseInt(getValue(path));
}catch(Exception ex){
error("getInteger: Exception:"+ex.getMessage());
}
return return_value;
}
public double getDouble(String path, double default_value){
double return_value=default_value;
try{
return_value=Double.parseDouble(getValue(path));
}catch(Exception ex){
error("getInteger: Exception:"+ex.getMessage());
}
return return_value;
}
}