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 Writer writer = null;
97 try
98 {
99 if ( !targetFile.getParentFile().exists() && !targetFile.getParentFile().mkdirs() )
100 {
101 throw new IOException( "Could not create parent [" + targetFile.getParentFile().getAbsolutePath() + "]" );
102 }
103
104 if ( !targetFile.exists() && !targetFile.createNewFile() )
105 {
106 throw new IOException( "Could not create file [" + targetFile.getAbsolutePath() + "]" );
107 }
108 writer = WriterFactory.newXmlWriter( targetFile );
109 xstream.toXML( webappStructure, writer );
110 }
111 finally
112 {
113 IOUtil.close( writer );
114 }
115 }
116 }