This project has retired. For details please refer to its Attic page.
Banner xref
View Javadoc
1   package org.apache.archiva.web.startup;
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.commons.lang3.StringUtils;
23  import org.slf4j.LoggerFactory;
24  
25  import java.util.regex.Matcher;
26  import java.util.regex.Pattern;
27  
28  /**
29   * Banner 
30   *
31   *
32   */
33  public class Banner
34  {
35      private static final String eol = System.getProperty("line.separator");
36  
37      public static String encode( String raw )
38      {
39          // Canonicalize line ends to make them easier to process
40          raw = raw.replace("\r\n", "\n").replace("\r", "\n");
41  
42          StringBuilder encoded = new StringBuilder();
43          int rawlen = raw.length();
44  
45          for ( int i = 0; i < rawlen; i++ )
46          {
47              char c = raw.charAt( i );
48              if ( c == '\\' )
49              {
50                  encoded.append( "$." );
51              }
52              else if ( c == '$' )
53              {
54                  encoded.append( "$$" );
55              }
56              else if ( c == '\n' )
57              {
58                  encoded.append( "$n" );
59              }
60              else if ( Character.isDigit( c ) )
61              {
62                  encoded.append( c );
63              }
64              else if ( Character.isLetter( c ) )
65              {
66                  encoded.append( rot13( c ) );
67              }
68              else if ( i < raw.length() - 1 )
69              {
70                  char nc;
71                  boolean done = false;
72                  int count = 0;
73                  for ( int n = i; !done; n++ )
74                  {
75                      if ( n >= rawlen )
76                      {
77                          break;
78                      }
79  
80                      nc = raw.charAt( n );
81  
82                      if ( nc != c )
83                      {
84                          done = true;
85                      }
86                      else
87                      {
88                          count++;
89                      }
90                  }
91                  if ( count < 3 )
92                  {
93                      encoded.append( c );
94                  }
95                  else
96                  {
97                      encoded.append( "$" ).append( String.valueOf( count ) ).append( c );
98                      i += count - 1;
99                  }
100             }
101             else
102             {
103                 encoded.append( c );
104             }
105         }
106 
107         return encoded.toString();
108     }
109 
110     public static String decode( String encoded )
111     {
112         StringBuilder decoded = new StringBuilder();
113         int enlen = encoded.length();
114         for ( int i = 0; i < enlen; i++ )
115         {
116             char c = encoded.charAt( i );
117             if ( c == '$' )
118             {
119                 char nc = encoded.charAt( i + 1 );
120                 if ( nc == '$' )
121                 {
122                     decoded.append( '$' );
123                     i++;
124                 }
125                 else if ( nc == '.' )
126                 {
127                     decoded.append( '\\' );
128                     i++;
129                 }
130                 else if ( nc == 'n' )
131                 {
132                     decoded.append( eol );
133                     i++;
134                 }
135                 else if ( Character.isDigit( nc ) )
136                 {
137                     int count = 0;
138                     int nn = i + 1;
139                     while ( Character.isDigit( nc ) )
140                     {
141                         count = ( count * 10 );
142                         count += ( nc - '0' );
143                         nc = encoded.charAt( ++nn );
144                     }
145                     for ( int d = 0; d < count; d++ )
146                     {
147                         decoded.append( nc );
148                     }
149                     i = nn;
150                 }
151             }
152             else if ( Character.isLetter( c ) )
153             {
154                 decoded.append( rot13( c ) );
155             }
156             else
157             {
158                 decoded.append( c );
159             }
160         }
161 
162         return decoded.toString();
163     }
164 
165     private static char rot13( char c )
166     {
167         if ( ( c >= 'a' ) && ( c <= 'z' ) )
168         {
169             char dc = c += 13;
170             if ( dc > 'z' )
171             {
172                 dc -= 26;
173             }
174             return dc;
175         }
176         else if ( ( c >= 'A' ) && ( c <= 'Z' ) )
177         {
178             char dc = c += 13;
179             if ( dc > 'Z' )
180             {
181                 dc -= 26;
182             }
183             return dc;
184         }
185         else
186         {
187             return c;
188         }
189     }
190 
191     public static String injectVersion( String text, String version )
192     {
193         Pattern pat = Pattern.compile( "#{2,}" );
194         Matcher mat = pat.matcher( text );
195         StringBuilder ret = new StringBuilder();
196         int off = 0;
197 
198         while ( mat.find( off ) )
199         {
200             ret.append( text.substring( off, mat.start() ) );
201             String repl = mat.group();
202             ret.append( StringUtils.center( version, repl.length() ) );
203             off = mat.end();
204         }
205 
206         ret.append( text.substring( off ) );
207 
208         return ret.toString();
209     }
210 
211     public static String getBanner( String version )
212     {
213         String encodedBanner = "$26 $34_$n$15 /$._$7 /$34 $.$n$14 /`/@),$4 |  Ba" +
214                 " orunys bs nyy bs gur nycnpnf   |$n$14 |  (~'  __| gbvyvat njnl ba " +
215                 "gur Ncnpur Nepuvin |$n$6 _,--.$3_/  |$4 $.$5  cebwrpg grnz, V jbhyq y" +
216                 "vxr gb$3 |$n$4 ,' ,$5 ($3 |$5 $.$5     jrypbzr lbh gb Nepuvin$6 |$" +
217                 "n$4 |  ($6 $.  /$6 |  $32#  |$n$5 $.  )$._/  ,_/$7 |$36 |$n$5 / /$3 " +
218                 "( |/$9 |     uggc://nepuvin.ncnpur.bet/     |$n$4 ( |$4 ( |$10 |     hf" +
219                 "ref@nepuvin.ncnpur.bet$7 |$n$5 $.|$5 $.|$11 $.$34_/$n$n";
220 
221         return injectVersion( decode( encodedBanner ), version );
222     }
223 
224     public static void display( String version )
225     {
226         String banner = getBanner( version );
227         LoggerFactory.getLogger( Banner.class ).info( "{} {}{}" , StringUtils.repeat( "_", 25 ), eol, banner );
228     }
229 }