View Javadoc
1   package org.apache.maven.doxia.site.decoration;
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.codehaus.plexus.util.StringUtils;
23  import org.codehaus.plexus.util.xml.Xpp3Dom;
24  
25  /**
26   * Decoration model utilities.
27   * 
28   * @since 1.7
29   */
30  public class DecorationUtils
31  {
32      public static boolean isLink( String href )
33      {
34          return StringUtils.isNotBlank( href )
35              && ( startsWithAnyIgnoreCase( href, "http:/", "https:/", "ftp:/", "mailto:", "file:/" )
36                  || href.contains( "://" ) );
37      }
38  
39      private static boolean startsWithIgnoreCase( String str, String prefix )
40      {
41          if ( str == null || prefix == null )
42          {
43              return ( str == null && prefix == null );
44          }
45          if ( prefix.length() > str.length() )
46          {
47              return false;
48          }
49          return str.regionMatches( true, 0, prefix, 0, prefix.length() );
50      }
51  
52      public static boolean startsWithAnyIgnoreCase( String string, String... searchStrings )
53      {
54          for ( int i = 0; i < searchStrings.length; i++ )
55          {
56              String searchString = searchStrings[i];
57              if ( startsWithIgnoreCase( string, searchString ) )
58              {
59                  return true;
60              }
61          }
62          return false;
63      }
64  
65      /**
66       * Helper to get decoration custom DOM element by simply specifying a dotted path.
67       *
68       * @param custom the custom DOM element
69       * @param path the dotted path to the child
70       * @return <code>null</code> if any element in the path does not exist
71       * @since 1.8
72       */
73      public static Xpp3Dom getCustomChild( Xpp3Dom custom, String path )
74      {
75          String[] elements = path.split( "\\." );
76          for ( String element : elements )
77          {
78              if ( custom == null )
79              {
80                  return null;
81              }
82              custom = custom.getChild( element );
83          }
84          return custom;
85      }
86  
87      /**
88       * Helper to get decoration custom DOM element value by simply specifying a dotted path.
89       *
90       * @param custom the custom DOM element
91       * @param path the dotted path to the child
92       * @return the element value or <code>null</code> if any element in the path does not exist
93       * @since 1.8
94       */
95      public static String getCustomValue( Xpp3Dom custom, String path )
96      {
97          custom = getCustomChild( custom, path );
98          return ( custom == null ) ? null : custom.getValue();
99      }
100 
101     /**
102      * Helper to get decoration custom DOM element value by simply specifying a dotted path.
103      *
104      * @param custom the custom DOM element
105      * @param path the dotted path to the child
106      * @param defaultValue default value
107      * @return the element value or the default value if any element in the path does not exist
108      * @since 1.8
109      */
110     public static String getCustomValue( Xpp3Dom custom, String path, String defaultValue )
111     {
112         custom = getCustomChild( custom, path );
113         return ( custom == null ) ? defaultValue : custom.getValue();
114     }
115 }