KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cactus > integration > ant > deployment > application > ApplicationXmlIo


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

20 package org.apache.cactus.integration.ant.deployment.application;
21
22 import java.io.File JavaDoc;
23 import java.io.FileInputStream JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.InputStream JavaDoc;
26
27 import javax.xml.parsers.DocumentBuilder JavaDoc;
28 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
29 import javax.xml.parsers.ParserConfigurationException JavaDoc;
30
31 import org.xml.sax.EntityResolver JavaDoc;
32 import org.xml.sax.InputSource JavaDoc;
33 import org.xml.sax.SAXException JavaDoc;
34
35 /**
36  * Provides convenience methods for reading and writing enterprise application
37  * deployment descriptors (application.xml).
38  *
39  * @since Cactus 1.5
40  * @version $Id: ApplicationXmlIo.java,v 1.1 2004/05/31 20:05:24 vmassol Exp $
41  */

42 public class ApplicationXmlIo
43 {
44     
45     // Inner Classes -----------------------------------------------------------
46

47     /**
48      * Implementation of the SAX EntityResolver interface that looks up the
49      * application DTDs from the JAR.
50      */

51     private static class ApplicationXmlEntityResolver implements EntityResolver JavaDoc
52     {
53
54         /**
55          * @see org.xml.sax.EntityResolver#resolveEntity
56          */

57         public InputSource JavaDoc resolveEntity(String JavaDoc thePublicId, String JavaDoc theSystemId)
58             throws SAXException JavaDoc, IOException JavaDoc
59         {
60             ApplicationXmlVersion version =
61                 ApplicationXmlVersion.valueOf(thePublicId);
62             if (version != null)
63             {
64                 String JavaDoc fileName = version.getSystemId().substring(
65                     version.getSystemId().lastIndexOf('/'));
66                 InputStream JavaDoc in = this.getClass().getResourceAsStream(
67                     "/org/apache/cactus/integration/ant/deployment/resources"
68                     + fileName);
69                 if (in != null)
70                 {
71                     return new InputSource JavaDoc(in);
72                 }
73             }
74             return null;
75         }
76
77     }
78
79     // Public Methods ----------------------------------------------------------
80

81     /**
82      * Parses a deployment descriptor stored in a regular file.
83      *
84      * @param theFile The file to parse
85      * @param theEntityResolver A SAX entity resolver, or <code>null</code> to
86      * use the default
87      * @return The parsed descriptor
88      * @throws SAXException If the file could not be parsed
89      * @throws ParserConfigurationException If the XML parser was not correctly
90      * configured
91      * @throws IOException If an I/O error occurs
92      */

93     public static ApplicationXml parseApplicationXmlFromFile(File JavaDoc theFile,
94         EntityResolver JavaDoc theEntityResolver)
95         throws SAXException JavaDoc, ParserConfigurationException JavaDoc, IOException JavaDoc
96     {
97         InputStream JavaDoc in = null;
98         try
99         {
100             in = new FileInputStream JavaDoc(theFile);
101             return parseApplicationXml(in, theEntityResolver);
102         }
103         finally
104         {
105             if (in != null)
106             {
107                 try
108                 {
109                     in.close();
110                 }
111                 catch (IOException JavaDoc ioe)
112                 {
113                     // we'll pass on the original IO error, so ignore this one
114
}
115             }
116         }
117     }
118     
119     /**
120      * Parses a deployment descriptor provided as input stream.
121      *
122      * @param theInput The input stream
123      * @param theEntityResolver A SAX entity resolver, or <code>null</code> to
124      * use the default
125      * @return The parsed descriptor
126      * @throws SAXException If the input could not be parsed
127      * @throws ParserConfigurationException If the XML parser was not correctly
128      * configured
129      * @throws IOException If an I/O error occurs
130      */

131     public static ApplicationXml parseApplicationXml(InputStream JavaDoc theInput,
132         EntityResolver JavaDoc theEntityResolver)
133         throws SAXException JavaDoc, ParserConfigurationException JavaDoc, IOException JavaDoc
134     {
135         DocumentBuilderFactory JavaDoc factory =
136             DocumentBuilderFactory.newInstance();
137         factory.setValidating(false);
138         factory.setNamespaceAware(false);
139         DocumentBuilder JavaDoc builder = factory.newDocumentBuilder();
140         if (theEntityResolver != null)
141         {
142             builder.setEntityResolver(theEntityResolver);
143         }
144         else
145         {
146             builder.setEntityResolver(new ApplicationXmlEntityResolver());
147         }
148         return new DefaultApplicationXml(builder.parse(theInput));
149     }
150
151 }
152
Popular Tags