KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > servermgmt > util > DomainXmlSAXParser


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.admin.servermgmt.util;
25
26 import org.xml.sax.Attributes JavaDoc;
27 import org.xml.sax.InputSource JavaDoc;
28 import org.xml.sax.SAXException JavaDoc;
29 import org.xml.sax.EntityResolver JavaDoc;
30 import org.xml.sax.helpers.DefaultHandler JavaDoc;
31 import javax.xml.parsers.SAXParser JavaDoc;
32 import javax.xml.parsers.SAXParserFactory JavaDoc;
33 import java.io.File JavaDoc;
34
35 public class DomainXmlSAXParser extends DefaultHandler JavaDoc {
36   private final String JavaDoc PROPERTY = "property";
37   private int level = 0;
38   private String JavaDoc domainXmlEventListenerClass= null;
39   private File JavaDoc dtd;
40
41   public String JavaDoc getDomainXmlEventListenerClass() {
42     return domainXmlEventListenerClass;
43   }
44
45   public void parse(java.io.File JavaDoc domainXml,java.io.File JavaDoc dtd) throws javax.xml.parsers.ParserConfigurationException JavaDoc,org.xml.sax.SAXException JavaDoc,java.io.IOException JavaDoc {
46             this.dtd = dtd;
47         SAXParser JavaDoc saxParser;
48         SAXParserFactory JavaDoc factory = SAXParserFactory.newInstance();
49         saxParser = factory.newSAXParser();
50             saxParser.getXMLReader().setEntityResolver((EntityResolver JavaDoc)this);
51         saxParser.parse(domainXml,this);
52   }
53
54
55   public void startElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName, Attributes JavaDoc attrs)
56   throws SAXException JavaDoc {
57       level++;
58       if ( level==2 && PROPERTY.equals(qName)){
59           if (attrs != null) {
60               for (int i = 0; i < attrs.getLength(); i++) {
61                   String JavaDoc aName = attrs.getQName(i); // Attr name
62
String JavaDoc aValue = attrs.getValue(aName);
63                   if ("DomainXmlEventListenerClass".equals(aValue)) {
64                       domainXmlEventListenerClass=attrs.getValue("value");
65                   }
66               }
67           }
68       }
69   }
70
71
72   public void endElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName)
73       throws SAXException JavaDoc {
74       level--;
75   }
76
77   public InputSource JavaDoc resolveEntity(String JavaDoc publicId,String JavaDoc systemId) throws SAXException JavaDoc {
78       InputSource JavaDoc is = null;
79       try {
80           is = new InputSource JavaDoc(new java.io.FileInputStream JavaDoc(dtd));
81       } catch(Exception JavaDoc e) {
82           throw new SAXException JavaDoc("cannot resolve dtd", e);
83       }
84       return is;
85   }
86 }
87
88
89
Popular Tags