View Javadoc
1   package org.apache.maven.plugins.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 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   * Serializes {@link WebappStructure} back and forth.
45   *
46   * @author Stephane Nicoll
47   * @version $Id: WebappStructureSerializer.java 1792363 2017-04-23 13:36:39Z rfscholte $
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          // Register aliases
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       * Creates a new instance of the serializer.
80       */
81      public WebappStructureSerializer()
82      {
83      }
84  
85      /**
86       * Reads the {@link WebappStructure} from the specified file.
87       *
88       * @param file the file containing the webapp structure
89       * @return the webapp structure
90       * @throws IOException if an error occurred while reading the structure
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      * Saves the {@link WebappStructure} to the specified file.
113      *
114      * @param webappStructure the structure to save
115      * @param targetFile the file to use to save the structure
116      * @throws IOException if an error occurred while saving the webapp structure
117      */
118     public void toXml( WebappStructure webappStructure, File targetFile )
119         throws IOException
120     {
121         // CHECKSTYLE_OFF: LineLength
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         // CHECKSTYLE_ON: LineLength
144     }
145 }