1 package org.apache.maven.jelly.tags.maven;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 import java.io.File;
22 import java.io.IOException;
23 import java.util.StringTokenizer;
24
25 import org.apache.commons.jelly.JellyTagException;
26 import org.apache.commons.jelly.MissingAttributeException;
27 import org.apache.commons.jelly.XMLOutput;
28 import org.apache.maven.MavenUtils;
29 import org.apache.maven.jelly.tags.BaseTagSupport;
30
31 /**
32 * Jelly tag to convert an absolute path into a relative path by removing the basedir.
33 *
34 * @author Brett Porter <a href="mailto:brett@apache.org">brett@apache.org</a>
35 */
36 public class MakeRelativePathTag
37 extends BaseTagSupport
38 {
39 /** The base dir. */
40 private File basedir;
41
42 /** The path to convert. */
43 private String path;
44
45 /** Force a certain path separator. */
46 private String separator = null;
47
48 /** The jelly variable to store the result into. */
49 private String var;
50
51 /**
52 * Set the base directory.
53 * @param basedir the base directory.
54 */
55 public void setBasedir( File basedir )
56 {
57 this.basedir = basedir;
58 }
59
60 /**
61 * Set the path. This has to be absolute under basedir.
62 * @param path the path.
63 */
64 public void setPath( String path )
65 {
66 this.path = path;
67 }
68
69 /**
70 * Set the path separator to use.
71 * @param separator the separator.
72 */
73 public void setSeparator( String separator )
74 {
75 this.separator = separator;
76 }
77
78 /**
79 * Set the result variable. Returns path if path is not absolute under basedir.
80 * @param var the result variable name.
81 */
82 public void setVar( String var )
83 {
84 this.var = var;
85 }
86
87
88
89
90 public void doTag( XMLOutput output )
91 throws MissingAttributeException, JellyTagException
92 {
93 checkAttribute( basedir, "basedir" );
94 checkAttribute( path, "path" );
95 checkAttribute( var, "var" );
96
97 try
98 {
99 String canonicalPath = MavenUtils.makeRelativePath( basedir, path );
100 if ( separator != null )
101 {
102 StringBuffer buf = new StringBuffer();
103 StringTokenizer tok = new StringTokenizer( canonicalPath, "/\\" );
104 while ( tok.hasMoreTokens() )
105 {
106 buf.append( tok.nextToken() );
107 if ( tok.hasMoreTokens() )
108 {
109 buf.append( separator );
110 }
111 }
112 canonicalPath = buf.toString();
113 }
114 getContext().setVariable( var, canonicalPath );
115 }
116 catch ( IOException e )
117 {
118 throw new JellyTagException( "Unable to resolve directory", e );
119 }
120 }
121
122 }