1 package org.apache.maven.plugin.war.util;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
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 * Serializes {@link WebappStructure} back and forth.
36 *
37 * @author Stephane Nicoll
38 * @version $Id: WebappStructureSerializer.html 925069 2014-10-08 17:03:57Z khmarbaise $
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 // Register aliases
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 * Creates a new instance of the serializer.
58 */
59 public WebappStructureSerializer()
60 {
61 }
62
63 /**
64 * Reads the {@link WebappStructure} from the specified file.
65 *
66 * @param file the file containing the webapp structure
67 * @return the webapp structure
68 * @throws IOException if an error occurred while reading the structure
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 * Saves the {@link WebappStructure} to the specified file.
88 *
89 * @param webappStructure the structure to save
90 * @param targetFile the file to use to save the structure
91 * @throws IOException if an error occurred while saving the webapp structure
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 }