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.bpel.project;
020:
021: import java.io.File;
022: import java.io.IOException;
023:
024: import java.net.MalformedURLException;
025: import java.net.URI;
026: import java.util.ArrayList;
027:
028: import org.apache.xml.resolver.Catalog;
029: import org.apache.xml.resolver.CatalogManager;
030: import org.apache.xml.resolver.helpers.Debug;
031: import org.apache.xml.resolver.tools.CatalogResolver;
032:
033: import org.netbeans.modules.xml.xam.locator.CatalogModelException;
034:
035: /**
036: * Helper class to read catalog.xml
037: * @author Sreenivasan Genipudi
038: */
039: public class ApacheResolverHelper {
040: /**
041: * Constructor
042: */
043: public ApacheResolverHelper() {
044: }
045:
046: /**
047: * Checks if the given location is listed in catalog.xml
048: * @param catalogLocation Location of Catalog.xml
049: * @param locationURI The location URI to look for
050: * @return true if present else returns false
051: */
052: public static boolean isPresent(String catalogLocation,
053: String locationURI) {
054: try {
055: if (getURI(catalogLocation, locationURI) != null) {
056: return true;
057: }
058: } catch (Exception ex) {
059:
060: }
061: return false;
062: }
063:
064: public static String getURI(String catalogLocation,
065: String locationURI) {
066: CatalogResolver catalogResolver;
067: Catalog apacheCatalogResolverObj;
068:
069: CatalogManager manager = new CatalogManager(null);
070: manager.setUseStaticCatalog(false);
071: manager.setPreferPublic(false);
072: catalogResolver = new CatalogResolver(manager);
073:
074: apacheCatalogResolverObj = catalogResolver.getCatalog();
075: try {
076: apacheCatalogResolverObj.parseCatalog(catalogLocation);
077: } catch (Exception me) {
078: throw new RuntimeException(me);
079: }
080:
081: String result = null;
082: try {
083: result = apacheCatalogResolverObj
084: .resolveSystem(locationURI);
085: } catch (MalformedURLException ex) {
086: result = null;
087: } catch (IOException ex) {
088: result = null;
089: }
090: if (result != null && result.equals("")) {
091: result = null;
092: }
093: return result;
094: }
095:
096: public static boolean isPresent(
097: ArrayList<File> catalogLocationList, String locationURI) {
098: CatalogResolver catalogResolver;
099: Catalog apacheCatalogResolverObj;
100:
101: CatalogManager manager = new CatalogManager(null);
102: manager.setUseStaticCatalog(false);
103: manager.setPreferPublic(false);
104: catalogResolver = new CatalogResolver(manager);
105:
106: apacheCatalogResolverObj = catalogResolver.getCatalog();
107: for (File catalogLocation : catalogLocationList) {
108: try {
109: if (catalogLocation != null && catalogLocation.exists()) {
110: apacheCatalogResolverObj
111: .parseCatalog(catalogLocation
112: .getAbsolutePath());
113: } else {
114: continue;
115: }
116: } catch (Exception me) {
117: throw new RuntimeException(me);
118: }
119:
120: String result = null;
121: try {
122: result = apacheCatalogResolverObj
123: .resolveSystem(locationURI);
124: if (result != null && (!result.equals(""))) {
125: return true;
126: }
127:
128: } catch (MalformedURLException ex) {
129: result = "";
130: } catch (IOException ex) {
131: result = "";
132: }
133: return false;
134: }
135: return false;
136: }
137:
138: }
|