KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > javadocexport > JavadocReader


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.ui.javadocexport;
12
13 import java.io.BufferedInputStream JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.io.InputStream JavaDoc;
16
17 import javax.xml.parsers.DocumentBuilder JavaDoc;
18 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
19 import javax.xml.parsers.ParserConfigurationException JavaDoc;
20
21 import org.eclipse.core.runtime.Assert;
22
23 import org.w3c.dom.Element JavaDoc;
24 import org.w3c.dom.Node JavaDoc;
25 import org.w3c.dom.NodeList JavaDoc;
26 import org.xml.sax.InputSource JavaDoc;
27 import org.xml.sax.SAXException JavaDoc;
28
29
30 /**
31  * Reads data from an InputStream and returns a JarPackage
32  */

33 public class JavadocReader extends Object JavaDoc {
34
35     private InputStream JavaDoc fInputStream;
36
37     /**
38      * Reads a Javadoc Ant Script from the underlying stream.
39      * It is the client's responsibility to close the stream.
40      */

41     public JavadocReader(InputStream JavaDoc inputStream) {
42         Assert.isNotNull(inputStream);
43         fInputStream= new BufferedInputStream JavaDoc(inputStream);
44     }
45
46     /**
47      * Closes this stream.
48      * It is the clients responsibility to close the stream.
49      *
50      * @exception IOException
51      */

52     public void close() throws IOException JavaDoc {
53         if (fInputStream != null)
54             fInputStream.close();
55     }
56
57     public Element JavaDoc readXML() throws IOException JavaDoc, SAXException JavaDoc {
58
59         DocumentBuilderFactory JavaDoc factory= DocumentBuilderFactory.newInstance();
60         factory.setValidating(false);
61         DocumentBuilder JavaDoc parser= null;
62         try {
63             parser= factory.newDocumentBuilder();
64         } catch (ParserConfigurationException JavaDoc ex) {
65             throw new IOException JavaDoc(ex.getMessage());
66         } finally {
67             // Note: Above code is OK since clients are responsible to close the stream
68
}
69
70         //find the project associated with the ant script
71
Element JavaDoc xmlJavadocDesc= parser.parse(new InputSource JavaDoc(fInputStream)).getDocumentElement();
72
73         NodeList JavaDoc targets= xmlJavadocDesc.getChildNodes();
74
75         for (int i= 0; i < targets.getLength(); i++) {
76             Node JavaDoc target= targets.item(i);
77
78             //look through the XML file for the javadoc task
79
if (target.getNodeName().equals("target")) { //$NON-NLS-1$
80
NodeList JavaDoc children= target.getChildNodes();
81                 for (int j= 0; j < children.getLength(); j++) {
82                     Node JavaDoc child= children.item(j);
83                     if (child.getNodeName().equals("javadoc") && (child.getNodeType() == Node.ELEMENT_NODE)) { //$NON-NLS-1$
84
return (Element JavaDoc) child;
85                     }
86                 }
87             }
88
89         }
90         return null;
91     }
92
93 }
94
Popular Tags