View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.doxia.site;
20  
21  import org.codehaus.plexus.util.StringUtils;
22  import org.codehaus.plexus.util.xml.Xpp3Dom;
23  
24  /**
25   * Site model utilities.
26   *
27   * @since 1.7
28   */
29  public class SiteUtils {
30      public static boolean isLink(String href) {
31          return StringUtils.isNotBlank(href)
32                  && (startsWithAnyIgnoreCase(href, "http:/", "https:/", "ftp:/", "mailto:", "file:/")
33                          || href.contains("://"));
34      }
35  
36      private static boolean startsWithIgnoreCase(String str, String prefix) {
37          if (str == null || prefix == null) {
38              return (str == null && prefix == null);
39          }
40          if (prefix.length() > str.length()) {
41              return false;
42          }
43          return str.regionMatches(true, 0, prefix, 0, prefix.length());
44      }
45  
46      public static boolean startsWithAnyIgnoreCase(String string, String... searchStrings) {
47          for (int i = 0; i < searchStrings.length; i++) {
48              String searchString = searchStrings[i];
49              if (startsWithIgnoreCase(string, searchString)) {
50                  return true;
51              }
52          }
53          return false;
54      }
55  
56      /**
57       * Helper to get site custom DOM element by simply specifying a dotted path.
58       *
59       * @param custom the custom DOM element
60       * @param path the dotted path to the child
61       * @return <code>null</code> if any element in the path does not exist
62       * @since 1.8
63       */
64      public static Xpp3Dom getCustomChild(Xpp3Dom custom, String path) {
65          String[] elements = path.split("\\.");
66          for (String element : elements) {
67              if (custom == null) {
68                  return null;
69              }
70              custom = custom.getChild(element);
71          }
72          return custom;
73      }
74  
75      /**
76       * Helper to get site custom DOM element value by simply specifying a dotted path.
77       *
78       * @param custom the custom DOM element
79       * @param path the dotted path to the child
80       * @return the element value or <code>null</code> if any element in the path does not exist
81       * @since 1.8
82       */
83      public static String getCustomValue(Xpp3Dom custom, String path) {
84          custom = getCustomChild(custom, path);
85          return (custom == null) ? null : custom.getValue();
86      }
87  
88      /**
89       * Helper to get site custom DOM element value by simply specifying a dotted path.
90       *
91       * @param custom the custom DOM element
92       * @param path the dotted path to the child
93       * @param defaultValue default value
94       * @return the element value or the default value if any element in the path does not exist
95       * @since 1.8
96       */
97      public static String getCustomValue(Xpp3Dom custom, String path, String defaultValue) {
98          custom = getCustomChild(custom, path);
99          return (custom == null) ? defaultValue : custom.getValue();
100     }
101 }