01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. The ASF licenses this file to You
04: * under the Apache License, Version 2.0 (the "License"); you may not
05: * use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License. For additional information regarding
15: * copyright in this work, please see the NOTICE file in the top level
16: * directory of this distribution.
17: */
18: /*
19: * UserEntrySet.java
20: *
21: * Created on January 17, 2006, 12:44 PM
22: */
23:
24: package org.apache.roller.webservices.adminapi.sdk;
25:
26: import java.io.IOException;
27: import java.io.InputStream;
28: import java.util.ArrayList;
29: import java.util.Iterator;
30: import java.util.List;
31: import org.jdom.Document;
32: import org.jdom.Element;
33: import org.jdom.JDOMException;
34: import org.jdom.input.SAXBuilder;
35: import org.apache.roller.webservices.adminapi.sdk.EntrySet.Types;
36:
37: /**
38: * This class describes a set of user entries.
39: * @author jtb
40: */
41: public class UserEntrySet extends EntrySet {
42: /** XML tags that describe a set of user entries. */
43: private static interface Tags {
44: public static final String USERS = "users";
45: }
46:
47: /** Construct based on an array of Roller UserData objects. */
48: public UserEntrySet(String urlPrefix) {
49: setHref(urlPrefix + "/" + Types.USERS);
50: }
51:
52: /** Construct based on a JDOM Document object. */
53: public UserEntrySet(Document d, String urlPrefix)
54: throws UnexpectedRootElementException {
55: populate(d, urlPrefix);
56: }
57:
58: public UserEntrySet(InputStream stream, String urlPrefix)
59: throws JDOMException, IOException,
60: UnexpectedRootElementException {
61: SAXBuilder sb = new SAXBuilder();
62: Document d = sb.build(stream);
63:
64: populate(d, urlPrefix);
65: }
66:
67: private void populate(Document d, String urlPrefix)
68: throws UnexpectedRootElementException {
69: Element root = d.getRootElement();
70: String rootName = root.getName();
71: if (!rootName.equals(Tags.USERS)) {
72: throw new UnexpectedRootElementException(
73: "ERROR: Unexpected root element", Tags.USERS,
74: rootName);
75: }
76: List users = root.getChildren(UserEntry.Tags.USER, NAMESPACE);
77: if (users != null) {
78: List entries = new ArrayList();
79: for (Iterator i = users.iterator(); i.hasNext();) {
80: Element user = (Element) i.next();
81: UserEntry entry = new UserEntry(user, urlPrefix);
82: entries.add(entry);
83: }
84: setEntries((Entry[]) entries.toArray(new Entry[0]));
85: }
86: setHref(urlPrefix + "/" + Types.USERS);
87: }
88:
89: public String getType() {
90: return Types.USERS;
91: }
92: }
|