1:
30:
31: package ;
32:
33: import ;
34: import ;
35: import ;
36:
37:
42: public class RepositoryUtilities
43: {
44: private RepositoryUtilities()
45: {
46:
47: }
48:
49: public static ContentEntity getEntity (final Repository repository, final String[] name)
50: throws ContentIOException
51: {
52: if (name.length == 0)
53: {
54: return repository.getRoot();
55: }
56:
57: ContentLocation node = repository.getRoot();
58: for (int i = 0; i < name.length - 1; i++)
59: {
60: final String nameItem = name[i];
61: final ContentEntity entry = node.getEntry(nameItem);
62: if (entry instanceof ContentLocation == false)
63: {
64:
65: throw new ContentIOException("No such item.");
66: }
67: node = (ContentLocation) entry;
68: }
69: return node.getEntry(name[name.length - 1]);
70: }
71:
72: public static ContentItem createItem (final Repository repository, final String[] name)
73: throws ContentIOException
74: {
75: if (name.length == 0)
76: {
77: throw new IllegalArgumentException("Empty name not permitted.");
78: }
79:
80: ContentLocation node = repository.getRoot();
81: for (int i = 0; i < name.length - 1; i++)
82: {
83: final String nameItem = name[i];
84: if (node.exists(nameItem) == false)
85: {
86:
87: node = node.createLocation(nameItem);
88: }
89: else
90: {
91: final ContentEntity entry = node.getEntry(nameItem);
92: if (entry instanceof ContentLocation == false)
93: {
94:
95: throw new ContentIOException("No such item.");
96: }
97: node = (ContentLocation) entry;
98: }
99: }
100: return node.createItem(name[name.length - 1]);
101: }
102:
103: public static ContentLocation createLocation (final Repository repository, final String[] name)
104: throws ContentIOException
105: {
106: if (name.length == 0)
107: {
108: throw new IllegalArgumentException("Empty name not permitted.");
109: }
110:
111: ContentLocation node = repository.getRoot();
112: for (int i = 0; i < name.length - 1; i++)
113: {
114: final String nameItem = name[i];
115: if (node.exists(nameItem) == false)
116: {
117:
118: node = node.createLocation(nameItem);
119: }
120: else
121: {
122: final ContentEntity entry = node.getEntry(nameItem);
123: if (entry instanceof ContentLocation == false)
124: {
125:
126: throw new ContentIOException("No such item.");
127: }
128: node = (ContentLocation) entry;
129: }
130: }
131: return node.createLocation(name[name.length - 1]);
132: }
133:
134: public static String[] split (final String name, final String separator)
135: {
136: final StringTokenizer strtok = new StringTokenizer(name, separator, false);
137: final int tokenCount = strtok.countTokens();
138: final String[] retval = new String[tokenCount];
139: int i = 0;
140: while (strtok.hasMoreTokens())
141: {
142: retval[i] = strtok.nextToken();
143: i += 1;
144: }
145: return retval;
146: }
147:
148: public static String[] buildNameArray (ContentEntity entity)
149: {
150: final LinkedList collector = new LinkedList();
151: while (entity != null)
152: {
153: final ContentLocation parent = entity.getParent();
154: if (parent != null)
155: {
156:
157: collector.add(0, entity.getName());
158: }
159: entity = parent;
160: }
161: return (String[]) collector.toArray(new String[collector.size()]);
162: }
163:
164: public static String buildName (ContentEntity entity, final String separator)
165: {
166: final ArrayList collector = new ArrayList();
167: while (entity != null)
168: {
169: final ContentLocation parent = entity.getParent();
170: if (parent != null)
171: {
172:
173: collector.add(entity.getName());
174: }
175: entity = parent;
176: }
177:
178: final StringBuffer builder = new StringBuffer();
179: final int maxIdx = collector.size() - 1;
180: for (int i = maxIdx; i >= 0; i--)
181: {
182: final String s = (String) collector.get(i);
183: if (i != maxIdx)
184: {
185: builder.append(separator);
186: }
187: builder.append(s);
188: }
189: return builder.toString();
190: }
191: }