1 | import java.io.InputStream;
|
---|
2 | import java.io.FileInputStream;
|
---|
3 | import java.io.File;
|
---|
4 |
|
---|
5 | import org.w3c.dom.Document;
|
---|
6 | import javax.xml.parsers.DocumentBuilderFactory;
|
---|
7 | import javax.xml.parsers.DocumentBuilder;
|
---|
8 | import org.w3c.dom.Document;
|
---|
9 | import org.w3c.dom.Element;
|
---|
10 | import org.w3c.dom.Node;
|
---|
11 | import org.w3c.dom.NodeList;
|
---|
12 |
|
---|
13 |
|
---|
14 | public class ExternalRef{
|
---|
15 | private String basePath;
|
---|
16 | public ExternalRef(){
|
---|
17 | }
|
---|
18 |
|
---|
19 | public String getRef(String file, String lang, String targettype, String id){
|
---|
20 | getPath();
|
---|
21 | try{
|
---|
22 | String tfile = file+"_"+lang+".xml";
|
---|
23 | String path = basePath + "/xml-source/" + lang + "/" + tfile;
|
---|
24 |
|
---|
25 | DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
---|
26 | DocumentBuilder domBuilder = factory.newDocumentBuilder();
|
---|
27 | InputStream is = new FileInputStream(path);
|
---|
28 | Document doc = domBuilder.parse(is);
|
---|
29 |
|
---|
30 | NodeList list = doc.getElementsByTagName(targettype);
|
---|
31 | int i=0;
|
---|
32 | for(i=0; i<list.getLength(); i++){
|
---|
33 | Node node = list.item(i);
|
---|
34 | String id_temp = node.getAttributes().getNamedItem("id").getNodeValue();
|
---|
35 | if(id_temp.equals(id)) break;
|
---|
36 | }
|
---|
37 | return (++i)+"";
|
---|
38 |
|
---|
39 | }catch(Exception e){
|
---|
40 | System.err.println(e.getMessage());
|
---|
41 | //e.printStackTrace();
|
---|
42 | return "";
|
---|
43 | }
|
---|
44 | }
|
---|
45 |
|
---|
46 | private void getPath(){
|
---|
47 | basePath = System.getProperty("user.dir");
|
---|
48 |
|
---|
49 | //find where is the manuals directory
|
---|
50 | File tempfile = new File(basePath);
|
---|
51 | String parentPath = tempfile.getParent();
|
---|
52 |
|
---|
53 | boolean find = false;
|
---|
54 | while(!find){
|
---|
55 | File tempfile2 = new File(parentPath+"/manuals");
|
---|
56 | if(tempfile2.exists() && tempfile2.isDirectory()){
|
---|
57 | basePath = parentPath+"/manuals";
|
---|
58 | find = true;
|
---|
59 | }
|
---|
60 | else if(parentPath.endsWith("manuals")){
|
---|
61 | //if the shared directory is inside the manual directory
|
---|
62 | basePath = parentPath;
|
---|
63 | find = true;
|
---|
64 | }
|
---|
65 | if(!find){
|
---|
66 | tempfile = new File(parentPath);
|
---|
67 | parentPath = tempfile.getParent();
|
---|
68 | }
|
---|
69 | }
|
---|
70 | }
|
---|
71 | }
|
---|