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;
020:
021: import java.util.ArrayList;
022: import java.util.List;
023:
024: import org.restlet.data.CharacterSet;
025: import org.restlet.data.Encoding;
026: import org.restlet.data.Language;
027: import org.restlet.data.MediaType;
028: import org.restlet.data.Request;
029: import org.restlet.data.Response;
030: import org.restlet.resource.Representation;
031: import org.restlet.resource.TransformRepresentation;
032:
033: /**
034: * Filter that can transform XML representations by applying an XSLT transform
035: * sheet. It uses the {@link org.restlet.resource.TransformRepresentation} to
036: * actually transform the XML entities.
037: *
038: * @author Jerome Louvel (contact@noelios.com) <a
039: * href="http://www.noelios.com/">Noelios Consulting</a>
040: */
041: public class Transformer extends Filter {
042: /**
043: * Mode that transforms request entities before their handling by the
044: * attached Restlet.
045: */
046: public static final int MODE_REQUEST = 1;
047:
048: /**
049: * Mode that transforms response entities after their handling by the
050: * attached Restlet.
051: */
052: public static final int MODE_RESPONSE = 2;
053:
054: /** The transformation mode. */
055: private int mode;
056:
057: /** The XSLT transform sheet to apply to message entities. */
058: private Representation transformSheet;
059:
060: /**
061: * The character set of the result representation. The default value is
062: * null.
063: */
064: private CharacterSet resultCharacterSet;
065:
066: /**
067: * The encodings of the result representation.
068: */
069: private List<Encoding> resultEncodings;
070:
071: /** The languages of the result representation. */
072: private List<Language> resultLanguages;
073:
074: /**
075: * The media type of the result representation. MediaType.APPLICATION_XML by
076: * default.
077: */
078: private MediaType resultMediaType;
079:
080: /**
081: * Constructor.
082: *
083: * @param mode
084: * The transformation mode.
085: * @param transformSheet
086: * The XSLT transform sheet to apply to message entities.
087: */
088: public Transformer(int mode, Representation transformSheet) {
089: this .mode = mode;
090: this .transformSheet = transformSheet;
091: this .resultMediaType = MediaType.APPLICATION_XML;
092: this .resultLanguages = null;
093: this .resultCharacterSet = null;
094: }
095:
096: @Override
097: protected void afterHandle(Request request, Response response) {
098: if (getMode() == MODE_RESPONSE) {
099: response.setEntity(transform(response.getEntity()));
100: }
101: }
102:
103: @Override
104: protected void beforeHandle(Request request, Response response) {
105: if (getMode() == MODE_REQUEST) {
106: request.setEntity(transform(request.getEntity()));
107: }
108: }
109:
110: /**
111: * Returns the transformation mode. See MODE_* constants.
112: *
113: * @return The transformation mode.
114: */
115: public int getMode() {
116: return this .mode;
117: }
118:
119: /**
120: * Returns the character set of the result representation. The default value
121: * is null.
122: *
123: * @return The character set of the result representation.
124: */
125: public CharacterSet getResultCharacterSet() {
126: return this .resultCharacterSet;
127: }
128:
129: /**
130: * Returns the encoding of the result representation. The default value is
131: * null.
132: *
133: * @return The encoding of the result representation.
134: */
135: public List<Encoding> getResultEncodings() {
136: if (this .resultEncodings == null)
137: this .resultEncodings = new ArrayList<Encoding>();
138: return this .resultEncodings;
139: }
140:
141: /**
142: * Returns the languages of the result representation.
143: *
144: * @return The language of the result representation.
145: */
146: public List<Language> getResultLanguages() {
147: if (this .resultLanguages == null)
148: this .resultLanguages = new ArrayList<Language>();
149: return this .resultLanguages;
150: }
151:
152: /**
153: * Returns the media type of the result representation. The default value is
154: * MediaType.APPLICATION_XML.
155: *
156: * @return The media type of the result representation.
157: */
158: public MediaType getResultMediaType() {
159: return this .resultMediaType;
160: }
161:
162: /**
163: * Returns the XSLT transform sheet to apply to message entities.
164: *
165: * @return The XSLT transform sheet to apply to message entities.
166: */
167: public Representation getTransformSheet() {
168: return this .transformSheet;
169: }
170:
171: /**
172: * Sets the transformation mode. See MODE_* constants.
173: *
174: * @param mode
175: * The transformation mode.
176: */
177: public void setMode(int mode) {
178: this .mode = mode;
179: }
180:
181: /**
182: * Sets the character set of the result representation.
183: *
184: * @param resultCharacterSet
185: * The character set of the result representation.
186: */
187: public void setResultCharacterSet(CharacterSet resultCharacterSet) {
188: this .resultCharacterSet = resultCharacterSet;
189: }
190:
191: /**
192: * Sets the media type of the result representation.
193: *
194: * @param resultMediaType
195: * The media type of the result representation.
196: */
197: public void setResultMediaType(MediaType resultMediaType) {
198: this .resultMediaType = resultMediaType;
199: }
200:
201: /**
202: * Sets the XSLT transform sheet to apply to message entities.
203: *
204: * @param transformSheet
205: * The XSLT transform sheet to apply to message entities.
206: */
207: public void setTransformSheet(Representation transformSheet) {
208: this .transformSheet = transformSheet;
209: }
210:
211: /**
212: * Transforms a source XML representation by applying an XSLT transform
213: * sheet to it.
214: *
215: * @param source
216: * The source XML representation.
217: * @return The generated result representation.
218: */
219: public Representation transform(Representation source) {
220: Representation result = new TransformRepresentation(
221: getContext(), source, getTransformSheet());
222:
223: if (this.resultLanguages != null) {
224: result.getLanguages().addAll(getResultLanguages());
225: }
226:
227: result.setCharacterSet(getResultCharacterSet());
228: if (this.resultEncodings != null) {
229: result.getEncodings().addAll(getResultEncodings());
230: }
231:
232: result.setMediaType(getResultMediaType());
233: return result;
234: }
235:
236: }
|