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
71 public WebappStructure fromXml( File file )
72 throws IOException
73 {
74 Reader reader = null;
75
76 try
77 {
78 reader = ReaderFactory.newXmlReader( file );
79 return (WebappStructure) xStream.fromXML( reader );
80 }
81 finally
82 {
83 IOUtil.close( reader );
84 }
85 }
86
87
88
89
90
91
92
93
94 public void toXml( WebappStructure webappStructure, File targetFile )
95 throws IOException
96 {
97 Writer writer = null;
98 try
99 {
100 if ( !targetFile.getParentFile().exists() && !targetFile.getParentFile().mkdirs() )
101 {
102 throw new IOException(
103 "Could not create parent [" + targetFile.getParentFile().getAbsolutePath() + "]" );
104 }
105
106 if ( !targetFile.exists() && !targetFile.createNewFile() )
107 {
108 throw new IOException( "Could not create file [" + targetFile.getAbsolutePath() + "]" );
109 }
110 writer = WriterFactory.newXmlWriter( targetFile );
111 xStream.toXML( webappStructure, writer );
112 }
113 finally
114 {
115 IOUtil.close( writer );
116 }
117 }
118 }