source: trunk/gsdl-documentation/shared/ExternalRef.java@ 13650

Last change on this file since 13650 was 13650, checked in by lh92, 17 years ago

added a method to find where is the xml-source directory

  • Property svn:keywords set to Author Date Id Revision
File size: 2.0 KB
Line 
1import java.io.InputStream;
2import java.io.FileInputStream;
3import java.io.File;
4
5import org.w3c.dom.Document;
6import javax.xml.parsers.DocumentBuilderFactory;
7import javax.xml.parsers.DocumentBuilder;
8import org.w3c.dom.Document;
9import org.w3c.dom.Element;
10import org.w3c.dom.Node;
11import org.w3c.dom.NodeList;
12
13
14public 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 System.out.println(list.getLength());
32 int i=0;
33 for(i=0; i<list.getLength(); i++){
34 Node node = list.item(i);
35 String id_temp = node.getAttributes().getNamedItem("id").getNodeValue();
36 if(id_temp.equals(id)) break;
37 }
38 return (++i)+"";
39
40 }catch(Exception e){
41 System.err.println(e.getMessage());
42 //e.printStackTrace();
43 return "";
44 }
45 }
46
47 private void getPath(){
48 basePath = System.getProperty("user.dir");
49
50 System.err.println(basePath);
51
52 //find where is the manuals directory
53 File tempfile = new File(basePath);
54 String parentPath = tempfile.getParent();
55
56 boolean find = false;
57 while(!find){
58 System.err.println(parentPath);
59 File tempfile2 = new File(parentPath+"/manuals");
60 if(tempfile2.exists() && tempfile2.isDirectory()){
61 basePath = parentPath+"/manuals";
62 find = true;
63 }
64 else if(parentPath.endsWith("manuals")){
65 //if the shared directory is inside the manual directory
66 basePath = parentPath;
67 find = true;
68 }
69 if(!find){
70 tempfile = new File(parentPath);
71 parentPath = tempfile.getParent();
72 }
73 }
74 }
75}
Note: See TracBrowser for help on using the repository browser.