Skip to content

Method: pathToDidlId(Path)

1: /*
2: * *********************************************************************************************************************
3: *
4: * blueMarine II: Semantic Media Centre
5: * http://tidalwave.it/projects/bluemarine2
6: *
7: * Copyright (C) 2015 - 2021 by Tidalwave s.a.s. (http://tidalwave.it)
8: *
9: * *********************************************************************************************************************
10: *
11: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
12: * the License. You may obtain a copy of the License at
13: *
14: * http://www.apache.org/licenses/LICENSE-2.0
15: *
16: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
17: * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
18: * specific language governing permissions and limitations under the License.
19: *
20: * *********************************************************************************************************************
21: *
22: * git clone https://bitbucket.org/tidalwave/bluemarine2-src
23: * git clone https://github.com/tidalwave-it/bluemarine2-src
24: *
25: * *********************************************************************************************************************
26: */
27: package it.tidalwave.bluemarine2.upnp.mediaserver.impl;
28:
29: import javax.annotation.Nonnegative;
30: import javax.annotation.Nonnull;
31: import java.nio.file.Path;
32: import java.nio.file.Paths;
33: import lombok.NoArgsConstructor;
34: import static lombok.AccessLevel.PRIVATE;
35:
36: /***********************************************************************************************************************
37: *
38: * Holder of miscellaneous utility methods.
39: *
40: * @author Fabrizio Giudici
41: *
42: **********************************************************************************************************************/
43: @NoArgsConstructor(access = PRIVATE)
44: public final class UpnpUtilities
45: {
46: public static final String ID_NONE = "-1";
47:
48: private static final String ID_ROOT = "0";
49:
50: private static final String PATH_ROOT = "/";
51:
52: private static final String REGEXP_ROOT = "^/$";
53:
54: /*******************************************************************************************************************
55: *
56: * A conversion table for shortening external URLs. This unfortunately introduces a dependency on browsers and
57: * entity types, but some DLNA cients don't accept long paths. See BMT-62.
58: *
59: ******************************************************************************************************************/
60: private static final String[][] REPLACEMENT_PAIRS = new String[][]
61: {
62: { "/urn:bluemarine:artist:", "/a:" },
63: { "/urn:bluemarine:record:", "/r:" },
64: { "/urn:bluemarine:track:", "/t:" },
65: { "/RepositoryBrowserByArtistThenRecord", "/a+r+t" },
66: { "/RepositoryBrowserByArtistThenTrack", "/a+t" },
67: { "/RepositoryBrowserByRecordThenTrack", "/r+t" },
68: { "/RepositoryBrowserByTrack", "/t" },
69: { "/DefaultMediaFileSystem", "/fs" },
70: };
71:
72: /*******************************************************************************************************************
73: *
74: * Converts to a {@link Path} to a a DIDL id.
75: *
76: * @param path the path
77: * @return the DIDL id
78: *
79: ******************************************************************************************************************/
80: @Nonnull
81: public static String pathToDidlId (@Nonnull final Path path)
82: {
83: return externalized(path.toString().replaceAll(REGEXP_ROOT, ID_ROOT));
84: }
85:
86: /*******************************************************************************************************************
87: *
88: * Converts a DIDL id to a {@link Path}.
89: *
90: * @param id the DIDL id
91: * @return the path
92: *
93: ******************************************************************************************************************/
94: @Nonnull
95: public static Path didlIdToPath (@Nonnull final String id)
96: {
97: return Paths.get(id.equals(ID_ROOT) ? PATH_ROOT : internalized(id));
98: }
99:
100: /*******************************************************************************************************************
101: *
102: * See BMT-62.
103: *
104: ******************************************************************************************************************/
105: @Nonnull
106: public static String externalized (@Nonnull String string)
107: {
108: for (final String[] pair : REPLACEMENT_PAIRS)
109: {
110: string = string.replace(pair[0], pair[1]);
111: }
112:
113: return string;
114: }
115:
116: /*******************************************************************************************************************
117: *
118: * See BMT-62.
119: *
120: ******************************************************************************************************************/
121: @Nonnull
122: public static String internalized (@Nonnull String string)
123: {
124: for (final String[] pair : REPLACEMENT_PAIRS)
125: {
126: string = string.replace(pair[1], pair[0]);
127: }
128:
129: return string;
130: }
131:
132: /*******************************************************************************************************************
133: *
134: * Fixes the {@code maxCount} parameter of ContentDirectory {@code browse()}.
135: *
136: * @param value the input value
137: * @return the max count
138: *
139: ******************************************************************************************************************/
140: @Nonnegative
141: public static int maxCount (@Nonnegative final long value)
142: {
143: return (value == 0) ? Integer.MAX_VALUE : (int)value;
144: }
145: }