KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > environment > commandline > LinkSamplingEnvironment


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */

16 package org.apache.cocoon.environment.commandline;
17
18 import java.io.BufferedReader JavaDoc;
19 import java.io.ByteArrayInputStream JavaDoc;
20 import java.io.ByteArrayOutputStream JavaDoc;
21 import java.io.File JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.io.InputStreamReader JavaDoc;
24 import java.net.MalformedURLException JavaDoc;
25 import java.util.Collection JavaDoc;
26 import java.util.HashSet JavaDoc;
27 import java.util.Map JavaDoc;
28
29 import org.apache.avalon.framework.logger.Logger;
30 import org.apache.cocoon.Constants;
31 import org.apache.cocoon.environment.ObjectModelHelper;
32
33 /**
34  * This environment is sample the links of the resource.
35  *
36  * @author <a HREF="mailto:stefano@apache.org">Stefano Mazzocchi</a>
37  * @version CVS $Id: LinkSamplingEnvironment.java 30932 2004-07-29 17:35:38Z vgritsenko $
38  */

39
40 public class LinkSamplingEnvironment extends AbstractCommandLineEnvironment {
41
42     private boolean skip = false;
43
44     public LinkSamplingEnvironment(String JavaDoc uri,
45                                    File JavaDoc contextFile,
46                                    Map JavaDoc attributes,
47                                    Map JavaDoc parameters,
48                                    CommandLineContext cliContext,
49                                    Logger log)
50     throws MalformedURLException JavaDoc, IOException JavaDoc {
51         super(uri, Constants.LINK_VIEW, contextFile, new ByteArrayOutputStream JavaDoc(), log);
52         if (getLogger().isDebugEnabled()) {
53             getLogger().debug("uri = " + uri);
54         }
55         this.objectModel.put(ObjectModelHelper.REQUEST_OBJECT,
56                              new CommandLineRequest(this, null, uri, null, attributes, parameters));
57         this.objectModel.put(ObjectModelHelper.RESPONSE_OBJECT,
58                              new CommandLineResponse());
59         this.objectModel.put(ObjectModelHelper.CONTEXT_OBJECT,
60                              cliContext);
61     }
62
63     /**
64      * Set the ContentType
65      */

66     public void setContentType(String JavaDoc contentType) {
67         if (!Constants.LINK_CONTENT_TYPE.equals(contentType)) {
68             this.skip = true;
69         }
70     }
71
72     /**
73      * Indicates if other links are present.
74      */

75     public Collection JavaDoc getLinks() throws IOException JavaDoc {
76         HashSet JavaDoc set = new HashSet JavaDoc();
77         if (!skip) {
78             BufferedReader JavaDoc buffer = null;
79             try {
80                 buffer = new BufferedReader JavaDoc(
81                         new InputStreamReader JavaDoc(
82                                 new ByteArrayInputStream JavaDoc(
83                                         ((ByteArrayOutputStream JavaDoc) super.outputStream).toByteArray())));
84
85                 String JavaDoc line;
86                 while ((line = buffer.readLine()) !=null) {
87                     set.add(line);
88                 }
89             } finally {
90                 // explictly close the input
91
if (buffer != null) {
92                     try {
93                         buffer.close();
94                         buffer = null;
95                     } catch (IOException JavaDoc ignored) {
96                     }
97                 }
98             }
99         }
100         return set;
101     }
102 }
103
Popular Tags