1 package org.apache.maven.plugin.war.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import com.thoughtworks.xstream.XStream;
23 import com.thoughtworks.xstream.io.xml.DomDriver;
24 import org.apache.maven.model.Dependency;
25 import org.codehaus.plexus.util.IOUtil;
26 import org.codehaus.plexus.util.ReaderFactory;
27 import org.codehaus.plexus.util.WriterFactory;
28
29 import java.io.File;
30 import java.io.IOException;
31 import java.io.Reader;
32 import java.io.Writer;
33
34
35
36
37
38
39
40 public class WebappStructureSerializer
41 {
42
43 private static final XStream XSTREAM;
44
45 static
46 {
47 XSTREAM = new XStream( new DomDriver() );
48
49
50 XSTREAM.alias( "webapp-structure", WebappStructure.class );
51 XSTREAM.alias( "path-set", PathSet.class );
52 XSTREAM.alias( "dependency", Dependency.class );
53
54 }
55
56
57
58
59 public WebappStructureSerializer()
60 {
61 }
62
63
64
65
66
67
68
69
70 public WebappStructure fromXml( File file )
71 throws IOException
72 {
73 Reader reader = null;
74
75 try
76 {
77 reader = ReaderFactory.newXmlReader( file );
78 return (WebappStructure) XSTREAM.fromXML( reader );
79 }
80 finally
81 {
82 IOUtil.close( reader );
83 }
84 }
85
86
87
88
89
90
91
92
93 public void toXml( WebappStructure webappStructure, File targetFile )
94 throws IOException
95 {
96
97 Writer writer = null;
98 try
99 {
100 if ( !targetFile.getParentFile().exists() && !targetFile.getParentFile().mkdirs() )
101 {
102 throw new IOException( "Could not create parent [" + targetFile.getParentFile().getAbsolutePath() + "]" );
103 }
104
105 if ( !targetFile.exists() && !targetFile.createNewFile() )
106 {
107 throw new IOException( "Could not create file [" + targetFile.getAbsolutePath() + "]" );
108 }
109 writer = WriterFactory.newXmlWriter( targetFile );
110 XSTREAM.toXML( webappStructure, writer );
111 }
112 finally
113 {
114 IOUtil.close( writer );
115 }
116
117 }
118 }