1 16 package org.apache.cocoon.components.source.impl; 17 18 import java.io.IOException ; 19 import java.io.InputStream ; 20 import java.io.OutputStream ; 21 import java.util.ArrayList ; 22 import java.util.Arrays ; 23 import java.util.Collection ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 27 import org.apache.avalon.framework.logger.AbstractLogEnabled; 28 import org.apache.avalon.framework.logger.Logger; 29 import org.apache.cocoon.components.source.InspectableSource; 30 import org.apache.cocoon.components.source.SourceDescriptor; 31 import org.apache.cocoon.components.source.helpers.SourceProperty; 32 import org.apache.excalibur.source.ModifiableTraversableSource; 33 import org.apache.excalibur.source.Source; 34 import org.apache.excalibur.source.SourceException; 35 import org.apache.excalibur.source.SourceNotFoundException; 36 import org.apache.excalibur.source.SourceValidity; 37 import org.apache.excalibur.source.impl.validity.AggregatedValidity; 38 39 51 public class RepositorySource extends AbstractLogEnabled 52 implements Source, ModifiableTraversableSource, InspectableSource { 53 54 final String m_prefix; 56 final ModifiableTraversableSource m_delegate; 58 private final SourceDescriptor m_descriptor; 59 60 62 public RepositorySource( 63 final String prefix, 64 final ModifiableTraversableSource delegate, 65 final SourceDescriptor descriptor, 66 final Logger logger) throws SourceException { 67 68 m_prefix = prefix; 69 m_delegate = delegate; 70 m_descriptor = descriptor; 71 enableLogging(logger); 72 } 73 74 76 83 public SourceProperty[] getSourceProperties() throws SourceException { 84 85 final List properties = new ArrayList (); 86 if (m_delegate instanceof InspectableSource) { 87 properties.addAll(Arrays.asList(((InspectableSource) m_delegate).getSourceProperties())); 88 } 89 if (m_descriptor != null) { 90 properties.addAll(Arrays.asList(m_descriptor.getSourceProperties(m_delegate))); 91 } 92 return (SourceProperty[]) properties.toArray(new SourceProperty[properties.size()]); 93 } 94 95 100 public SourceProperty getSourceProperty(String uri, String name) throws SourceException { 101 SourceProperty property = null; 102 if (m_delegate instanceof InspectableSource) { 103 property = ((InspectableSource) m_delegate).getSourceProperty(uri,name); 104 } 105 if (property == null && m_descriptor != null) { 106 property = m_descriptor.getSourceProperty(m_delegate,uri,name); 107 } 108 return property; 109 } 110 111 116 public void removeSourceProperty(String uri, String name) throws SourceException { 117 if (m_delegate instanceof InspectableSource) { 118 ((InspectableSource) m_delegate).removeSourceProperty(uri,name); 119 } 120 if (m_descriptor != null) { 121 m_descriptor.removeSourceProperty(m_delegate,uri,name); 122 } 123 } 124 125 130 public void setSourceProperty(SourceProperty property) throws SourceException { 131 if (m_delegate instanceof InspectableSource) { 132 ((InspectableSource) m_delegate).setSourceProperty(property); 133 } else if (m_descriptor != null) { 134 m_descriptor.setSourceProperty(m_delegate, property); 135 } 136 } 137 138 139 141 public boolean exists() { 142 return m_delegate.exists(); 143 } 144 145 public long getContentLength() { 146 return m_delegate.getContentLength(); 147 } 148 149 public InputStream getInputStream() 150 throws IOException , SourceNotFoundException { 151 return m_delegate.getInputStream(); 152 } 153 154 public long getLastModified() { 155 return m_delegate.getLastModified(); 156 } 157 158 public String getMimeType() { 159 return m_delegate.getMimeType(); 160 } 161 162 public String getScheme() { 163 return m_prefix; 164 } 165 166 public String getURI() { 167 return m_prefix + ":" + m_delegate.getURI(); 168 } 169 170 180 public SourceValidity getValidity() { 181 SourceValidity val1; 182 val1 = m_delegate.getValidity(); 183 if (val1 != null && m_descriptor != null) { 184 SourceValidity val2 = m_descriptor.getValidity(m_delegate); 185 if (val2 != null) { 186 AggregatedValidity result = new AggregatedValidity(); 187 result.add(val1); 188 result.add(val2); 189 return result; 190 } 191 } 192 return val1; 193 } 194 195 public void refresh() { 196 m_delegate.refresh(); 197 } 198 199 200 202 public Source getChild(String name) throws SourceException { 203 if (!m_delegate.isCollection()) return null; 204 ModifiableTraversableSource child = (ModifiableTraversableSource) m_delegate.getChild(name); 205 if (child == null) return null; 206 207 return new RepositorySource( 208 m_prefix, 209 child, 210 m_descriptor, 211 getLogger() 212 ); 213 } 214 215 public Collection getChildren() throws SourceException { 216 if (!m_delegate.isCollection()) return null; 217 Collection result = new ArrayList (); 218 Iterator iter = m_delegate.getChildren().iterator(); 219 while(iter.hasNext()) { 220 ModifiableTraversableSource child = (ModifiableTraversableSource) iter.next(); 221 222 result.add( 223 new RepositorySource( 224 m_prefix, 225 child, 226 m_descriptor, 227 getLogger() 228 ) 229 ); 230 } 231 return result; 232 } 233 234 public String getName() { 235 return m_delegate.getName(); 236 } 237 238 public Source getParent() throws SourceException { 239 return new RepositorySource( 240 m_prefix, 241 (ModifiableTraversableSource) m_delegate.getParent(), 242 m_descriptor, 243 getLogger() 244 ); 245 } 246 247 public boolean isCollection() { 248 return m_delegate.isCollection(); 249 } 250 251 public void makeCollection() throws SourceException { 252 m_delegate.makeCollection(); 253 } 254 255 public boolean canCancel(OutputStream out) { 256 return m_delegate.canCancel(out); 257 } 258 259 public void cancel(OutputStream out) throws IOException { 260 m_delegate.cancel(out); 261 } 262 263 public void delete() throws SourceException { 264 m_delegate.delete(); 265 } 266 267 public OutputStream getOutputStream() throws IOException { 268 return m_delegate.getOutputStream(); 269 } 270 271 } 272 | Popular Tags |