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.creadur.tentacles;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.util.Collections;
24  import java.util.Map;
25  
26  public class Licenses {
27  
28      private final IOSystem ioSystem;
29      private final Map<String, String> licenses;
30  
31      public Licenses(final Map<String, String> licenses, final Platform platform) {
32          super();
33          this.ioSystem = platform.getIoSystem();
34          this.licenses = Collections.unmodifiableMap(licenses);
35      }
36  
37      public License from(final File document) throws IOException {
38          return license(this.ioSystem.slurp(document));
39      }
40  
41      private License license(final String text) {
42          final String key = toKey(text);
43          return new License(key, normalize(text));
44      }
45  
46      private String toKey(final String text) {
47          return text.replaceAll("[ \\n\\t\\r]+", "").toLowerCase().intern();
48      }
49  
50      private String normalize(String text) {
51          for (final Map.Entry<String, String> license : this.licenses.entrySet()) {
52              text =
53                      text.replace(
54                              license.getValue(),
55                              String.format("---[%s - full text]---\n\n",
56                                      license.getKey()));
57          }
58          text = text.intern();
59          return text;
60      }
61  }