You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@metamodel.apache.org by ka...@apache.org on 2013/08/23 10:46:14 UTC
[2/3] git commit: METAMODEL-4: Added ResourceUtils for getting parent
paths of resources.
METAMODEL-4: Added ResourceUtils for getting parent paths of resources.
Project: http://git-wip-us.apache.org/repos/asf/incubator-metamodel/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-metamodel/commit/bb54ab78
Tree: http://git-wip-us.apache.org/repos/asf/incubator-metamodel/tree/bb54ab78
Diff: http://git-wip-us.apache.org/repos/asf/incubator-metamodel/diff/bb54ab78
Branch: refs/heads/master
Commit: bb54ab78a5df6a74299806513372520d0c2f8bd0
Parents: 3bbd56f
Author: Kasper Sørensen <i....@gmail.com>
Authored: Fri Aug 23 10:26:31 2013 +0200
Committer: Kasper Sørensen <i....@gmail.com>
Committed: Fri Aug 23 10:26:31 2013 +0200
----------------------------------------------------------------------
.../apache/metamodel/util/ResourceUtils.java | 61 ++++++++++++++++++++
.../metamodel/util/ResourceUtilsTest.java | 40 +++++++++++++
2 files changed, 101 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/bb54ab78/core/src/main/java/org/apache/metamodel/util/ResourceUtils.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/metamodel/util/ResourceUtils.java b/core/src/main/java/org/apache/metamodel/util/ResourceUtils.java
new file mode 100644
index 0000000..f39b2c8
--- /dev/null
+++ b/core/src/main/java/org/apache/metamodel/util/ResourceUtils.java
@@ -0,0 +1,61 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.metamodel.util;
+
+/**
+ * Static utility methods for handling {@link Resource}s.
+ */
+public class ResourceUtils {
+
+ /**
+ * Gets the parent name of a resource. For example, if the resource's
+ * qualified path is /foo/bar/baz, this method will return "bar".
+ *
+ * @param resource
+ * @return
+ */
+ public static String getParentName(Resource resource) {
+ String name = resource.getName();
+ String qualifiedPath = resource.getQualifiedPath();
+
+ assert qualifiedPath.endsWith(name);
+
+ int indexOfChild = qualifiedPath.length() - name.length() - 1;
+
+ if (indexOfChild <= 0) {
+ return name;
+ }
+
+ String parentQualifiedPath = qualifiedPath.substring(0, indexOfChild);
+
+ int lastIndexOfSlash = parentQualifiedPath.lastIndexOf('/');
+ int lastIndexOfBackSlash = parentQualifiedPath.lastIndexOf('\\');
+ int lastIndexToUse = Math.max(lastIndexOfSlash, lastIndexOfBackSlash);
+
+ if (lastIndexToUse == -1) {
+ return parentQualifiedPath;
+ }
+
+ // add one because of the slash/backslash itself
+ lastIndexToUse++;
+
+ String parentName = parentQualifiedPath.substring(lastIndexToUse);
+ return parentName;
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/bb54ab78/core/src/test/java/org/apache/metamodel/util/ResourceUtilsTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/metamodel/util/ResourceUtilsTest.java b/core/src/test/java/org/apache/metamodel/util/ResourceUtilsTest.java
new file mode 100644
index 0000000..c3a24aa
--- /dev/null
+++ b/core/src/test/java/org/apache/metamodel/util/ResourceUtilsTest.java
@@ -0,0 +1,40 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.metamodel.util;
+
+import java.io.File;
+
+import junit.framework.TestCase;
+
+public class ResourceUtilsTest extends TestCase {
+
+ public void testGetParentName() throws Exception {
+ Resource res = new FileResource(new File("src/test/resources/folder/foo"));
+
+ assertEquals("folder", ResourceUtils.getParentName(res));
+
+ res = new FileResource(new File("src/test/resources/folder/"));
+
+ assertEquals("resources", ResourceUtils.getParentName(res));
+
+ assertEquals("", ResourceUtils.getParentName(new InMemoryResource("")));
+ assertEquals("foo", ResourceUtils.getParentName(new InMemoryResource("foo")));
+ assertEquals("bar", ResourceUtils.getParentName(new InMemoryResource("foo/bar\\baz")));
+ }
+}