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.File;
022:
023: import java.util.ArrayList;
024: import java.util.HashSet;
025: import java.util.Set;
026:
027: import java.util.logging.Logger;
028:
029: import org.apache.tools.ant.BuildException;
030: import org.apache.tools.ant.Project;
031: import org.apache.tools.ant.taskdefs.Copy;
032: import org.apache.tools.ant.taskdefs.Delete;
033: import org.apache.tools.ant.types.FileSet;
034:
035: import org.netbeans.modules.xslt.project.CommandlineXsltProjectXmlCatalogProvider;
036:
037: public class PackageCatalogArtifacts extends org.apache.tools.ant.Task {
038:
039: private static final String dirSep = "/";
040: public static HashSet mValidRetrievedUriSet = new HashSet();
041: // Member variable representing build directory
042: /**
043: * Logger instance
044: */
045: private Logger logger = Logger
046: .getLogger(PackageCatalogArtifacts.class.getName());
047:
048: /**
049: * Build directory
050: */
051: private String mBuildDirectoryPath = null;
052:
053: public PackageCatalogArtifacts() {
054: }
055:
056: public void doCopy(String projectCatalogPath, File buildDir) {
057: projectCatalogPath = projectCatalogPath.replace('\\', '/');
058: File localCatalogFile = new File(
059: CommandlineXsltProjectXmlCatalogProvider.getInstance()
060: .getProjectWideCatalog());
061: String localCatalogPath = null;
062: if (!localCatalogFile.exists()) {
063: return;
064: } else {
065: localCatalogPath = localCatalogFile.getAbsolutePath()
066: .replace('\\', '/');
067: }
068:
069: File catalogFile = new File(projectCatalogPath);
070: if (catalogFile.exists() && catalogFile.length() > 0) {
071: Set<String> setOfURIs = null;
072: XsltProjectCatalogReader bpProjectCatRdr = null;
073: XsltProjectCatalogReader bpLocalCatRdr = null;
074: try {
075: bpProjectCatRdr = new XsltProjectCatalogReader(
076: projectCatalogPath);
077: bpLocalCatRdr = new XsltProjectCatalogReader(
078: localCatalogPath);
079: doCopy(buildDir, bpProjectCatRdr, bpLocalCatRdr);
080: } catch (Throwable ex) {
081: logger.fine(ex.getMessage());
082: }
083: }
084:
085: }
086:
087: private void doCopy(File mBuildDir,
088: XsltProjectCatalogReader projectCtlg,
089: XsltProjectCatalogReader localCtlg) throws Exception {
090:
091: ArrayList<String> listOfProjectNS = projectCtlg
092: .getListOfNamespaces();
093: ArrayList<String> listOfProjoectURIs = projectCtlg
094: .getListOfLocalURIs();
095:
096: ArrayList<String> listOfLocalURIs = localCtlg
097: .getListOfLocalURIs();
098: ArrayList<String> listOfLocalNSs = localCtlg
099: .getListOfNamespaces();
100:
101: Copy copyOp = new Copy();
102: String metaInfDir = mBuildDir.getAbsolutePath() + dirSep
103: + "META-INF";
104: File metaInfFile = new File(metaInfDir);
105: if (!metaInfFile.exists()) {
106: metaInfFile.mkdirs();
107: }
108: org.apache.tools.ant.Project packProject = new org.apache.tools.ant.Project();
109: packProject.init();
110: copyOp.setProject(packProject);
111: int localIndex = -1;
112: int projIndx = -1;
113: int projectIndex = -1;
114: String localURILoc = null;
115: String prjURILoc = null;
116: File localURIFile = null;
117: String projURLoc = null;
118: for (String ns : listOfLocalNSs) {
119: //Get the index of the NS in Project catalog
120: projIndx = projectCtlg.locateNS(ns);
121: //If the entry is not found in project catalog leave it
122: if (projIndx == -1) {
123: continue;
124: }
125: //Check the Namespace entry in Local Catalog (retreived\catalog.xml)
126: localIndex = localCtlg.locateNS(ns);
127: //If found, get the URI Location from local catalog
128: localURILoc = (String) listOfLocalURIs.get(localIndex);
129: prjURILoc = (String) listOfProjoectURIs.get(projIndx);
130: prjURILoc = prjURILoc.replace('\\', '/');
131:
132: localURIFile = new File(localURILoc);
133: int delimIndx = -1;
134: String localURIFileParentDir = localURIFile.getParent();
135: if (localURIFileParentDir != null) {
136: localURILoc = localURIFileParentDir.replace('\\', '/');
137: } else {
138: localURILoc = localURIFile.getAbsolutePath().replace(
139: '\\', '/');
140: delimIndx = localURILoc.lastIndexOf("/");
141: if (delimIndx > 0) {
142: localURILoc = localURILoc.substring(0, delimIndx);
143: } else {
144: continue;
145: }
146: }
147: //Set the destination Dir
148: copyOp
149: .setTodir(new File(metaInfDir + dirSep
150: + localURILoc));
151: copyOp.setOverwrite(true);
152:
153: FileSet fs = null;
154: File projectDir = mBuildDir.getParentFile();
155: if (projectDir == null) {
156: projectDir = new File(mBuildDir.getAbsolutePath()
157: + dirSep + "..");
158: }
159: File deleteDir = null;
160: boolean bDelete = true;
161: copyOp.setFile(new File(projectDir + dirSep + prjURILoc));
162: copyOp.execute();
163:
164: }
165:
166: }
167:
168: }
|