1 package org.apache.maven.plugins.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 static com.thoughtworks.xstream.XStream.PRIORITY_NORMAL;
24 import static com.thoughtworks.xstream.XStream.PRIORITY_VERY_LOW;
25 import com.thoughtworks.xstream.converters.basic.IntConverter;
26 import com.thoughtworks.xstream.converters.basic.StringConverter;
27 import com.thoughtworks.xstream.converters.collections.CollectionConverter;
28 import com.thoughtworks.xstream.converters.collections.MapConverter;
29 import com.thoughtworks.xstream.converters.reflection.ReflectionConverter;
30 import com.thoughtworks.xstream.converters.reflection.ReflectionProvider;
31 import com.thoughtworks.xstream.io.xml.DomDriver;
32 import com.thoughtworks.xstream.mapper.Mapper;
33 import org.apache.maven.model.Dependency;
34 import org.codehaus.plexus.util.IOUtil;
35 import org.codehaus.plexus.util.ReaderFactory;
36 import org.codehaus.plexus.util.WriterFactory;
37
38 import java.io.File;
39 import java.io.IOException;
40 import java.io.Reader;
41 import java.io.Writer;
42
43
44
45
46
47
48
49 public class WebappStructureSerializer
50 {
51
52 private static final XStream XSTREAM;
53
54 static
55 {
56 XSTREAM = new XStream( new DomDriver() )
57 {
58 @Override
59 protected void setupConverters()
60 {
61 Mapper mapper = getMapper();
62 ReflectionProvider reflectionProvider = getReflectionProvider();
63 registerConverter(
64 new ReflectionConverter( mapper, reflectionProvider ), PRIORITY_VERY_LOW );
65 registerConverter( new StringConverter(), PRIORITY_NORMAL );
66 registerConverter( new IntConverter(), PRIORITY_NORMAL );
67 registerConverter( new CollectionConverter( mapper ), PRIORITY_NORMAL );
68 registerConverter( new MapConverter( mapper ), PRIORITY_NORMAL );
69 }
70 };
71
72 XSTREAM.alias( "webapp-structure", WebappStructure.class );
73 XSTREAM.alias( "path-set", PathSet.class );
74 XSTREAM.alias( "dependency", Dependency.class );
75
76 }
77
78
79
80
81 public WebappStructureSerializer()
82 {
83 }
84
85
86
87
88
89
90
91
92 public WebappStructure fromXml( File file )
93 throws IOException
94 {
95 Reader reader = null;
96
97 try
98 {
99 reader = ReaderFactory.newXmlReader( file );
100 final WebappStructure webappStructure = (WebappStructure) XSTREAM.fromXML( reader );
101 reader.close();
102 reader = null;
103 return webappStructure;
104 }
105 finally
106 {
107 IOUtil.close( reader );
108 }
109 }
110
111
112
113
114
115
116
117
118 public void toXml( WebappStructure webappStructure, File targetFile )
119 throws IOException
120 {
121
122 Writer writer = null;
123 try
124 {
125 if ( !targetFile.getParentFile().exists() && !targetFile.getParentFile().mkdirs() )
126 {
127 throw new IOException( "Could not create parent [" + targetFile.getParentFile().getAbsolutePath() + "]" );
128 }
129
130 if ( !targetFile.exists() && !targetFile.createNewFile() )
131 {
132 throw new IOException( "Could not create file [" + targetFile.getAbsolutePath() + "]" );
133 }
134 writer = WriterFactory.newXmlWriter( targetFile );
135 XSTREAM.toXML( webappStructure, writer );
136 writer.close();
137 writer = null;
138 }
139 finally
140 {
141 IOUtil.close( writer );
142 }
143
144 }
145 }