1 package org.apache.maven.doxia.siterenderer;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.ByteArrayInputStream;
23 import java.io.ByteArrayOutputStream;
24 import java.io.IOException;
25 import java.io.InputStream;
26
27 import org.apache.velocity.runtime.resource.Resource;
28 import org.apache.velocity.runtime.resource.loader.ResourceLoader;
29 import org.apache.velocity.exception.ResourceNotFoundException;
30 import org.apache.commons.collections.ExtendedProperties;
31 import org.codehaus.plexus.util.IOUtil;
32
33
34
35
36
37
38
39 public class SkinResourceLoader
40 extends ResourceLoader
41 {
42 public void init( ExtendedProperties configuration )
43 {
44 }
45
46 public synchronized InputStream getResourceStream( String name )
47 throws ResourceNotFoundException
48 {
49 ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
50
51 if ( name.startsWith( "/" ) )
52 {
53 name = name.substring( 1 );
54 }
55
56 return normalizeNewline( classLoader.getResourceAsStream( name ) );
57 }
58
59 InputStream normalizeNewline( InputStream in )
60 throws ResourceNotFoundException
61 {
62 if ( in == null )
63 {
64 return null;
65 }
66
67 try
68 {
69 byte[] content = IOUtil.toByteArray( in );
70
71
72
73 byte[] eol = System.getProperty( "line.separator" ).getBytes();
74
75 final int size = content.length;
76
77 ByteArrayOutputStream out = new ByteArrayOutputStream( size );
78
79 for ( int i = 0; i < size; i++ )
80 {
81 byte b = content[i];
82
83 if ( b == '\r' )
84 {
85 if ( ( i + 1 ) < size && content[i + 1] == '\n' )
86 {
87 i++;
88 }
89
90 out.write( eol );
91 }
92 else if ( b == '\n' )
93 {
94 out.write( eol );
95 }
96 else
97 {
98 out.write( b );
99 }
100 }
101
102 return new ByteArrayInputStream( out.toByteArray() );
103 }
104 catch ( IOException ioe )
105 {
106 throw new ResourceNotFoundException( "cannot read resource", ioe );
107 }
108 finally
109 {
110 IOUtil.close( in );
111 }
112 }
113
114 public boolean isSourceModified( Resource resource )
115 {
116 return false;
117 }
118
119 public long getLastModified( Resource resource )
120 {
121 return 0;
122 }
123 }