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.java 985593 2010-08-14 22:12:09Z dennisl $
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 /**
65 * Reads the {@link WebappStructure} from the specified file.
66 *
67 * @param file the file containing the webapp structure
68 * @return the webapp structure
69 * @throws IOException if an error occurred while reading the structure
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 * Saves the {@link WebappStructure} to the specified file.
89 *
90 * @param webappStructure the structure to save
91 * @param targetFile the file to use to save the structure
92 * @throws IOException if an error occurred while saving the webapp structure
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 }