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