KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > tools > files > 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.tools.files;
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
28 /**
29  * Substitute external DTD files with local DTD files.
30  *
31  * @author Khue
32  * @version 1.0
33  */

34 public class DtdEntityResolver implements org.xml.sax.EntityResolver JavaDoc {
35
36     /**
37      * Registered DTD, the key is the public id
38      */

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

48     public org.xml.sax.InputSource JavaDoc resolveEntity (String JavaDoc publicID, String JavaDoc systemID) {
49
50         //System.out.println("DtdEntityResolver: " + publicID + " " + systemID);
51

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

81     public void registerDTD(String JavaDoc publicID, String JavaDoc dtd) {
82     
83         m_DTDs.put(publicID, dtd);
84
85     }
86
87
88 }
Popular Tags