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.rat.document;
20  
21  import java.io.File;
22  import java.nio.file.Path;
23  import java.nio.file.Paths;
24  import java.util.Collections;
25  
26  public class ArchiveEntryName extends DocumentName {
27      /** The name of the document that contains this entry. */
28      private final DocumentName archiveFileName;
29  
30      private static DocumentName.Builder prepareBuilder(final DocumentName archiveFileName, final String archiveEntryName) {
31          String root = archiveFileName.getName() + "#";
32          FSInfo fsInfo = new FSInfo("archiveEntry", "/", true, Collections.singletonList(root));
33          return DocumentName.builder(fsInfo)
34                  .setRoot(root)
35                  .setBaseName(root + "/")
36                  .setName(archiveEntryName);
37      }
38      public ArchiveEntryName(final DocumentName archiveFileName, final String archiveEntryName) {
39          super(prepareBuilder(archiveFileName, archiveEntryName));
40          this.archiveFileName = archiveFileName;
41      }
42  
43      @Override
44      public File asFile() {
45          return archiveFileName.asFile();
46      }
47  
48      @Override
49      public Path asPath() {
50          return Paths.get(archiveFileName.asPath().toString(), "#", super.asPath().toString());
51      }
52  
53      @Override
54      public DocumentName resolve(final String child) {
55          return new ArchiveEntryName(this.archiveFileName, super.resolve(child).localized());
56      }
57  
58      @Override
59      public String getBaseName() {
60          return archiveFileName.getName() + "#";
61      }
62  
63      @Override
64      boolean startsWithRootOrSeparator(final String candidate, final String root, final String separator) {
65          return super.startsWithRootOrSeparator(candidate, root, separator);
66      }
67  
68      @Override
69      public String localized(final String dirSeparator) {
70          String superLocal = super.localized(dirSeparator);
71          superLocal = superLocal.substring(superLocal.lastIndexOf("#") + 1);
72          return archiveFileName.localized(dirSeparator) + "#" + superLocal;
73      }
74  }