1 16 package org.apache.cocoon.environment.commandline; 17 18 import java.io.BufferedReader ; 19 import java.io.ByteArrayInputStream ; 20 import java.io.ByteArrayOutputStream ; 21 import java.io.File ; 22 import java.io.IOException ; 23 import java.io.InputStreamReader ; 24 import java.net.MalformedURLException ; 25 import java.util.Collection ; 26 import java.util.HashSet ; 27 import java.util.Map ; 28 29 import org.apache.avalon.framework.logger.Logger; 30 import org.apache.cocoon.Constants; 31 import org.apache.cocoon.environment.ObjectModelHelper; 32 33 39 40 public class LinkSamplingEnvironment extends AbstractCommandLineEnvironment { 41 42 private boolean skip = false; 43 44 public LinkSamplingEnvironment(String uri, 45 File contextFile, 46 Map attributes, 47 Map parameters, 48 CommandLineContext cliContext, 49 Logger log) 50 throws MalformedURLException , IOException { 51 super(uri, Constants.LINK_VIEW, contextFile, new ByteArrayOutputStream (), 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 66 public void setContentType(String contentType) { 67 if (!Constants.LINK_CONTENT_TYPE.equals(contentType)) { 68 this.skip = true; 69 } 70 } 71 72 75 public Collection getLinks() throws IOException { 76 HashSet set = new HashSet (); 77 if (!skip) { 78 BufferedReader buffer = null; 79 try { 80 buffer = new BufferedReader ( 81 new InputStreamReader ( 82 new ByteArrayInputStream ( 83 ((ByteArrayOutputStream ) super.outputStream).toByteArray()))); 84 85 String line; 86 while ((line = buffer.readLine()) !=null) { 87 set.add(line); 88 } 89 } finally { 90 if (buffer != null) { 92 try { 93 buffer.close(); 94 buffer = null; 95 } catch (IOException ignored) { 96 } 97 } 98 } 99 } 100 return set; 101 } 102 } 103 | Popular Tags |