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 /**
27 * The DocumentName for an ArchiveEntry.
28 */
29 public class ArchiveEntryName extends DocumentName {
30 /** The name of the document that contains this entry. */
31 private final DocumentName archiveFileName;
32
33 /**
34 * Sets the builder so that a proper DocumentName is constructed for an archive entry.
35 * @param archiveFileName the archive file DocumentName
36 * @param archiveEntryName the entry name
37 * @return the DocumentName.Builder for the archive entry.
38 */
39 private static DocumentName.Builder prepareBuilder(final DocumentName archiveFileName, final String archiveEntryName) {
40 String root = archiveFileName.getName() + "#/";
41 FSInfo fsInfo = new FSInfo("archiveEntry", "/", true, Collections.singletonList(root));
42 return DocumentName.builder(fsInfo)
43 .setRoot(root)
44 .setBaseName("/")
45 .setName(archiveEntryName);
46 }
47
48 /**
49 * Constucts an archive file name from an archive file document name and an entry name.
50 * @param archiveFileName the archive file document name.
51 * @param archiveEntryName the archive entry name.
52 */
53 public ArchiveEntryName(final DocumentName archiveFileName, final String archiveEntryName) {
54 super(prepareBuilder(archiveFileName, archiveEntryName));
55 this.archiveFileName = archiveFileName;
56 }
57
58 @Override
59 public File asFile() {
60 return archiveFileName.asFile();
61 }
62
63 @Override
64 public Path asPath() {
65 return Paths.get(archiveFileName.asPath().toString(), "#", super.asPath().toString());
66 }
67
68 @Override
69 public DocumentName resolve(final String child) {
70 return new ArchiveEntryName(this.archiveFileName, super.resolve(child).localized());
71 }
72
73 @Override
74 public String getBaseName() {
75 return archiveFileName.getName() + "#";
76 }
77
78 @Override
79 public String localized(final String dirSeparator) {
80 String superLocal = super.localized(dirSeparator);
81 superLocal = superLocal.substring(superLocal.lastIndexOf("#") + 1);
82 return archiveFileName.localized(dirSeparator) + "#" + superLocal;
83 }
84 }