001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019: package org.netbeans.modules.xslt.project.anttasks;
020:
021: import java.io.FileReader;
022:
023: import java.net.MalformedURLException;
024: import java.util.ArrayList;
025:
026: import java.util.HashMap;
027: import java.util.HashSet;
028: import java.util.List;
029: import java.util.Set;
030:
031: import org.apache.xml.resolver.Catalog;
032: import org.apache.xml.resolver.CatalogManager;
033: import org.apache.xml.resolver.tools.CatalogResolver;
034: import org.apache.xml.resolver.tools.ResolvingXMLReader;
035:
036: import org.xml.sax.Attributes;
037: import org.xml.sax.InputSource;
038: import org.xml.sax.helpers.DefaultHandler;
039:
040: /**
041: * This class helps Xslt project to Read the Catalog XML file
042: * @author Sreenivasan Genipudi
043: */
044: public class XsltProjectCatalogReader {
045: private MyContentHandler mContentHandler = new MyContentHandler();
046:
047: /**
048: * Constructor
049: * @param catalogXML Location of Catalog XML
050: * @throws Excepetion Exception during parsing the Catalog.xml file.
051: */
052: public XsltProjectCatalogReader(String catalogXML) throws Exception {
053: CatalogResolver catalogResolver;
054: Catalog apacheCatalogResolverObj;
055:
056: CatalogManager manager = new CatalogManager(null);
057: manager.setUseStaticCatalog(false);
058: manager.setPreferPublic(false);
059: catalogResolver = new CatalogResolver(manager);
060: apacheCatalogResolverObj = catalogResolver.getCatalog();
061: try {
062: apacheCatalogResolverObj.parseCatalog(catalogXML);
063: } catch (MalformedURLException ex) {
064: throw new RuntimeException(ex);
065: }
066: ResolvingXMLReader saxParser = new ResolvingXMLReader(manager);
067: saxParser.setContentHandler(mContentHandler);
068: FileReader finRead = new FileReader(catalogXML);
069: InputSource isource = new InputSource(finRead);
070: saxParser.parse(isource);
071: }
072:
073: /**
074: * Get the list of URI's listed in Catalog.xml
075: * @return Set of URIs listed in Catalog.xml
076: */
077: public ArrayList<String> getListOfLocalURIs() {
078: return mContentHandler.listOfURI;
079: }
080:
081: public int locateNS(String ns) {
082: if (mContentHandler.listOfNS.size() <= 0) {
083: return -1;
084: }
085: int in = 0;
086: for (String myNS : mContentHandler.listOfNS) {
087: if (myNS.equals(ns)) {
088: return in;
089: }
090: in++;
091: }
092: return -1;
093: }
094:
095: /**
096: * Get the Set of Namespaces listed in Catalog.xml
097: * @return Set of Namespaces listed in Catalog.xml
098: */
099: public ArrayList<String> getListOfNamespaces() {
100: return mContentHandler.listOfNS;
101: }
102:
103: }
104:
105: class MyContentHandler extends DefaultHandler {
106: private static final String SYSTEM_CONST = "system";
107: private static final String SYSTEM_ID_CONST = "systemId";
108: private static final String URI_CONST = "uri";
109: boolean isSystem = false;
110: ArrayList<String> listOfURI = new ArrayList<String>();
111: ArrayList<String> listOfNS = new ArrayList<String>();
112:
113: public List getURIs() {
114: return listOfURI;
115: }
116:
117: public List getNSs() {
118: return listOfNS;
119: }
120:
121: public void startElement(String uri, String localName,
122: String qName, Attributes atts) {
123: if (qName.equals(SYSTEM_CONST)) {
124: String nameSpace = atts.getValue(SYSTEM_ID_CONST);
125: String location = atts.getValue(URI_CONST);
126: if (nameSpace != null) {
127: listOfNS.add(nameSpace);
128: }
129: if (location != null) {
130: listOfURI.add(location);
131: }
132: }
133:
134: }
135:
136: public void endElement(String uri, String localName, String qName) {
137: }
138:
139: public void characters(char[] chars, int start, int length) {
140: }
141:
142: }
|