View Javadoc

1   package org.apache.maven.continuum.web.appareance;
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 org.apache.continuum.web.appearance.ContinuumAppearance;
23  import org.apache.continuum.web.appearance.io.xpp3.ContinuumAppearanceModelsXpp3Reader;
24  import org.apache.continuum.web.appearance.io.xpp3.ContinuumAppearanceModelsXpp3Writer;
25  import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
26  import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
27  import org.codehaus.plexus.util.ReaderFactory;
28  import org.codehaus.plexus.util.StringUtils;
29  import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
30  import org.jsoup.Jsoup;
31  import org.jsoup.safety.Whitelist;
32  import org.slf4j.Logger;
33  import org.slf4j.LoggerFactory;
34  
35  import java.io.File;
36  import java.io.FileWriter;
37  import java.io.IOException;
38  import java.util.Calendar;
39  
40  /**
41   * @author <a href="mailto:olamy@apache.org">olamy</a>
42   * @version $Id: DefaultAppareanceConfiguration.java 1372267 2012-08-13 05:47:30Z brett $
43   * @plexus.component role="org.apache.maven.continuum.web.appareance.AppareanceConfiguration" role-hint="default"
44   * @since 10 nov. 07
45   */
46  public class DefaultAppareanceConfiguration
47      implements AppareanceConfiguration, Initializable
48  {
49      private static final Logger log = LoggerFactory.getLogger( DefaultAppareanceConfiguration.class );
50  
51      private String footer;
52  
53      public static final String APPEARANCE_FILE_NAME = "continuum-appearance.xml";
54  
55      private ContinuumAppearance continuumAppearance = new ContinuumAppearance();
56  
57      // ------------------------------------------------
58      //  Plexus Lifecycle
59      // ------------------------------------------------
60  
61      public void initialize()
62          throws InitializationException
63      {
64  
65          File appearanceConfFile = getAppearanceConfigurationFile();
66  
67          if ( appearanceConfFile.exists() )
68          {
69              try
70              {
71                  ContinuumAppearanceModelsXpp3Reader appearanceReader = new ContinuumAppearanceModelsXpp3Reader();
72                  this.continuumAppearance = appearanceReader.read( ReaderFactory.newXmlReader( appearanceConfFile ) );
73                  if ( continuumAppearance != null )
74                  {
75                      this.footer = continuumAppearance.getFooter();
76                  }
77              }
78              catch ( IOException e )
79              {
80                  log.warn(
81                      "skip IOException reading appearance file " + APPEARANCE_FILE_NAME + ", msg " + e.getMessage() );
82              }
83              catch ( XmlPullParserException e )
84              {
85                  log.warn( "skip XmlPullParserException reading appearance file " + APPEARANCE_FILE_NAME + ", msg " +
86                                e.getMessage() );
87              }
88          }
89          if ( StringUtils.isEmpty( this.footer ) )
90          {
91              // initiate with default footer (save in registry ?)
92              this.footer = getDefaultFooter();
93          }
94      }
95  
96      /**
97       * @see org.apache.maven.continuum.web.appareance.AppareanceConfiguration#getFooter()
98       */
99      public String getFooter()
100     {
101         return this.footer;
102     }
103 
104     /**
105      * @see org.apache.maven.continuum.web.appareance.AppareanceConfiguration#saveFooter(java.lang.String)
106      */
107     public void saveFooter( String footerHtmlContent )
108         throws IOException
109     {
110         String safeFooterHtmlContent = Jsoup.clean( footerHtmlContent, Whitelist.basic() );
111 
112         continuumAppearance.setFooter( safeFooterHtmlContent );
113         ContinuumAppearanceModelsXpp3Writer writer = new ContinuumAppearanceModelsXpp3Writer();
114         File confFile = getAppearanceConfigurationFile();
115         if ( !confFile.exists() )
116         {
117             confFile.getParentFile().mkdirs();
118         }
119         FileWriter fileWriter = new FileWriter( confFile );
120         writer.write( fileWriter, continuumAppearance );
121         fileWriter.close();
122         this.footer = safeFooterHtmlContent;
123     }
124 
125 
126     private String getDefaultFooter()
127     {
128         int inceptionYear = 2005;
129         int currentYear = Calendar.getInstance().get( Calendar.YEAR );
130         StringBuilder stringBuilder = new StringBuilder();
131         stringBuilder.append( "<div class=\"xright\">" );
132         stringBuilder.append( "Copyright &copy; " );
133         stringBuilder.append( String.valueOf( inceptionYear ) ).append( "-" ).append( String.valueOf( currentYear ) );
134         stringBuilder.append( "&nbsp;The Apache Software Foundation" );
135         stringBuilder.append( "</div> <div class=\"clear\"><hr/></div>" );
136         return stringBuilder.toString();
137     }
138 
139 
140     private File getAppearanceConfigurationFile()
141     {
142         return new File( System.getProperty( "appserver.base" ) + File.separator + "conf" + File.separator +
143                              APPEARANCE_FILE_NAME );
144     }
145 }