Debian Codegraph @

src/com/adobe/internal/xmp/XMPPathFactory.java

raw symbols SCIP
1 // =================================================================================================
2 // ADOBE SYSTEMS INCORPORATED
3 // Copyright 2006 Adobe Systems Incorporated
4 // All Rights Reserved
5 //
6 // NOTICE: Adobe permits you to use, modify, and distribute this file in accordance with the terms
7 // of the Adobe license agreement accompanying it.
8 // =================================================================================================
9
10 package com.adobe.internal.xmp;
11
12 import com.adobe.internal.xmp.impl.Utils;
13 import com.adobe.internal.xmp.impl.xpath.XMPPath;
14 import com.adobe.internal.xmp.impl.xpath.XMPPathParser;
15
16 /**
17 * Utility services for the metadata object. It has only public static functions, you cannot create
18 * an object. These are all functions that layer cleanly on top of the core XMP toolkit.
19 * <p>
20 * These functions provide support for composing path expressions to deeply nested properties. The
21 * functions <code>XMPMeta</code> such as <code>getProperty()</code>,
22 * <code>getArrayItem()</code> and <code>getStructField()</code> provide easy access to top
23 * level simple properties, items in top level arrays, and fields of top level structs. They do not
24 * provide convenient access to more complex things like fields several levels deep in a complex
25 * struct, or fields within an array of structs, or items of an array that is a field of a struct.
26 * These functions can also be used to compose paths to top level array items or struct fields so
27 * that you can use the binary accessors like <code>getPropertyAsInteger()</code>.
28 * <p>
29 * You can use these functions is to compose a complete path expression, or all but the last
30 * component. Suppose you have a property that is an array of integers within a struct. You can
31 * access one of the array items like this:
32 *
33 * <blockquote>
34 *
35 * <pre>
36 * String path = XMPPathFactory.composeStructFieldPath (schemaNS, &quot;Struct&quot;, fieldNS,
37 * &quot;Array&quot;);
38 * String path += XMPPathFactory.composeArrayItemPath (schemaNS, &quot;Array&quot; index);
39 * PropertyInteger result = xmpObj.getPropertyAsInteger(schemaNS, path);
40 * </pre>
41 *
42 * </blockquote> You could also use this code if you want the string form of the integer:
43 * <blockquote>
44 *
45 * <pre>
46 * String path = XMPPathFactory.composeStructFieldPath (schemaNS, &quot;Struct&quot;, fieldNS,
47 * &quot;Array&quot;);
48 * PropertyText xmpObj.getArrayItem (schemaNS, path, index);
49 * </pre>
50 *
51 * </blockquote>
52 * <p>
53 * <em>Note:</em> It might look confusing that the schemaNS is passed in all of the calls above.
54 * This is because the XMP toolkit keeps the top level &quot;schema&quot; namespace separate from
55 * the rest of the path expression.
56 * <em>Note:</em> These methods are much simpler than in the C++-API, they don't check the given
57 * path or array indices.
58 *
59 * @author Stefan Makswit
60 * @version $Revision$
61 * @since 25.01.2006
62 */
63 public final class XMPPathFactory
64 {
65 /** Private constructor */
66 private XMPPathFactory()
67 {
68 // EMPTY
69 }
70
71
72 /**
73 * Compose the path expression for an item in an array.
74 *
75 * @param arrayName The name of the array. May be a general path expression, must not be
76 * <code>null</code> or the empty string.
77 * @param itemIndex The index of the desired item. Arrays in XMP are indexed from 1.
78 * 0 and below means last array item and renders as <code>[last()]</code>.
79 *
80 * @return Returns the composed path basing on fullPath. This will be of the form
81 * <tt>ns:arrayName[i]</tt>, where &quot;ns&quot; is the prefix for schemaNS and
82 * &quot;i&quot; is the decimal representation of itemIndex.
83 * @throws XMPException Throws exeption if index zero is used.
84 */
85 public static String composeArrayItemPath(String arrayName, int itemIndex) throws XMPException
86 {
87 if (itemIndex > 0)
88 {
89 return arrayName + '[' + itemIndex + ']';
90 }
91 else if (itemIndex == XMPConst.ARRAY_LAST_ITEM)
92 {
93 return arrayName + "[last()]";
94 }
95 else
96 {
97 throw new XMPException("Array index must be larger than zero", XMPError.BADINDEX);
98 }
99 }
100
101
102 /**
103 * Compose the path expression for a field in a struct. The result can be added to the
104 * path of
105 *
106 *
107 * @param fieldNS The namespace URI for the field. Must not be <code>null</code> or the empty
108 * string.
109 * @param fieldName The name of the field. Must be a simple XML name, must not be
110 * <code>null</code> or the empty string.
111 * @return Returns the composed path. This will be of the form
112 * <tt>ns:structName/fNS:fieldName</tt>, where &quot;ns&quot; is the prefix for
113 * schemaNS and &quot;fNS&quot; is the prefix for fieldNS.
114 * @throws XMPException Thrown if the path to create is not valid.
115 */
116 public static String composeStructFieldPath(String fieldNS,
117 String fieldName) throws XMPException
118 {
119 assertFieldNS(fieldNS);
120 assertFieldName(fieldName);
121
122 XMPPath fieldPath = XMPPathParser.expandXPath(fieldNS, fieldName);
123 if (fieldPath.size() != 2)
124 {
125 throw new XMPException("The field name must be simple", XMPError.BADXPATH);
126 }
127
128 return '/' + fieldPath.getSegment(XMPPath.STEP_ROOT_PROP).getName();
129 }
130
131
132 /**
133 * Compose the path expression for a qualifier.
134 *
135 * @param qualNS The namespace URI for the qualifier. May be <code>null</code> or the empty
136 * string if the qualifier is in the XML empty namespace.
137 * @param qualName The name of the qualifier. Must be a simple XML name, must not be
138 * <code>null</code> or the empty string.
139 * @return Returns the composed path. This will be of the form
140 * <tt>ns:propName/?qNS:qualName</tt>, where &quot;ns&quot; is the prefix for
141 * schemaNS and &quot;qNS&quot; is the prefix for qualNS.
142 * @throws XMPException Thrown if the path to create is not valid.
143 */
144 public static String composeQualifierPath(
145 String qualNS,
146 String qualName) throws XMPException
147 {
148 assertQualNS(qualNS);
149 assertQualName(qualName);
150
151 XMPPath qualPath = XMPPathParser.expandXPath(qualNS, qualName);
152 if (qualPath.size() != 2)
153 {
154 throw new XMPException("The qualifier name must be simple", XMPError.BADXPATH);
155 }
156
157 return "/?" + qualPath.getSegment(XMPPath.STEP_ROOT_PROP).getName();
158 }
159
160
161 /**
162 * Compose the path expression to select an alternate item by language. The
163 * path syntax allows two forms of &quot;content addressing&quot; that may
164 * be used to select an item in an array of alternatives. The form used in
165 * ComposeLangSelector lets you select an item in an alt-text array based on
166 * the value of its <tt>xml:lang</tt> qualifier. The other form of content
167 * addressing is shown in ComposeFieldSelector. \note ComposeLangSelector
168 * does not supplant SetLocalizedText or GetLocalizedText. They should
169 * generally be used, as they provide extra logic to choose the appropriate
170 * language and maintain consistency with the 'x-default' value.
171 * ComposeLangSelector gives you an path expression that is explicitly and
172 * only for the language given in the langName parameter.
173 *
174 * @param arrayName
175 * The name of the array. May be a general path expression, must
176 * not be <code>null</code> or the empty string.
177 * @param langName
178 * The RFC 3066 code for the desired language.
179 * @return Returns the composed path. This will be of the form
180 * <tt>ns:arrayName[@xml:lang='langName']</tt>, where
181 * &quot;ns&quot; is the prefix for schemaNS.
182 */
183 public static String composeLangSelector(String arrayName,
184 String langName)
185 {
186 return arrayName + "[?xml:lang=\"" + Utils.normalizeLangValue(langName) + "\"]";
187 }
188
189
190 /**
191 * Compose the path expression to select an alternate item by a field's value. The path syntax
192 * allows two forms of &quot;content addressing&quot; that may be used to select an item in an
193 * array of alternatives. The form used in ComposeFieldSelector lets you select an item in an
194 * array of structs based on the value of one of the fields in the structs. The other form of
195 * content addressing is shown in ComposeLangSelector. For example, consider a simple struct
196 * that has two fields, the name of a city and the URI of an FTP site in that city. Use this to
197 * create an array of download alternatives. You can show the user a popup built from the values
198 * of the city fields. You can then get the corresponding URI as follows:
199 *
200 * <blockquote>
201 *
202 * <pre>
203 * String path = composeFieldSelector ( schemaNS, &quot;Downloads&quot;, fieldNS,
204 * &quot;City&quot;, chosenCity );
205 * XMPProperty prop = xmpObj.getStructField ( schemaNS, path, fieldNS, &quot;URI&quot; );
206 * </pre>
207 *
208 * </blockquote>
209 *
210 * @param arrayName The name of the array. May be a general path expression, must not be
211 * <code>null</code> or the empty string.
212 * @param fieldNS The namespace URI for the field used as the selector. Must not be
213 * <code>null</code> or the empty string.
214 * @param fieldName The name of the field used as the selector. Must be a simple XML name, must
215 * not be <code>null</code> or the empty string. It must be the name of a field that is
216 * itself simple.
217 * @param fieldValue The desired value of the field.
218 * @return Returns the composed path. This will be of the form
219 * <tt>ns:arrayName[fNS:fieldName='fieldValue']</tt>, where &quot;ns&quot; is the
220 * prefix for schemaNS and &quot;fNS&quot; is the prefix for fieldNS.
221 * @throws XMPException Thrown if the path to create is not valid.
222 */
223 public static String composeFieldSelector(String arrayName, String fieldNS,
224 String fieldName, String fieldValue) throws XMPException
225 {
226 XMPPath fieldPath = XMPPathParser.expandXPath(fieldNS, fieldName);
227 if (fieldPath.size() != 2)
228 {
229 throw new XMPException("The fieldName name must be simple", XMPError.BADXPATH);
230 }
231
232 return arrayName + '[' + fieldPath.getSegment(XMPPath.STEP_ROOT_PROP).getName() +
233 "=\"" + fieldValue + "\"]";
234 }
235
236
237 /**
238 * ParameterAsserts that a qualifier namespace is set.
239 * @param qualNS a qualifier namespace
240 * @throws XMPException Qualifier schema is null or empty
241 */
242 private static void assertQualNS(String qualNS) throws XMPException
243 {
244 if (qualNS == null || qualNS.length() == 0)
245 {
246 throw new XMPException("Empty qualifier namespace URI", XMPError.BADSCHEMA);
247 }
248
249 }
250
251
252 /**
253 * ParameterAsserts that a qualifier name is set.
254 * @param qualName a qualifier name or path
255 * @throws XMPException Qualifier name is null or empty
256 */
257 private static void assertQualName(String qualName) throws XMPException
258 {
259 if (qualName == null || qualName.length() == 0)
260 {
261 throw new XMPException("Empty qualifier name", XMPError.BADXPATH);
262 }
263 }
264
265
266 /**
267 * ParameterAsserts that a struct field namespace is set.
268 * @param fieldNS a struct field namespace
269 * @throws XMPException Struct field schema is null or empty
270 */
271 private static void assertFieldNS(String fieldNS) throws XMPException
272 {
273 if (fieldNS == null || fieldNS.length() == 0)
274 {
275 throw new XMPException("Empty field namespace URI", XMPError.BADSCHEMA);
276 }
277
278 }
279
280
281 /**
282 * ParameterAsserts that a struct field name is set.
283 * @param fieldName a struct field name or path
284 * @throws XMPException Struct field name is null or empty
285 */
286 private static void assertFieldName(String fieldName) throws XMPException
287 {
288 if (fieldName == null || fieldName.length() == 0)
289 {
290 throw new XMPException("Empty f name", XMPError.BADXPATH);
291 }
292 }
293 }