1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  package org.apache.felix.obrplugin;
20  
21  
22  import java.io.File;
23  import java.net.URI;
24  import java.util.Iterator;
25  import java.util.regex.Pattern;
26  
27  import org.apache.maven.artifact.Artifact;
28  import org.apache.maven.artifact.repository.ArtifactRepository;
29  import org.apache.maven.model.Resource;
30  import org.apache.maven.project.MavenProject;
31  
32  
33  
34  
35  
36  
37  
38  public class ObrUtils
39  {
40      private static final String DOT_XML = ".xml";
41      private static final String REPO_XML = "repository.xml";
42      private static final String OBR_XML = "obr.xml";
43  
44  
45      
46  
47  
48  
49  
50      public static URI findRepositoryXml( String mavenRepository, String obrRepository )
51      {
52          String targetPath = obrRepository;
53  
54          Pattern ignoredNames = Pattern.compile( "^(true|false|none|null)?$", Pattern.CASE_INSENSITIVE );
55  
56          
57          if ( null == targetPath || ignoredNames.matcher( targetPath ).matches() )
58          {
59              targetPath = mavenRepository + '/' + REPO_XML;
60          }
61          else if ( !targetPath.toLowerCase().endsWith( DOT_XML ) )
62          {
63              targetPath = targetPath + '/' + REPO_XML;
64          }
65  
66          URI uri;
67          try
68          {
69              uri = new URI( targetPath );
70              uri.toURL(); 
71          }
72          catch ( Exception e )
73          {
74              uri = null;
75          }
76  
77          
78          if ( null == uri || !uri.isAbsolute() )
79          {
80              uri = new File( targetPath ).toURI();
81          }
82  
83          return uri;
84      }
85  
86  
87      
88  
89  
90  
91      public static URI findObrXml( MavenProject project )
92      {
93          File obrFile = new File( project.getBuild().getOutputDirectory(), OBR_XML );
94          if ( obrFile.exists() )
95          {
96              return obrFile.toURI();
97          }
98          for ( Iterator i = project.getResources().iterator(); i.hasNext(); )
99          {
100             Resource resource = ( Resource ) i.next();
101             obrFile = new File( resource.getDirectory(), OBR_XML );
102             if ( obrFile.exists() )
103             {
104                 return obrFile.toURI();
105             }
106         }
107         return null;
108     }
109 
110 
111     
112 
113 
114 
115 
116     public static URI getArtifactURI( ArtifactRepository repository, Artifact artifact )
117     {
118         String baseDir = repository.getBasedir();
119         String artifactPath = repository.pathOf( artifact );
120 
121         return toFileURI( baseDir + '/' + artifactPath );
122     }
123 
124 
125     
126 
127 
128 
129     public static URI toFileURI( String path )
130     {
131         if ( null == path )
132         {
133             return null;
134         }
135         else if ( path.startsWith( "file:" ) )
136         {
137             return URI.create( path );
138         }
139         else
140         {
141             return new File( path ).toURI();
142         }
143     }
144 
145 
146     
147 
148 
149 
150 
151     public static URI getRelativeURI( URI repositoryXml, URI bundleJar )
152     {
153         try
154         {
155             String repositoryPath = repositoryXml.getPath();
156             if ( repositoryPath.toLowerCase().endsWith( DOT_XML ) )
157             {
158                 
159                 int dirnameIndex = repositoryPath.lastIndexOf( '/' );
160                 repositoryPath = repositoryPath.substring( 0, dirnameIndex );
161             }
162 
163             URI rootURI = new URI( null, repositoryPath, null );
164             URI localURI = new URI( null, bundleJar.getPath(), null );
165 
166             return rootURI.relativize( localURI );
167         }
168         catch ( Exception e )
169         {
170             return bundleJar;
171         }
172     }
173 }