~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to xml/catalogsupport/src/org/netbeans/modules/xml/catalogsupport/util/ProjectReferenceUtility.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
6
 * The contents of this file are subject to the terms of either the GNU
 
7
 * General Public License Version 2 only ("GPL") or the Common
 
8
 * Development and Distribution License("CDDL") (collectively, the
 
9
 * "License"). You may not use this file except in compliance with the
 
10
 * License. You can obtain a copy of the License at
 
11
 * http://www.netbeans.org/cddl-gplv2.html
 
12
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
 
13
 * specific language governing permissions and limitations under the
 
14
 * License.  When distributing the software, include this License Header
 
15
 * Notice in each file and include the License file at
 
16
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
 
17
 * particular file as subject to the "Classpath" exception as provided
 
18
 * by Sun in the GPL Version 2 section of the License file that
 
19
 * accompanied this code. If applicable, add the following below the
 
20
 * License Header, with the fields enclosed by brackets [] replaced by
 
21
 * your own identifying information:
 
22
 * "Portions Copyrighted [year] [name of copyright owner]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
26
 * The Original Software is NetBeans. The Initial Developer of the Original
 
27
 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
 
28
 * Microsystems, Inc. All Rights Reserved.
 
29
 *
 
30
 * If you wish your version of this file to be governed by only the CDDL
 
31
 * or only the GPL Version 2, indicate your decision by adding
 
32
 * "[Contributor] elects to include this software in this distribution
 
33
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
 
34
 * single choice of license, a recipient has the option to distribute
 
35
 * your version of this file under either the CDDL, the GPL Version 2 or
 
36
 * to extend the choice of license to its licensees as provided above.
 
37
 * However, if you add GPL Version 2 code and therefore, elected the GPL
 
38
 * Version 2 license, then the option applies only if the new code is
 
39
 * made subject to such option by the copyright holder.
 
40
 */
 
41
 
 
42
package org.netbeans.modules.xml.catalogsupport.util;
 
43
 
 
44
import java.net.URI;
 
45
import java.net.URISyntaxException;
 
46
 
 
47
import org.openide.filesystems.FileObject;
 
48
import org.netbeans.api.project.Project;
 
49
import org.netbeans.api.project.ProjectInformation;
 
50
import org.netbeans.api.project.ProjectUtils;
 
51
import org.netbeans.api.project.ant.AntArtifact;
 
52
import org.netbeans.spi.project.ant.AntArtifactProvider;
 
53
import org.netbeans.spi.project.support.ant.PropertyUtils;
 
54
import org.netbeans.spi.project.support.ant.ReferenceHelper;
 
55
 
 
56
import org.netbeans.modules.xml.xam.locator.CatalogModelException;
 
57
import org.netbeans.modules.xml.retriever.catalog.CatalogWriteModel;
 
58
import org.netbeans.modules.xml.retriever.catalog.CatalogWriteModelFactory;
 
59
import org.netbeans.modules.xml.retriever.catalog.CatalogEntry;
 
60
import org.netbeans.modules.xml.catalogsupport.DefaultProjectCatalogSupport;
 
61
import org.netbeans.modules.xml.catalogsupport.ProjectConstants;
 
62
 
 
63
/**
 
64
 * Utility Class to provide project reference support.
 
65
 * @author Ajit
 
66
 */
 
67
public class ProjectReferenceUtility {
 
68
    
 
69
    /** Creates a new instance of ProjectReferenceUtility */
 
70
    private ProjectReferenceUtility() {
 
71
    }
 
72
    
 
73
    /**
 
74
     * This api adds a project reference to another project.
 
75
     * The api looks up AntArtifactProvider in referenced project's lookup,
 
76
     * and find all the build artifacts to which reference is to be created,
 
77
     * in the referencing project.
 
78
     * @param refHelper The Reference Helper of the referencing project.
 
79
     * @param refProject The referenced project.
 
80
     * @see org.netbeans.spi.project.support.ant.ReferenceHelper
 
81
     * @see org.netbeans.api.project.Project#getLookup
 
82
     * @see org.netbeans.spi.project.ant.AntArtifactProvider
 
83
     */
 
84
    public static void addProjectReference(ReferenceHelper refHelper, Project refProject) {
 
85
        AntArtifactProvider prov = (AntArtifactProvider)refProject.
 
86
                getLookup().lookup(AntArtifactProvider.class);
 
87
        if(prov!=null) {
 
88
            AntArtifact[] antArtifacts = prov.getBuildArtifacts();
 
89
            for(AntArtifact artifact:antArtifacts) {
 
90
                if(artifact.getType().equals(ProjectConstants.ARTIFACT_TYPE_WAR)
 
91
                || artifact.getType().equals(ProjectConstants.ARTIFACT_TYPE_JAR)){
 
92
                    for(URI uri:artifact.getArtifactLocations()) {
 
93
                        refHelper.addReference(artifact,uri);
 
94
                    }
 
95
                }
 
96
            }
 
97
        }
 
98
    }
 
99
 
 
100
    /**
 
101
     * This api removes a project reference from another project.
 
102
     * The api looks up AntArtifactProvider in referenced project's lookup,
 
103
     * and removes all the build artifacts's references from the referencing project.
 
104
     * @param refHelper The Reference Helper of the referencing project.
 
105
     * @param refProject The referenced project.
 
106
     * @see org.netbeans.spi.project.support.ant.ReferenceHelper
 
107
     * @see org.netbeans.api.project.Project#getLookup
 
108
     * @see org.netbeans.spi.project.ant.AntArtifactProvider
 
109
     */
 
110
    public static void removeProjectReference(ReferenceHelper refHelper, Project refProject) {
 
111
        AntArtifactProvider prov = (AntArtifactProvider)refProject.
 
112
                getLookup().lookup(AntArtifactProvider.class);
 
113
        if(prov!=null) {
 
114
            ProjectInformation pInfo = ProjectUtils.getInformation(refProject);
 
115
            String refPrefix = "${reference."+PropertyUtils.
 
116
                    getUsablePropertyName(pInfo.getName()).replace('.', '_')+".";
 
117
            AntArtifact[] antArtifacts = prov.getBuildArtifacts();
 
118
            for(AntArtifact artifact:antArtifacts) {
 
119
                refHelper.destroyReference(refPrefix+PropertyUtils.
 
120
                        getUsablePropertyName(artifact.getID()).replace('.', '_')+"}");
 
121
            }
 
122
        }
 
123
    }
 
124
    
 
125
    /**
 
126
     * This api checks if  a project is referenced in the catalog of another project.
 
127
     * The api looks up the catalog entries in target project and returns true,
 
128
     * if any of the entry points to the refProject
 
129
     * @param targetProject The target project which may have references,
 
130
     *        to refProject, in its catalog.
 
131
     * @param refProject The referenced project.
 
132
     */
 
133
    public static boolean hasProjectReferenceInCatalog(Project targetProject, Project refProject) {
 
134
        FileObject projectDirectory = targetProject.getProjectDirectory();
 
135
        DefaultProjectCatalogSupport catalogSupport =
 
136
                DefaultProjectCatalogSupport.getInstance(projectDirectory);
 
137
        ProjectInformation pInfo = ProjectUtils.getInformation(refProject);
 
138
        String refProjectName = PropertyUtils.getUsablePropertyName(
 
139
                pInfo.getName()).replace('.', '_');
 
140
        try {
 
141
            CatalogWriteModel cwm = CatalogWriteModelFactory.getInstance().
 
142
                    getCatalogWriteModelForProject(projectDirectory);
 
143
            for(CatalogEntry ce:cwm.getCatalogEntries()) {
 
144
                URI uri = new URI(ce.getTarget());
 
145
                if(catalogSupport.isProjectProtocol(uri) &&
 
146
                        refProjectName.equals(uri.getSchemeSpecificPart())) {
 
147
                    return true;
 
148
                }
 
149
            }
 
150
        } catch (URISyntaxException ex) {
 
151
        } catch (CatalogModelException ex) {
 
152
        }
 
153
        return false;
 
154
    }
 
155
}