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
10package com.adobe.xmp.impl.xpath;
11
12import java.util.ArrayList;
13import java.util.List;
14
15
16/**
17 * Representates an XMP XMPPath with segment accessor methods.
18 *
19 * @since   28.02.2006
20 */
21public class XMPPath
22{
23	// Bits for XPathStepInfo options.
24
25	/** Marks a struct field step , also for top level nodes (schema "fields"). */
26	public static final int STRUCT_FIELD_STEP = 0x01;
27	/** Marks a qualifier step.
28	 *  Note: Order is significant to separate struct/qual from array kinds! */
29	public static final int QUALIFIER_STEP = 0x02; 		//
30	/** Marks an array index step */
31	public static final int ARRAY_INDEX_STEP = 0x03;
32	/** */
33	public static final int ARRAY_LAST_STEP = 0x04;
34	/** */
35	public static final int QUAL_SELECTOR_STEP = 0x05;
36	/** */
37	public static final int FIELD_SELECTOR_STEP = 0x06;
38	/** */
39	public static final int SCHEMA_NODE = 0x80000000;
40	/** */
41	public static final int STEP_SCHEMA = 0;
42	/** */
43	public static final int STEP_ROOT_PROP = 1;
44
45
46	/** stores the segments of an XMPPath */
47	private List segments = new ArrayList(5);
48
49
50	/**
51	 * Append a path segment
52	 *
53	 * @param segment the segment to add
54	 */
55	public void add(XMPPathSegment segment)
56	{
57		segments.add(segment);
58	}
59
60
61	/**
62	 * @param index the index of the segment to return
63	 * @return Returns a path segment.
64	 */
65	public XMPPathSegment getSegment(int index)
66	{
67		return (XMPPathSegment) segments.get(index);
68	}
69
70
71	/**
72	 * @return Returns the size of the xmp path.
73	 */
74	public int size()
75	{
76		return segments.size();
77	}
78
79
80	/**
81	 * Serializes the normalized XMP-path.
82	 * @see Object#toString()
83	 */
84	public String toString()
85	{
86		StringBuffer result = new StringBuffer();
87		int index = 1;
88		while (index < size())
89		{
90			result.append(getSegment(index));
91			if (index < size() - 1)
92			{
93				int kind = getSegment(index + 1).getKind();
94				if (kind == STRUCT_FIELD_STEP  ||
95					kind == QUALIFIER_STEP)
96				{
97					// all but last and array indices
98					result.append('/');
99				}
100			}
101			index++;
102		}
103
104		return result.toString();
105	}
106}