KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jdon > controller > config > XmlParser


1 /**
2  * Copyright 2003-2006 the original author or authors.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6
7        http://www.apache.org/licenses/LICENSE-2.0
8
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */

15
16 package com.jdon.controller.config;
17
18 import java.io.InputStream JavaDoc;
19 import java.util.LinkedHashMap JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import org.jdom.Document;
23 import org.jdom.Element;
24 import org.jdom.JDOMException;
25 import org.jdom.input.SAXBuilder;
26
27 import com.jdon.util.Debug;
28 import com.jdon.util.FileLocator;
29
30 /**
31  * by Jdom parse the jdonframework.xml
32  * dom4j is faster than jdom, but this parser only be runned for
33  * one time ,so parsing speed is not important.
34  *
35  * <p>@author <a HREF="mailto:banqiao@jdon.com">banq</a></p>
36  * <p>@version JdonFramework 2005 v1.0</p>
37  */

38 public abstract class XmlParser {
39   private final static String JavaDoc module = XmlParser.class.getName();
40   
41   private FileLocator fileLocator = new FileLocator();
42
43   public Map JavaDoc load(String JavaDoc configFileName) {
44     Map JavaDoc mps = new LinkedHashMap JavaDoc();
45     try {
46       if (configFileName == null)
47          return mps;
48       
49       Document doc = buildDocument(configFileName);
50       if (doc == null)
51         return mps;
52       
53       Element root = doc.getRootElement();
54       //Iterator iter = root.getChildren().iterator();
55
//while(iter.hasNext()){
56
//Debug.logVerbose("[JdonFramework] test: " + iter.next(), module);
57
//}
58
parse(root, mps);
59
60       Debug.logVerbose("[JdonFramework]<!-- config load finished -->", module);
61     } catch (Exception JavaDoc ex) {
62       Debug.logError("[JdonFramework]configure FileName: " + configFileName + " parsed error: " + ex, module);
63     }
64     return mps;
65   }
66
67   private Document buildDocument(String JavaDoc configFileName) {
68     Debug.logVerbose("[JdonFramework] locate configure file :" + configFileName, module);
69     Document doc = null;
70     try {
71         InputStream JavaDoc xmlStream = fileLocator.getConfPathXmlStream(configFileName);
72         if (xmlStream == null){
73             Debug.logVerbose("[JdonFramework]can't locate file:" + configFileName, module);
74             return null;
75         }else{
76             Debug.logVerbose("[JdonFramework] configure file found :" + xmlStream, module);
77         }
78
79         SAXBuilder builder = new SAXBuilder();
80         builder.setEntityResolver(new DTDEntityResolver());
81         doc = builder.build(xmlStream);
82         Debug.logVerbose("[JdonFramework] got mapping file ", module);
83     } catch (JDOMException e) {
84         Debug.logError("[JdonFramework] JDOMException error: " + e, module);
85     }
86     return doc;
87   }
88
89   public abstract void parse(Element root, Map JavaDoc results) throws Exception JavaDoc;
90
91   
92
93 }
94
Popular Tags