001: /*
002: * Copyright 2005-2007 Noelios Consulting.
003: *
004: * The contents of this file are subject to the terms of the Common Development
005: * and Distribution License (the "License"). You may not use this file except in
006: * compliance with the License.
007: *
008: * You can obtain a copy of the license at
009: * http://www.opensource.org/licenses/cddl1.txt See the License for the specific
010: * language governing permissions and limitations under the License.
011: *
012: * When distributing Covered Code, include this CDDL HEADER in each file and
013: * include the License file at http://www.opensource.org/licenses/cddl1.txt If
014: * applicable, add the following below this CDDL HEADER, with the fields
015: * enclosed by brackets "[]" replaced with your own identifying information:
016: * Portions Copyright [yyyy] [name of copyright owner]
017: */
018:
019: package org.restlet.data;
020:
021: import java.io.BufferedReader;
022: import java.io.IOException;
023: import java.io.InputStreamReader;
024: import java.util.ArrayList;
025: import java.util.List;
026:
027: import org.restlet.resource.Representation;
028: import org.restlet.resource.StringRepresentation;
029: import org.restlet.util.WrapperList;
030:
031: /**
032: * List of URI references.
033: *
034: * @author Jerome Louvel (contact@noelios.com)
035: */
036: public class ReferenceList extends WrapperList<Reference> {
037: /** The list identifier. */
038: private Reference identifier;
039:
040: /**
041: * Constructor.
042: */
043: public ReferenceList() {
044: super ();
045: }
046:
047: /**
048: * Constructor.
049: *
050: * @param initialCapacity
051: * The initial list capacity.
052: */
053: public ReferenceList(int initialCapacity) {
054: super (new ArrayList<Reference>(initialCapacity));
055: }
056:
057: /**
058: * Constructor.
059: *
060: * @param delegate
061: * The delegate list.
062: */
063: public ReferenceList(List<Reference> delegate) {
064: super (delegate);
065: }
066:
067: /**
068: * Constructor from a "text/uri-list" representation.
069: *
070: * @param uriList
071: * The "text/uri-list" representation to parse.
072: * @throws IOException
073: */
074: public ReferenceList(Representation uriList) throws IOException {
075: BufferedReader br = null;
076: try {
077: br = new BufferedReader(new InputStreamReader(uriList
078: .getStream()));
079:
080: String line = br.readLine();
081:
082: // Checks if the list reference is specified as the first comment.
083: if ((line != null) && line.startsWith("#")) {
084: setIdentifier(new Reference(line.substring(1).trim()));
085: line = br.readLine();
086: }
087:
088: while (line != null) {
089: if (!line.startsWith("#")) {
090: add(new Reference(line.trim()));
091: }
092:
093: line = br.readLine();
094: }
095: } finally {
096: if (br != null) {
097: br.close();
098: }
099: }
100: }
101:
102: /**
103: * Creates then adds a reference at the end of the list.
104: *
105: * @param uri
106: * The uri of the reference to add.
107: * @return True (as per the general contract of the Collection.add method).
108: */
109: public boolean add(String uri) {
110: return add(new Reference(uri));
111: }
112:
113: /**
114: * Returns the list identifier.
115: *
116: * @return The list identifier.
117: */
118: public Reference getIdentifier() {
119: return this .identifier;
120: }
121:
122: /**
123: * Returns a representation of the list in the "text/uri-list" format.
124: *
125: * @return A representation of the list in the "text/uri-list" format.
126: */
127: public Representation getTextRepresentation() {
128: StringBuilder sb = new StringBuilder();
129:
130: if (getIdentifier() != null) {
131: sb.append("# ").append(getIdentifier().toString()).append(
132: "\r\n");
133: }
134:
135: for (Reference ref : this ) {
136: sb.append(ref.toString()).append("\r\n");
137: }
138:
139: return new StringRepresentation(sb.toString(),
140: MediaType.TEXT_URI_LIST);
141: }
142:
143: /**
144: * Returns a representation of the list in "text/html" format.
145: *
146: * @return A representation of the list in "text/html" format.
147: */
148: public Representation getWebRepresentation() {
149: // Create a simple HTML list
150: StringBuilder sb = new StringBuilder();
151: sb.append("<html><body>\n");
152:
153: if (getIdentifier() != null) {
154: sb.append("<h2>Listing of \"" + getIdentifier().getPath()
155: + "\"</h2>\n");
156: Reference parentRef = getIdentifier().getParentRef();
157:
158: if (!parentRef.equals(getIdentifier())) {
159: sb
160: .append("<a href=\"" + parentRef
161: + "\">..</a><br/>\n");
162: }
163: } else {
164: sb.append("<h2>List of references</h2>\n");
165: }
166:
167: for (Reference ref : this ) {
168: sb.append("<a href=\"" + ref.toString() + "\">"
169: + ref.getRelativeRef(getIdentifier())
170: + "</a><br/>\n");
171: }
172: sb.append("</body></html>\n");
173:
174: return new StringRepresentation(sb.toString(),
175: MediaType.TEXT_HTML);
176: }
177:
178: /**
179: * Sets the list reference.
180: *
181: * @param identifier
182: * The list identifier.
183: */
184: public void setIdentifier(Reference identifier) {
185: this .identifier = identifier;
186: }
187:
188: /**
189: * Sets the list reference.
190: *
191: * @param identifier
192: * The list identifier as a URI.
193: */
194: public void setIdentifier(String identifier) {
195: setIdentifier(new Reference(identifier));
196: }
197:
198: /**
199: * Returns a view of the portion of this list between the specified
200: * fromIndex, inclusive, and toIndex, exclusive.
201: *
202: * @param fromIndex
203: * The start position.
204: * @param toIndex
205: * The end position (exclusive).
206: * @return The sub-list.
207: */
208: @Override
209: public ReferenceList subList(int fromIndex, int toIndex) {
210: return new ReferenceList(getDelegate().subList(fromIndex,
211: toIndex));
212: }
213:
214: }
|