1 | package org.farng.mp3.filename; |
2 | |
3 | import org.farng.mp3.AbstractMP3Tag; |
4 | import org.farng.mp3.TagUtility; |
5 | import org.farng.mp3.id3.AbstractID3v2Frame; |
6 | import org.farng.mp3.id3.AbstractID3v2FrameBody; |
7 | import org.farng.mp3.id3.ID3v2_4; |
8 | |
9 | import java.util.Iterator; |
10 | |
11 | /** |
12 | * This class is a composite subclass which represents a delimiter within a filename. Delimiters are usually punctuation |
13 | * such as " - " |
14 | * |
15 | * @author Eric Farng |
16 | * @version $Revision: 1.5 $ |
17 | */ |
18 | public class FilenameDelimiter extends AbstractFilenameComposite { |
19 | |
20 | /** |
21 | * The composite that comes before the delimiter |
22 | */ |
23 | private AbstractFilenameComposite afterComposite = null; |
24 | /** |
25 | * The composite that comes after the delimiter |
26 | */ |
27 | private AbstractFilenameComposite beforeComposite = null; |
28 | /** |
29 | * The delimiter that was used to split the token |
30 | */ |
31 | private String delimiter = null; |
32 | |
33 | /** |
34 | * Creates a new FilenameDelimiter object. |
35 | */ |
36 | public FilenameDelimiter() { |
37 | super(); |
38 | } |
39 | |
40 | /** |
41 | * Creates a new FilenameDelimiter object. |
42 | */ |
43 | public FilenameDelimiter(final FilenameDelimiter copyObject) { |
44 | super(copyObject); |
45 | delimiter = copyObject.delimiter; |
46 | afterComposite = (AbstractFilenameComposite) TagUtility.copyObject(copyObject.afterComposite); |
47 | beforeComposite = (AbstractFilenameComposite) TagUtility.copyObject(copyObject.beforeComposite); |
48 | } |
49 | |
50 | /** |
51 | * Sets the composite that comes after the delimiter. |
52 | * |
53 | * @param afterComposite The composite that comes after the delimiter. |
54 | */ |
55 | public void setAfterComposite(final AbstractFilenameComposite afterComposite) { |
56 | this.afterComposite = afterComposite; |
57 | } |
58 | |
59 | /** |
60 | * Returns the composite that comes after the delimiter. |
61 | * |
62 | * @return the composite that comes after the delimiter. |
63 | */ |
64 | public AbstractFilenameComposite getAfterComposite() { |
65 | return afterComposite; |
66 | } |
67 | |
68 | /** |
69 | * Sets the composite that comes before the delimiter |
70 | * |
71 | * @param beforeComposite the composite that comes before the delimiter |
72 | */ |
73 | public void setBeforeComposite(final AbstractFilenameComposite beforeComposite) { |
74 | this.beforeComposite = beforeComposite; |
75 | } |
76 | |
77 | /** |
78 | * Returns the composite that comes before the delimiter |
79 | * |
80 | * @return the composite that comes before the delimiter |
81 | */ |
82 | public AbstractFilenameComposite getBeforeComposite() { |
83 | return beforeComposite; |
84 | } |
85 | |
86 | /** |
87 | * Sets the delimiter that splits the two halves of this composite |
88 | * |
89 | * @param delimiter delimiter that splits the two halves of this composite |
90 | */ |
91 | public void setDelimiter(final String delimiter) { |
92 | this.delimiter = delimiter; |
93 | } |
94 | |
95 | /** |
96 | * Returns the delimiter that splits the two halfs of this composite |
97 | * |
98 | * @return the delimiter that splits the two halfs of this composite |
99 | */ |
100 | public String getDelimiter() { |
101 | return delimiter; |
102 | } |
103 | |
104 | public void setFrame(final AbstractID3v2Frame frame) { |
105 | if (frame != null) { |
106 | if (beforeComposite != null) { |
107 | beforeComposite.setFrame(frame); |
108 | } |
109 | if (afterComposite != null) { |
110 | afterComposite.setFrame(frame); |
111 | } |
112 | } |
113 | } |
114 | |
115 | /** |
116 | * Create the filename that this composite represents |
117 | * |
118 | * @return the filename that this composite represents |
119 | */ |
120 | public String composeFilename() { |
121 | final StringBuffer stringBuffer = new StringBuffer(128); |
122 | if (beforeComposite != null) { |
123 | stringBuffer.append(beforeComposite.composeFilename()); |
124 | } |
125 | stringBuffer.append(delimiter); |
126 | stringBuffer.append(' '); |
127 | if (afterComposite != null) { |
128 | stringBuffer.append(afterComposite.composeFilename()); |
129 | } |
130 | return stringBuffer.toString(); |
131 | } |
132 | |
133 | public ID3v2_4 createId3Tag() { |
134 | ID3v2_4 newTag = null; |
135 | if (beforeComposite != null) { |
136 | newTag = beforeComposite.createId3Tag(); |
137 | } |
138 | if (afterComposite != null) { |
139 | if (newTag != null) { |
140 | newTag.append(afterComposite.createId3Tag()); |
141 | } else { |
142 | newTag = afterComposite.createId3Tag(); |
143 | } |
144 | } |
145 | return newTag; |
146 | } |
147 | |
148 | /** |
149 | * Returns an iterator through each <code>FilenameToken</code> in this composite in the correct order for the file |
150 | * name. |
151 | * |
152 | * @return an iterator through each <code>FilenameToken</code> in this composite |
153 | */ |
154 | public Iterator iterator() { |
155 | return new FilenameDelimiterIterator(this); |
156 | } |
157 | |
158 | /** |
159 | * Match all elements of this composite against the keywords for this class type found in |
160 | * <code>TagOptionSingleton</code>. If the <code>FilenameToken</code> matches the keyword, the token's class is |
161 | * set. |
162 | * |
163 | * @param id3v2FrameBodyClass Class of keywords to match against. |
164 | */ |
165 | public void matchAgainstKeyword(final Class id3v2FrameBodyClass) { |
166 | if (AbstractID3v2FrameBody.class.isAssignableFrom(id3v2FrameBodyClass)) { |
167 | if (beforeComposite != null) { |
168 | beforeComposite.matchAgainstKeyword(id3v2FrameBodyClass); |
169 | } |
170 | if (afterComposite != null) { |
171 | afterComposite.matchAgainstKeyword(id3v2FrameBodyClass); |
172 | } |
173 | } |
174 | } |
175 | |
176 | /** |
177 | * Match all elements of this composite against the given tag. If any element of <code>matchTag</code> matches any |
178 | * element of this tag's composite, then this tag's composite leaf node's class is set. |
179 | * |
180 | * @param matchTag Tag to match against |
181 | */ |
182 | public void matchAgainstTag(final AbstractMP3Tag matchTag) { |
183 | if (matchTag != null) { |
184 | if (beforeComposite != null) { |
185 | beforeComposite.matchAgainstTag(matchTag); |
186 | } |
187 | if (afterComposite != null) { |
188 | afterComposite.matchAgainstTag(matchTag); |
189 | } |
190 | } |
191 | } |
192 | } |