KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > utils > xml > DtdEntityResolver


1 //
2
// ____.
3
// __/\ ______| |__/\. _______
4
// __ .____| | \ | +----+ \
5
// _______| /--| | | - \ _ | : - \_________
6
// \\______: :---| : : | : | \________>
7
// |__\---\_____________:______: :____|____:_____\
8
// /_____|
9
//
10
// . . . i n j a h i a w e t r u s t . . .
11
//
12
//
13
//
14
// DtdEntityResolver
15
//
16
// NK 08.03.2001
17
//
18

19 package org.jahia.utils.xml;
20
21
22 import java.io.File JavaDoc;
23 import java.io.FileInputStream JavaDoc;
24 import java.io.FileNotFoundException JavaDoc;
25 import java.util.Hashtable JavaDoc;
26
27 import org.xml.sax.EntityResolver JavaDoc;
28 import org.xml.sax.InputSource JavaDoc;
29
30
31 /**
32  * Substitute external DTD files with local DTD files.
33  *
34  * @author Khue
35  * @version 1.0
36  */

37 public class DtdEntityResolver implements EntityResolver JavaDoc {
38
39     /**
40      * Registered DTD, the key is the public id
41      */

42     private Hashtable JavaDoc m_DTDs = new Hashtable JavaDoc();
43     
44
45     /**
46      * Replace external dtd with local dtd.
47      * Return an InputSource to a local DTD file or null if not found
48      * in the registered DTD
49      *
50      */

51     public InputSource JavaDoc resolveEntity (String JavaDoc publicID, String JavaDoc systemID) {
52
53         //System.out.println("DtdEntityResolver: " + publicID + " " + systemID);
54

55         if ( publicID == null ){
56             return null;
57         }
58         
59         String JavaDoc dtd = null;
60                 
61         if ( m_DTDs.get(publicID) != null ){
62             dtd = (String JavaDoc) m_DTDs.get(publicID);
63         }
64         
65         if( dtd != null ) {
66             //System.out.println("DtdEntityResolver: found dtd ");
67
File JavaDoc f = new File JavaDoc( dtd );
68             if( f.exists() )
69             try {
70                 return new InputSource JavaDoc(new FileInputStream JavaDoc(f));
71             } catch( FileNotFoundException JavaDoc ex ) {
72             }
73         }
74
75         return null;
76     }
77
78
79     /**
80      *
81      * @param publicID the key to identify the requested dtd
82      * @param dtd the dtd resource file
83      */

84     public void registerDTD(String JavaDoc publicID, String JavaDoc dtd) {
85     
86         m_DTDs.put(publicID, dtd);
87
88     }
89
90
91 }
Popular Tags