@@ -8,11 +8,13 @@ import _root_.java.io.{
8
8
PrintWriter => JPrintWriter ,
9
9
FileReader => JFileReader ,
10
10
BufferedInputStream ,
11
- FileInputStream ,
11
+ InputStream ,
12
+ InputStreamReader ,
12
13
FileOutputStream ,
13
14
BufferedOutputStream ,
14
15
FileNotFoundException
15
16
}
17
+ import _root_ .java .net .URL
16
18
import _root_ .java .util .{ Map => JMap , List => JList }
17
19
import model .{ Entity , Package }
18
20
import model .json ._
@@ -21,16 +23,16 @@ import scala.collection.JavaConverters._
21
23
22
24
class OutputWriter {
23
25
24
- def writeJava (packs : JMap [String , Package ], templatePath : String , outPath : String , resources : JList [String ]): Unit = {
25
- write(packs.asScala, templatePath, outPath , resources.asScala)
26
+ def writeJava (packs : JMap [String , Package ], outPath : String , template : URL , resources : JList [URL ]): Unit = {
27
+ write(packs.asScala, outPath, template , resources.asScala)
26
28
}
27
29
28
- def write (packs : collection.Map [String , Package ], templatePath : String , outPath : String , resources : Iterable [ String ]): Unit = {
30
+ def write (packs : collection.Map [String , Package ], outPath : String , template : URL , resources : Traversable [ URL ]): Unit = {
29
31
// Write all packages to `outPath`
30
32
for (pack <- packs.values) {
31
33
println(s """ Writing ' ${pack.path.mkString(" ." )}' """ )
32
34
writeFile(
33
- expandTemplate(templatePath , pack, outPath),
35
+ expandTemplate(template , pack, outPath),
34
36
outPath + pack.path.mkString(" /" , " /" , " /" ),
35
37
" index.html" )
36
38
@@ -41,7 +43,7 @@ class OutputWriter {
41
43
} {
42
44
println(s """ Writing ' ${child.path.mkString(" ." )}' """ )
43
45
writeFile(
44
- expandTemplate(templatePath , child, outPath),
46
+ expandTemplate(template , child, outPath),
45
47
outPath + child.path.dropRight(1 ).mkString(" /" , " /" , " /" ),
46
48
child.path.last + " .html" )
47
49
}
@@ -55,7 +57,10 @@ class OutputWriter {
55
57
// Write resources to outPath
56
58
println(" Copying CSS/JS resources to destination..." )
57
59
assert(resources.nonEmpty)
58
- resources.map(s => copy(new JFile (s), outPath))
60
+
61
+ // TODO: splitting the URL by '/' and taking the last means that we don't
62
+ // allow folders among the resources
63
+ resources.foreach(url => copy(url.openStream, outPath, url.getFile.split(" /" ).last))
59
64
60
65
println(" Done writing static material, building js-app" )
61
66
}
@@ -66,29 +71,31 @@ class OutputWriter {
66
71
def writeJson (index : collection.Map [String , Package ], outputDir : String ): Unit =
67
72
writeFile(index.json, outputDir + " /" , " index.json" )
68
73
69
- def expandTemplate (templatePath : String , entity : Entity , outPath : String ): String = try {
74
+ def expandTemplate (template : URL , entity : Entity , outPath : String ): String = try {
70
75
import model .json ._
71
76
import model .java ._
72
77
78
+ val inputStream = template.openStream
73
79
val writer = new _root_.java.io.StringWriter ()
74
80
val mf = new DefaultMustacheFactory ()
75
81
76
- def toRoot = " ../" * (entity.path.length - 1 )
82
+ def toRoot = " ../" * (entity.path.length - { if (entity. isInstanceOf [ Package ]) 0 else 1 } )
77
83
78
84
val entityWithExtras = entity.asJava(Map (
79
85
" assets" -> s " ${toRoot}docassets " ,
80
86
" index" -> s " ${toRoot}docassets/index.js " ,
81
87
" currentEntity" -> entity.json
82
88
))
83
89
84
- mf.compile(new JFileReader (templatePath ), " template" )
90
+ mf.compile(new InputStreamReader (inputStream ), " template" )
85
91
.execute(writer, entityWithExtras)
86
92
93
+ inputStream.close()
87
94
writer.flush()
88
95
writer.toString
89
96
} catch {
90
97
case fnf : FileNotFoundException =>
91
- dottydoc.println(s """ Couldn't find the template: " $templatePath "...exiting """ )
98
+ dottydoc.println(s """ Couldn't find the template: " ${template.getFile} "...exiting """ )
92
99
System .exit(1 ); " "
93
100
}
94
101
@@ -107,11 +114,12 @@ class OutputWriter {
107
114
printToFile(new JFile (path + file))(printer => bytes.foreach(printer.print))
108
115
}
109
116
110
- def copy (src : JFile , path : String ): Unit = {
111
- val reader = new BufferedInputStream (new FileInputStream ( src) )
117
+ def copy (src : InputStream , path : String , name : String ): Unit = {
118
+ val reader = new BufferedInputStream (src)
112
119
try {
113
- val bytes = Stream .continually(reader.read).takeWhile(- 1 != _).map(_.toByte)
114
- writeFile(bytes.toArray, path + " /docassets/" , src.getName)
120
+ val bytes = Stream .continually(reader.read).takeWhile(- 1 != _).map(_.toByte)
121
+ writeFile(bytes.toArray, path + " /docassets/" , name)
122
+ src.close()
115
123
} finally reader.close()
116
124
}
117
125
}
0 commit comments