KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > store > SegmentedProductPathFinder


1 package com.openedit.store;
2
3 /**
4  * An implementation of {@link ProductPathFinder} that places products in
5  * subdirectories based on segments of their ID. For example, a product with ID
6  * <tt>abcdefghij</tt> might become <tt>abc/def/abcdefghij</tt>, if
7  * the segment length is 3 and the maximum number of segments is 2.
8  *
9  * @author Eric Galluzzo
10  */

11 public class SegmentedProductPathFinder implements ProductPathFinder
12 {
13     protected int fSegmentLength = 0;
14     protected String JavaDoc fieldPrefix;
15     protected boolean fieldReverse;
16     public boolean isReverse()
17     {
18         return fieldReverse;
19     }
20
21     public void setReverse(boolean inReverse)
22     {
23         fieldReverse = inReverse;
24     }
25
26     /**
27      * Creates a segmented product path finder that will never use
28      * subdirectories.
29      */

30     public SegmentedProductPathFinder()
31     {
32     }
33
34     /**
35      * Creates a segmented product path finder that will create segments of the
36      * given length up to the given maximum.
37      *
38      * @param inSegmentLength
39      * The length of each path segment (e.g. 3 for
40      * <tt>abc/def/...</tt>)
41      * @param inMaxSegments
42      * The maximum number of segments
43      */

44     public SegmentedProductPathFinder( int inSegmentLength )
45     {
46         fSegmentLength = inSegmentLength;
47     }
48
49     public int getSegmentLength()
50     {
51         return fSegmentLength;
52     }
53
54     public void setSegmentLength( int inSegmentLength )
55     {
56         fSegmentLength = inSegmentLength;
57     }
58
59     public String JavaDoc idToPath( String JavaDoc inProductId )
60     {
61         if ( inProductId == null )
62         {
63             return null;
64         }
65         
66         if( getSegmentLength() == 0 && getPrefix() == null)
67         {
68             return inProductId;
69         }
70         
71         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
72         if( getPrefix() != null)
73         {
74             sb.append(getPrefix());
75         }
76         if( getSegmentLength() > 0)
77         {
78             if( isReverse())
79             {
80                 sb.append( inProductId.substring(inProductId.length() - getSegmentLength() ) );
81             }
82             else
83             {
84                 sb.append( inProductId.substring(0,getSegmentLength() ) );
85             }
86             sb.append( "/" );
87         }
88         
89 // for ( int segmentCount = 0; segmentCount < getMaxSegments(); segmentCount++ )
90
// {
91
// int index = segmentCount * getSegmentLength();
92
// if ( index + getSegmentLength() >= inProductId.length() )
93
// {
94
// break;
95
// }
96
// sb.append( inProductId
97
// .substring( index, index + getSegmentLength() ) );
98
// sb.append( "/" );
99
// }
100

101         sb.append( inProductId );
102
103         return sb.toString();
104     }
105
106     public String JavaDoc getPrefix()
107     {
108         return fieldPrefix;
109     }
110
111     public void setPrefix(String JavaDoc inPrefix)
112     {
113         fieldPrefix = inPrefix;
114     }
115 }
116
Popular Tags