Turned "assert" in ResourceUtils into "if"
[metamodel.git] / core / src / main / java / org / apache / metamodel / util / ResourceUtils.java
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.metamodel.util;
20
21 import java.net.URI;
22
23 import org.apache.metamodel.factory.ResourceFactoryRegistryImpl;
24 import org.apache.metamodel.factory.ResourceProperties;
25 import org.apache.metamodel.factory.SimpleResourceProperties;
26 import org.apache.metamodel.factory.UnsupportedResourcePropertiesException;
27
28 /**
29 * Static utility methods for handling {@link Resource}s.
30 */
31 public class ResourceUtils {
32
33 /**
34 * Creates a Resource based on a URI
35 *
36 * @param uri
37 * @return
38 * @throws UnsupportedResourcePropertiesException
39 * if the scheme or other part of the URI is unsupported.
40 */
41 public static Resource toResource(URI uri) throws UnsupportedResourcePropertiesException {
42 return toResource(new SimpleResourceProperties(uri));
43 }
44
45 /**
46 * Creates a Resource based on a path or URI (represented by a String)
47 *
48 * @param uri
49 * @return
50 * @throws UnsupportedResourcePropertiesException
51 * if the scheme or other part of the string is unsupported.
52 */
53 public static Resource toResource(String uri) throws UnsupportedResourcePropertiesException {
54 return toResource(new SimpleResourceProperties(uri));
55 }
56
57 /**
58 * Creates a Resource based on the {@link ResourceProperties} definition.
59 *
60 * @param resourceProperties
61 * @return
62 * @throws UnsupportedResourcePropertiesException
63 * if the provided properties cannot be handled in creation of a
64 * resource.
65 */
66 public static Resource toResource(ResourceProperties resourceProperties)
67 throws UnsupportedResourcePropertiesException {
68 return ResourceFactoryRegistryImpl.getDefaultInstance().createResource(resourceProperties);
69 }
70
71 /**
72 * Gets the parent name of a resource. For example, if the resource's
73 * qualified path is /foo/bar/baz, this method will return "bar".
74 *
75 * @param resource
76 * @return
77 */
78 public static String getParentName(Resource resource) {
79 final String name = resource.getName();
80 final String qualifiedPath = resource.getQualifiedPath();
81
82 if (!qualifiedPath.endsWith(name)) {
83 return "";
84 }
85
86 final int indexOfChild = qualifiedPath.length() - name.length();
87
88 if (indexOfChild <= 0) {
89 return "";
90 }
91
92 String parentQualifiedPath = qualifiedPath.substring(0, indexOfChild);
93
94 if ("/".equals(parentQualifiedPath)) {
95 return parentQualifiedPath;
96 }
97
98 parentQualifiedPath = parentQualifiedPath.substring(0, parentQualifiedPath.length() - 1);
99
100 final int lastIndexOfSlash = parentQualifiedPath.lastIndexOf('/');
101 final int lastIndexOfBackSlash = parentQualifiedPath.lastIndexOf('\\');
102 final int lastIndexToUse = Math.max(lastIndexOfSlash, lastIndexOfBackSlash);
103
104 if (lastIndexToUse == -1) {
105 return parentQualifiedPath;
106 }
107
108 // add one because of the slash/backslash itself
109 // lastIndexToUse++;
110
111 final String parentName = parentQualifiedPath.substring(lastIndexToUse + 1);
112 return parentName;
113 }
114 }