You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@isis.apache.org by da...@apache.org on 2012/12/05 00:41:09 UTC
[15/52] [partial] ISIS-188: consolidating isis-core
http://git-wip-us.apache.org/repos/asf/isis/blob/e4735c72/framework/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/MethodUtils.java
----------------------------------------------------------------------
diff --git a/framework/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/MethodUtils.java b/framework/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/MethodUtils.java
new file mode 100644
index 0000000..964ee56
--- /dev/null
+++ b/framework/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/MethodUtils.java
@@ -0,0 +1,59 @@
+/*
+ * 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.isis.core.commons.lang;
+
+import java.lang.reflect.Method;
+
+public class MethodUtils {
+
+ private MethodUtils() {
+ }
+
+ public static Method getMethod(final Object object, final String methodName, final Class<?>... parameterClass) throws NoSuchMethodException {
+ return getMethod(object.getClass(), methodName, parameterClass);
+ }
+
+ public static Method getMethod(final Object object, final String methodName) throws NoSuchMethodException {
+ return getMethod(object.getClass(), methodName, new Class[0]);
+ }
+
+ public static Method getMethod(final Class<?> clazz, final String methodName, final Class<?>... parameterClass) throws NoSuchMethodException {
+ return clazz.getMethod(methodName, parameterClass);
+ }
+
+ public static Method findMethodElseNull(final Class<?> clazz, final String methodName, final Class<?>... parameterClass) {
+ try {
+ return clazz.getMethod(methodName, parameterClass);
+ } catch (final NoSuchMethodException e) {
+ return null;
+ }
+ }
+
+ public static Method findMethodElseNull(final Class<?> clazz, final String[] candidateMethodNames, final Class<?>... parameterClass) {
+ for (final String candidateMethodName : candidateMethodNames) {
+ final Method method = findMethodElseNull(clazz, candidateMethodName, parameterClass);
+ if (method != null) {
+ return method;
+ }
+ }
+ return null;
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/isis/blob/e4735c72/framework/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/NameUtils.java
----------------------------------------------------------------------
diff --git a/framework/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/NameUtils.java b/framework/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/NameUtils.java
new file mode 100644
index 0000000..8623afa
--- /dev/null
+++ b/framework/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/NameUtils.java
@@ -0,0 +1,171 @@
+/*
+ * 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.isis.core.commons.lang;
+
+public final class NameUtils {
+
+ private static final char SPACE = ' ';
+
+ private NameUtils() {
+ }
+
+ /**
+ * Returns the name of a Java entity without any prefix. A prefix is defined
+ * as the first set of lowercase letters and the name is characters from,
+ * and including, the first upper case letter. If no upper case letter is
+ * found then an empty string is returned.
+ *
+ * <p>
+ * Calling this method with the following Java names will produce these
+ * results:
+ *
+ * <pre>
+ * getCarRegistration -> CarRegistration
+ * CityMayor -> CityMayor
+ * isReady -> Ready
+ * </pre>
+ *
+ */
+ public static String javaBaseName(final String javaName) {
+ int pos = 0;
+
+ // find first upper case character
+ final int len = javaName.length();
+
+ while ((pos < len) && (javaName.charAt(pos) != '_') && Character.isLowerCase(javaName.charAt(pos))) {
+ pos++;
+ }
+
+ if (pos >= len) {
+ return "";
+ }
+
+ if (javaName.charAt(pos) == '_') {
+ pos++;
+ }
+
+ if (pos >= len) {
+ return "";
+ }
+
+ final String baseName = javaName.substring(pos);
+ final char firstChar = baseName.charAt(0);
+
+ if (Character.isLowerCase(firstChar)) {
+ return Character.toUpperCase(firstChar) + baseName.substring(1);
+ } else {
+ return baseName;
+ }
+ }
+
+ public static String javaBaseNameStripAccessorPrefixIfRequired(final String javaName) {
+ if (javaName.startsWith("is") || javaName.startsWith("get")) {
+ return javaBaseName(javaName);
+ } else {
+ return capitalizeName(javaName);
+ }
+ }
+
+ public static String capitalizeName(final String name) {
+ return Character.toUpperCase(name.charAt(0)) + name.substring(1);
+ }
+
+ public static boolean startsWith(final String fullMethodName, final String prefix) {
+ final int length = prefix.length();
+ if (length >= fullMethodName.length()) {
+ return false;
+ } else {
+ final char startingCharacter = fullMethodName.charAt(length);
+ return fullMethodName.startsWith(prefix) && Character.isUpperCase(startingCharacter);
+ }
+ }
+
+ /**
+ * Return a lower case, non-spaced version of the specified name.
+ */
+ public static String simpleName(final String name) {
+ final int len = name.length();
+ final StringBuffer sb = new StringBuffer(len);
+ for (int pos = 0; pos < len; pos++) {
+ final char ch = name.charAt(pos);
+ if (ch == ' ') {
+ continue;
+ }
+ sb.append(Character.toLowerCase(ch));
+ }
+ return sb.toString();
+ }
+
+ /**
+ * Returns a word spaced version of the specified name, so there are spaces
+ * between the words, where each word starts with a capital letter. E.g.,
+ * "NextAvailableDate" is returned as "Next Available Date".
+ */
+ public static String naturalName(final String name) {
+
+ final int length = name.length();
+
+ if (length <= 1) {
+ return name.toUpperCase();// ensure first character is upper case
+ }
+
+ final StringBuffer naturalName = new StringBuffer(length);
+
+ char previousCharacter;
+ char character = Character.toUpperCase(name.charAt(0));// ensure first
+ // character is
+ // upper case
+ naturalName.append(character);
+ char nextCharacter = name.charAt(1);
+
+ for (int pos = 2; pos < length; pos++) {
+ previousCharacter = character;
+ character = nextCharacter;
+ nextCharacter = name.charAt(pos);
+
+ if (previousCharacter != NameUtils.SPACE) {
+ if (Character.isUpperCase(character) && !Character.isUpperCase(previousCharacter)) {
+ naturalName.append(NameUtils.SPACE);
+ }
+ if (Character.isUpperCase(character) && Character.isLowerCase(nextCharacter) && Character.isUpperCase(previousCharacter)) {
+ naturalName.append(NameUtils.SPACE);
+ }
+ if (Character.isDigit(character) && !Character.isDigit(previousCharacter)) {
+ naturalName.append(NameUtils.SPACE);
+ }
+ }
+ naturalName.append(character);
+ }
+ naturalName.append(nextCharacter);
+ return naturalName.toString();
+ }
+
+ public static String pluralName(final String name) {
+ String pluralName;
+ if (name.endsWith("y")) {
+ pluralName = name.substring(0, name.length() - 1) + "ies";
+ } else if (name.endsWith("s") || name.endsWith("x")) {
+ pluralName = name + "es";
+ } else {
+ pluralName = name + 's';
+ }
+ return pluralName;
+ }
+}
http://git-wip-us.apache.org/repos/asf/isis/blob/e4735c72/framework/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/PathUtils.java
----------------------------------------------------------------------
diff --git a/framework/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/PathUtils.java b/framework/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/PathUtils.java
new file mode 100644
index 0000000..a2e7447
--- /dev/null
+++ b/framework/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/PathUtils.java
@@ -0,0 +1,47 @@
+/*
+ * 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.isis.core.commons.lang;
+
+public final class PathUtils {
+
+ private PathUtils() {
+ }
+
+ public static String combine(final String path, final String suffix) {
+ if (isEmpty(path) && isEmpty(suffix)) {
+ return "";
+ }
+ if (isEmpty(path)) {
+ return suffix;
+ }
+ if (isEmpty(suffix)) {
+ return path;
+ }
+ if (path.endsWith("/") || suffix.startsWith("/")) {
+ return path + suffix;
+ }
+ return path + "/" + suffix;
+ }
+
+ private static boolean isEmpty(final String str) {
+ return str == null || str.length() == 0;
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/isis/blob/e4735c72/framework/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/Resources.java
----------------------------------------------------------------------
diff --git a/framework/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/Resources.java b/framework/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/Resources.java
new file mode 100644
index 0000000..d4eb4e1
--- /dev/null
+++ b/framework/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/Resources.java
@@ -0,0 +1,122 @@
+/*
+ * 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.isis.core.commons.lang;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.util.Properties;
+
+/**
+ * Adapted from Ibatis Common.
+ */
+public class Resources {
+
+ /**
+ * Returns the URL, or null if not available.
+ */
+ public static URL getResourceURL(final String resource) {
+
+ // try thread's classloader
+ ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
+ URL url = classLoader.getResource(resource);
+ if (url != null) {
+ return url;
+ }
+
+ // try this class' classloader
+ classLoader = Resources.class.getClassLoader();
+ url = classLoader.getResource(resource);
+ if (url != null) {
+ return url;
+ }
+
+ // try system class loader (could return null)
+ // wrapping in a try...catch because when running tests by Maven for a
+ // non-existing
+ // resource, seems to bomb out. Is okay when run from Eclipse. A bit of
+ // a puzzle.
+ try {
+ return ClassLoader.getSystemResource(resource);
+ } catch (final NullPointerException ignore) {
+ return null;
+ }
+ }
+
+ public static InputStream getResourceAsStream(final String resource) {
+
+ // try thread's classloader
+ ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
+ InputStream is = classLoader.getResourceAsStream(resource);
+ if (is != null) {
+ return is;
+ }
+
+ // try this class' classloader
+ classLoader = Resources.class.getClassLoader();
+ is = classLoader.getResourceAsStream(resource);
+ if (is != null) {
+ return is;
+ }
+
+ // try system class loader (could return null)
+ // have wrapped in a try...catch because for same reason as
+ // getResourceURL
+ try {
+ return ClassLoader.getSystemResourceAsStream(resource);
+ } catch (final NullPointerException ignore) {
+ return null;
+ }
+ }
+
+ public static File getResourceAsFile(final String resource) {
+ final URL url = getResourceURL(resource);
+ if (url == null) {
+ return null;
+ }
+
+ return new File(url.getFile());
+ }
+
+ public static Properties getResourceAsProperties(final String resource) {
+
+ final InputStream is = getResourceAsStream(resource);
+ if (is == null) {
+ return null;
+ }
+
+ final Properties props = new Properties();
+ try {
+ props.load(is);
+ } catch (final IOException ex) {
+ throw new RuntimeException(ex);
+ } finally {
+ try {
+ is.close();
+ } catch (final IOException ignore) {
+ // ignore
+ }
+ }
+
+ return props;
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/isis/blob/e4735c72/framework/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/StringBuilderUtils.java
----------------------------------------------------------------------
diff --git a/framework/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/StringBuilderUtils.java b/framework/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/StringBuilderUtils.java
new file mode 100644
index 0000000..4f73f87
--- /dev/null
+++ b/framework/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/StringBuilderUtils.java
@@ -0,0 +1,36 @@
+/*
+ * 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.isis.core.commons.lang;
+
+public class StringBuilderUtils {
+
+ public static void appendBuf(final StringBuilder buf, final String formatString, final Object... args) {
+ buf.append(String.format(formatString, args));
+ }
+
+ /**
+ * @deprecated - use a {@link StringBuilder} instead!
+ */
+ @Deprecated
+ public static void appendBuf(final StringBuffer buf, final String formatString, final Object... args) {
+ buf.append(String.format(formatString, args));
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/isis/blob/e4735c72/framework/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/StringUtils.java
----------------------------------------------------------------------
diff --git a/framework/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/StringUtils.java b/framework/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/StringUtils.java
new file mode 100644
index 0000000..185425b
--- /dev/null
+++ b/framework/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/StringUtils.java
@@ -0,0 +1,374 @@
+/*
+ * 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.isis.core.commons.lang;
+
+import java.io.File;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.StringTokenizer;
+
+import com.google.common.collect.Lists;
+
+import org.apache.isis.applib.util.Enums;
+
+public final class StringUtils {
+ private StringUtils() {
+ }
+
+ // ////////////////////////////////////////////////////////////
+ // naturalName, naturalize, simpleName, camel, memberIdFor
+ // ////////////////////////////////////////////////////////////
+
+ public static String naturalName(final String name) {
+
+ int pos = 0;
+
+ // find first upper case character
+ while ((pos < name.length()) && Character.isLowerCase(name.charAt(pos))) {
+ pos++;
+ }
+
+ if (pos == name.length()) {
+ return "invalid name";
+ }
+ return naturalize(name, pos);
+ }
+
+ public static String naturalize(final String name) {
+ return naturalize(name, 0);
+ }
+
+ private static String naturalize(final String name, final int startingPosition) {
+ if (name.length() <= startingPosition) {
+ throw new IllegalArgumentException("string shorter than starting position provided");
+ }
+ final StringBuffer s = new StringBuffer(name.length() - startingPosition);
+ for (int j = startingPosition; j < name.length(); j++) { // process
+ // english name
+ // - add spaces
+ if ((j > startingPosition) && isStartOfNewWord(name.charAt(j), name.charAt(j - 1))) {
+ s.append(' ');
+ }
+ if (j == startingPosition) {
+ s.append(Character.toUpperCase(name.charAt(j)));
+ } else {
+ s.append(name.charAt(j));
+ }
+ }
+ final String str = s.toString();
+ return str;
+ }
+
+ private static boolean isStartOfNewWord(final char c, final char previousChar) {
+ return Character.isUpperCase(c) || Character.isDigit(c) && !Character.isDigit(previousChar);
+ }
+
+ public static String simpleName(final String str) {
+ final int lastDot = str.lastIndexOf('.');
+ if (lastDot == -1) {
+ return str;
+ }
+ if (lastDot == str.length() - 1) {
+ throw new IllegalArgumentException("Name cannot end in '.'");
+ }
+ return str.substring(lastDot + 1);
+ }
+
+ public static String camel(final String name) {
+ final StringBuffer b = new StringBuffer(name.length());
+ final StringTokenizer t = new StringTokenizer(name);
+ b.append(t.nextToken());
+ while (t.hasMoreTokens()) {
+ final String token = t.nextToken();
+ b.append(token.substring(0, 1).toUpperCase()); // replace spaces
+ // with
+ // camelCase
+ b.append(token.substring(1));
+ }
+ return b.toString();
+ }
+
+ // TODO: combine with camel
+ public static String camelLowerFirst(final String name) {
+ final StringBuffer b = new StringBuffer(name.length());
+ final StringTokenizer t = new StringTokenizer(name);
+ b.append(lowerFirst(t.nextToken()));
+ while (t.hasMoreTokens()) {
+ final String token = t.nextToken();
+ b.append(token.substring(0, 1).toUpperCase()); // replace spaces
+ // with camelCase
+ b.append(token.substring(1).toLowerCase());
+ }
+ return b.toString();
+ }
+
+ public static String toLowerDashed(String name) {
+ return name.toLowerCase().replaceAll("\\s+", "-");
+ }
+
+ public static String pascal(final String name) {
+ return capitalize(camel(name));
+ }
+
+ public static String memberIdFor(final String member) {
+ return lowerLeading(camel(member));
+ }
+
+ // ////////////////////////////////////////////////////////////
+ // capitalize, lowerFirst, firstWord
+ // ////////////////////////////////////////////////////////////
+
+ public static String capitalize(final String str) {
+ if (str == null || str.length() == 0) {
+ return str;
+ }
+ if (str.length() == 1) {
+ return str.toUpperCase();
+ }
+ return Character.toUpperCase(str.charAt(0)) + str.substring(1);
+ }
+
+ /**
+ * Simply forces first char to be lower case.
+ */
+ public static String lowerFirst(final String str) {
+ if (isNullOrEmpty(str)) {
+ return str;
+ }
+ if (str.length() == 1) {
+ return str.toLowerCase();
+ }
+ return str.substring(0, 1).toLowerCase() + str.substring(1);
+ }
+
+ public static String lowerLeading(final String str) {
+ return lowerFirst(str);
+ }
+
+ public static String firstWord(final String line) {
+ final String[] split = line.split(" ");
+ return split[0];
+ }
+
+ // ////////////////////////////////////////////////////////////
+ // isNullOrEmpty, nullSafeEquals
+ // ////////////////////////////////////////////////////////////
+
+ public static boolean isNullOrEmpty(final String str) {
+ return str == null || str.isEmpty();
+ }
+
+ public static boolean nullSafeEquals(final String str1, final String str2) {
+ if (str1 == null || str2 == null) {
+ return false;
+ }
+ return str1.equals(str2);
+ }
+
+ // ////////////////////////////////////////////////////////////
+ // in, combine, combinePaths, splitOnCommas
+ // ////////////////////////////////////////////////////////////
+
+ public static boolean in(final String str, final String[] strings) {
+ for (final String strCandidate : strings) {
+ if (strCandidate.equals(str)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ public static String combine(final List<String> list) {
+ final StringBuffer buf = new StringBuffer();
+ for (final String message : list) {
+ if (list.size() > 1) {
+ buf.append("; ");
+ }
+ buf.append(message);
+ }
+ return buf.toString();
+ }
+
+ public static String combinePaths(final String path, final String... furtherPaths) {
+ final StringBuilder buf = new StringBuilder(path);
+ for (final String furtherPath : furtherPaths) {
+ if (buf.charAt(buf.length() - 1) != File.separatorChar) {
+ buf.append(File.separatorChar);
+ }
+ buf.append(furtherPath);
+ }
+ return buf.toString();
+ }
+
+ public static List<String> splitOnCommas(final String commaSeparatedList) {
+ if (commaSeparatedList == null) {
+ return null;
+ }
+ final String removeLeadingWhiteSpace = removeLeadingWhiteSpace(commaSeparatedList);
+ // special handling
+ if (removeLeadingWhiteSpace.length() == 0) {
+ return Collections.emptyList();
+ }
+ final String[] splitAsArray = removeLeadingWhiteSpace.split("\\W*,\\W*");
+ return Arrays.asList(splitAsArray);
+ }
+
+ // ////////////////////////////////////////////////////////////
+ // commaSeparatedClassNames
+ // ////////////////////////////////////////////////////////////
+
+ public static String commaSeparatedClassNames(final List<Object> objects) {
+ final StringBuilder buf = new StringBuilder();
+ int i = 0;
+ for (final Object object : objects) {
+ if (i++ > 0) {
+ buf.append(',');
+ }
+ buf.append(object.getClass().getName());
+ }
+ return buf.toString();
+ }
+
+ private static final char CARRIAGE_RETURN = '\n';
+ private static final char LINE_FEED = '\r';
+
+ /**
+ * Converts any <tt>\n</tt> to <tt>line.separator</tt>
+ *
+ * @param string
+ * @return
+ */
+ public static String lineSeparated(final String string) {
+ final StringBuilder buf = new StringBuilder();
+ final String lineSeparator = System.getProperty("line.separator");
+ boolean lastWasLineFeed = false;
+ for (final char c : string.toCharArray()) {
+ final boolean isLineFeed = c == LINE_FEED;
+ final boolean isCarriageReturn = c == CARRIAGE_RETURN;
+ if (isCarriageReturn) {
+ buf.append(lineSeparator);
+ lastWasLineFeed = false;
+ } else {
+ if (lastWasLineFeed) {
+ buf.append(LINE_FEED);
+ }
+ if (isLineFeed) {
+ lastWasLineFeed = true;
+ } else {
+ buf.append(c);
+ lastWasLineFeed = false;
+ }
+ }
+ }
+ if (lastWasLineFeed) {
+ buf.append(LINE_FEED);
+ }
+ return buf.toString();
+ }
+
+ // ////////////////////////////////////////////////////////////
+ // removeTabs, removeLeadingWhiteSpace, stripLeadingSlash, stripNewLines,
+ // normalize
+ // ////////////////////////////////////////////////////////////
+
+ public static String removeTabs(final String text) {
+ // quick return - jvm java should always return here
+ if (text.indexOf('\t') == -1) {
+ return text;
+ }
+ final StringBuffer buf = new StringBuffer();
+ for (int i = 0; i < text.length(); i++) {
+ // a bit clunky to stay with j# api
+ if (text.charAt(i) != '\t') {
+ buf.append(text.charAt(i));
+ }
+ }
+ return buf.toString();
+ }
+
+ public static String removeLeadingWhiteSpace(final String str) {
+ if (str == null) {
+ return null;
+ }
+ return str.replaceAll("^\\W*", "");
+ }
+
+ public static String stripNewLines(final String str) {
+ return str.replaceAll("[\r\n]", "");
+ }
+
+ public static String stripLeadingSlash(final String path) {
+ if (!path.startsWith("/")) {
+ return path;
+ }
+ if (path.length() < 2) {
+ return "";
+ }
+ return path.substring(1);
+ }
+
+ /**
+ * Condenses any whitespace to a single character
+ *
+ * @param str
+ * @return
+ */
+ public static String normalized(final String str) {
+ if (str == null) {
+ return null;
+ }
+ return str.replaceAll("\\s+", " ");
+ }
+
+ public static String[] normalized(final String... strings) {
+ final List<String> stringList = Lists.newArrayList();
+ for (final String string : strings) {
+ stringList.add(normalized(string));
+ }
+ return stringList.toArray(new String[] {});
+ }
+
+ public static String removePrefix(final String name, final String prefix) {
+ if (name.startsWith(prefix)) {
+ return name.substring(prefix.length());
+ } else {
+ return name;
+ }
+ }
+
+ public static <T> T coalesce(final T... strings) {
+ for (final T str : strings) {
+ if (str != null) {
+ return str;
+ }
+ }
+ return null;
+ }
+
+ public static String enumTitle(String enumName) {
+ return Enums.getFriendlyNameOf(enumName);
+ }
+
+ public static String enumDeTitle(String enumFriendlyName) {
+ return Enums.getEnumNameFromFriendly(enumFriendlyName);
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/isis/blob/e4735c72/framework/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/Threads.java
----------------------------------------------------------------------
diff --git a/framework/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/Threads.java b/framework/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/Threads.java
new file mode 100644
index 0000000..9617cbd
--- /dev/null
+++ b/framework/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/Threads.java
@@ -0,0 +1,33 @@
+/*
+ * 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.isis.core.commons.lang;
+
+public class Threads {
+
+ private Threads() {
+ }
+
+ public static Thread startThread(final Runnable target, final String name) {
+ final Thread thread = new Thread(target, name);
+ thread.start();
+ return thread;
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/isis/blob/e4735c72/framework/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/ThrowableUtils.java
----------------------------------------------------------------------
diff --git a/framework/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/ThrowableUtils.java b/framework/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/ThrowableUtils.java
new file mode 100644
index 0000000..42d4743
--- /dev/null
+++ b/framework/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/ThrowableUtils.java
@@ -0,0 +1,43 @@
+/*
+ * 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.isis.core.commons.lang;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.PrintStream;
+
+public final class ThrowableUtils {
+
+ public static String stackTraceFor(final Throwable exception) {
+ ByteArrayOutputStream baos = null;
+ try {
+ baos = new ByteArrayOutputStream();
+ exception.printStackTrace(new PrintStream(baos));
+ return baos.toString();
+ } finally {
+ if (baos != null) {
+ try {
+ baos.close();
+ } catch (final IOException ignore) {
+ }
+ }
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/isis/blob/e4735c72/framework/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/ToString.java
----------------------------------------------------------------------
diff --git a/framework/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/ToString.java b/framework/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/ToString.java
new file mode 100644
index 0000000..4a7744a
--- /dev/null
+++ b/framework/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/ToString.java
@@ -0,0 +1,181 @@
+/*
+ * 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.isis.core.commons.lang;
+
+import java.text.SimpleDateFormat;
+import java.util.Date;
+
+public final class ToString {
+
+ public static String createName(final Object forObject) {
+ final StringBuffer buffer = new StringBuffer();
+ createName(forObject, buffer);
+ return buffer.toString();
+ }
+
+ private static void createName(final Object forObject, final StringBuffer string) {
+ string.append(name(forObject));
+ string.append("@");
+ string.append(Integer.toHexString(forObject.hashCode()));
+ }
+
+ public static String name(final Object forObject) {
+ final String name = forObject.getClass().getName();
+ return name.substring(name.lastIndexOf('.') + 1);
+ }
+
+ public static String timestamp(final Date date) {
+ final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd-hhmmssSSS");
+ return date == null ? "" : simpleDateFormat.format(date);
+ }
+
+ public static ToString createWithIdentifier(final Object object) {
+ return new ToString(object);
+ }
+
+ public static ToString createAnonymous(final Object object) {
+ final ToString string = new ToString();
+ string.append(name(object));
+ string.append("[");
+ return string;
+ }
+
+ private boolean addComma = false;
+ private final StringBuffer buf;
+ private boolean useLineBreaks;
+
+ private ToString() {
+ buf = new StringBuffer();
+ }
+
+ public ToString(final Object forObject) {
+ buf = new StringBuffer();
+ createName(forObject, buf);
+ buf.append("[");
+ }
+
+ public ToString(final Object forObject, final int id) {
+ buf = new StringBuffer();
+ buf.append(name(forObject));
+ buf.append("#");
+ buf.append(id);
+ buf.append("[");
+ }
+
+ public ToString(final Object forObject, final String text) {
+ this(forObject);
+ buf.append(text);
+ addComma = text.length() > 0;
+ }
+
+ public ToString append(final String text) {
+ buf.append(text);
+ return this;
+ }
+
+ public ToString append(final String name, final boolean flag) {
+ append(name, flag ? "true" : "false");
+ return this;
+ }
+
+ public ToString append(final String name, final byte number) {
+ append(name, Byte.toString(number));
+ return this;
+ }
+
+ public ToString append(final String name, final double number) {
+ append(name, Double.toString(number));
+ return this;
+ }
+
+ public ToString append(final String name, final float number) {
+ append(name, Float.toString(number));
+ return this;
+ }
+
+ public ToString append(final String name, final int number) {
+ append(name, Integer.toString(number));
+ return this;
+ }
+
+ public ToString append(final String name, final long number) {
+ append(name, Long.toString(number));
+ return this;
+ }
+
+ public ToString append(final String name, final Object object) {
+ append(name, object == null ? "null" : object.toString());
+ return this;
+ }
+
+ public ToString append(final String name, final short number) {
+ append(name, Short.toString(number));
+ return this;
+ }
+
+ public ToString append(final String name, final String string) {
+ if (addComma) {
+ this.buf.append(',');
+ if (useLineBreaks) {
+ this.buf.append("\n\t");
+ }
+ } else {
+ addComma = true;
+ }
+ this.buf.append(name);
+ this.buf.append('=');
+ this.buf.append(string);
+
+ return this;
+ }
+
+ public ToString appendAsHex(final String name, final long number) {
+ append(name, "#" + Long.toHexString(number));
+ return this;
+ }
+
+ public void appendAsTimestamp(final String name, final Date date) {
+ final String dateString = timestamp(date);
+ append(name, dateString);
+ }
+
+ public void appendTruncated(final String name, final String string, final int maxLength) {
+ if (string.length() > maxLength) {
+ append(name, string.substring(0, maxLength));
+ append("...");
+ } else {
+ append(name, string);
+ }
+ }
+
+ public void setAddComma() {
+ this.addComma = true;
+ }
+
+ public void setUseLineBreaks(final boolean useLineBreaks) {
+ this.useLineBreaks = useLineBreaks;
+ }
+
+ @Override
+ public String toString() {
+ buf.append(']');
+ return buf.toString();
+ }
+}
http://git-wip-us.apache.org/repos/asf/isis/blob/e4735c72/framework/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/Types.java
----------------------------------------------------------------------
diff --git a/framework/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/Types.java b/framework/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/Types.java
new file mode 100644
index 0000000..781cf17
--- /dev/null
+++ b/framework/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/Types.java
@@ -0,0 +1,58 @@
+/*
+ * 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.isis.core.commons.lang;
+
+import java.util.Collection;
+import java.util.List;
+
+import com.google.common.base.Function;
+import com.google.common.base.Predicate;
+import com.google.common.collect.Collections2;
+
+public final class Types {
+
+ private Types(){}
+
+ public static <T> Collection<T> filtered(final List<Object> candidates, final Class<T> type) {
+ return Collections2.transform(
+ Collections2.filter(candidates, Types.isOfType(type)),
+ Types.castTo(type));
+ }
+
+ public static final <T> Predicate<Object> isOfType(final Class<T> type) {
+ return new Predicate<Object>() {
+ @Override
+ public boolean apply(Object input) {
+ return type.isAssignableFrom(input.getClass());
+ }
+ };
+ }
+
+ public static <T> Function<Object, T> castTo(final Class<T> type) {
+ return new Function<Object, T>() {
+ @SuppressWarnings("unchecked")
+ @Override
+ public T apply(final Object input) {
+ return (T) input;
+ }
+ };
+ }
+
+
+}
http://git-wip-us.apache.org/repos/asf/isis/blob/e4735c72/framework/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/WrapperUtils.java
----------------------------------------------------------------------
diff --git a/framework/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/WrapperUtils.java b/framework/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/WrapperUtils.java
new file mode 100644
index 0000000..f6a3732
--- /dev/null
+++ b/framework/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/WrapperUtils.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.isis.core.commons.lang;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+public final class WrapperUtils {
+
+ private WrapperUtils() {
+ }
+
+ private static Map<Class<?>, Class<?>> wrapperClasses = new HashMap<Class<?>, Class<?>>();
+
+ static {
+ wrapperClasses.put(boolean.class, Boolean.class);
+ wrapperClasses.put(byte.class, Byte.class);
+ wrapperClasses.put(char.class, Character.class);
+ wrapperClasses.put(short.class, Short.class);
+ wrapperClasses.put(int.class, Integer.class);
+ wrapperClasses.put(long.class, Long.class);
+ wrapperClasses.put(float.class, Float.class);
+ wrapperClasses.put(double.class, Double.class);
+ }
+
+ public static Class<?> wrap(final Class<?> primitiveClass) {
+ return wrapperClasses.get(primitiveClass);
+ }
+
+ public static Class<?>[] wrapAsNecessary(final Class<?>[] classes) {
+ final List<Class<?>> wrappedClasses = new ArrayList<Class<?>>();
+ for (final Class<?> cls : classes) {
+ if (cls.isPrimitive()) {
+ wrappedClasses.add(wrap(cls));
+ } else {
+ wrappedClasses.add(cls);
+ }
+ }
+ return wrappedClasses.toArray(new Class[] {});
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/isis/blob/e4735c72/framework/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/package-info.java
----------------------------------------------------------------------
diff --git a/framework/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/package-info.java b/framework/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/package-info.java
new file mode 100644
index 0000000..ea099b4
--- /dev/null
+++ b/framework/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/package-info.java
@@ -0,0 +1,27 @@
+/*
+ * 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.
+ */
+
+/**
+ * This package holds a small number of classes to extend the JDK.
+ *
+ * <p>
+ * These classes should be thought of as being in the same spirit as
+ * the google <i>guava</i> library helper classes (and some may be replaced by guava in the future).
+ */
+package org.apache.isis.core.commons.lang;
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/isis/blob/e4735c72/framework/core/metamodel/src/main/java/org/apache/isis/core/commons/matchers/IsisMatchers.java
----------------------------------------------------------------------
diff --git a/framework/core/metamodel/src/main/java/org/apache/isis/core/commons/matchers/IsisMatchers.java b/framework/core/metamodel/src/main/java/org/apache/isis/core/commons/matchers/IsisMatchers.java
new file mode 100644
index 0000000..ffda9cf
--- /dev/null
+++ b/framework/core/metamodel/src/main/java/org/apache/isis/core/commons/matchers/IsisMatchers.java
@@ -0,0 +1,374 @@
+/*
+ * 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.isis.core.commons.matchers;
+
+import static org.hamcrest.CoreMatchers.nullValue;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.List;
+
+import org.apache.isis.core.commons.lang.StringUtils;
+import org.hamcrest.CoreMatchers;
+import org.hamcrest.Description;
+import org.hamcrest.Factory;
+import org.hamcrest.Matcher;
+import org.hamcrest.Matchers;
+import org.hamcrest.TypeSafeMatcher;
+import org.hamcrest.core.IsEqual;
+import org.hamcrest.core.StringContains;
+import org.hamcrest.core.StringEndsWith;
+import org.hamcrest.core.StringStartsWith;
+
+/**
+ * Hamcrest {@link Matcher} implementations.
+ *
+ */
+public final class IsisMatchers {
+
+ private IsisMatchers() {
+ }
+
+ @Factory
+ public static Matcher<String> containsStripNewLines(final String expected) {
+ final String strippedExpected = StringUtils.stripNewLines(expected);
+ return new StringContains(strippedExpected) {
+ @Override
+ public boolean matchesSafely(final String actual) {
+ return super.matchesSafely(StringUtils.stripNewLines(actual));
+ }
+
+ @Override
+ public void describeTo(final Description description) {
+ description.appendText("a string (ignoring new lines) containing").appendValue(strippedExpected);
+ }
+ };
+ }
+
+ @Factory
+ public static Matcher<String> equalToStripNewLines(final String expected) {
+ final String strippedExpected = StringUtils.stripNewLines(expected);
+ return new IsEqual<String>(strippedExpected) {
+ @Override
+ public boolean matches(final Object actualObj) {
+ final String actual = (String) actualObj;
+ return super.matches(StringUtils.stripNewLines(actual));
+ }
+
+ @Override
+ public void describeTo(final Description description) {
+ description.appendText("a string (ignoring new lines) equal to").appendValue(strippedExpected);
+ }
+ };
+ }
+
+ @Factory
+ public static StringStartsWith startsWithStripNewLines(final String expected) {
+ final String strippedExpected = StringUtils.stripNewLines(expected);
+ return new StringStartsWith(strippedExpected) {
+ @Override
+ public boolean matchesSafely(final String actual) {
+ return super.matchesSafely(StringUtils.stripNewLines(actual));
+ }
+
+ @Override
+ public void describeTo(final Description description) {
+ description.appendText("a string (ignoring new lines) starting with").appendValue(strippedExpected);
+ }
+ };
+ }
+
+ @Factory
+ public static Matcher<String> endsWithStripNewLines(final String expected) {
+ final String strippedExpected = StringUtils.stripNewLines(expected);
+ return new StringEndsWith(strippedExpected) {
+ @Override
+ public boolean matchesSafely(final String actual) {
+ return super.matchesSafely(StringUtils.stripNewLines(actual));
+ }
+
+ @Override
+ public void describeTo(final Description description) {
+ description.appendText("a string (ignoring new lines) ending with").appendValue(strippedExpected);
+ }
+ };
+ }
+
+ @Factory
+ public static <T> Matcher<T> anInstanceOf(final Class<T> expected) {
+ return new TypeSafeMatcher<T>() {
+ @Override
+ public boolean matchesSafely(final T actual) {
+ return expected.isAssignableFrom(actual.getClass());
+ }
+
+ @Override
+ public void describeTo(final Description description) {
+ description.appendText("an instance of ").appendValue(expected);
+ }
+ };
+ }
+
+ @Factory
+ public static Matcher<String> nonEmptyString() {
+ return new TypeSafeMatcher<String>() {
+ @Override
+ public boolean matchesSafely(final String str) {
+ return str != null && str.length() > 0;
+ }
+
+ @Override
+ public void describeTo(final Description description) {
+ description.appendText("a non empty string");
+ }
+
+ };
+ }
+
+ @Factory
+ @SuppressWarnings("unchecked")
+ public static Matcher<String> nonEmptyStringOrNull() {
+ return CoreMatchers.anyOf(nullValue(String.class), nonEmptyString());
+ }
+
+ @Factory
+ public static Matcher<List<?>> containsElementThat(final Matcher<?> elementMatcher) {
+ return new TypeSafeMatcher<List<?>>() {
+ @Override
+ public boolean matchesSafely(final List<?> list) {
+ for (final Object o : list) {
+ if (elementMatcher.matches(o)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ @Override
+ public void describeTo(final Description description) {
+ description.appendText("contains element that ").appendDescriptionOf(elementMatcher);
+ }
+ };
+ }
+
+ @Factory
+ public static <T extends Comparable<T>> Matcher<T> greaterThan(final T c) {
+ return Matchers.greaterThan(c);
+ }
+
+ @Factory
+ public static Matcher<Class<?>> classEqualTo(final Class<?> operand) {
+
+ class ClassEqualsMatcher extends TypeSafeMatcher<Class<?>> {
+ private final Class<?> clazz;
+
+ public ClassEqualsMatcher(final Class<?> clazz) {
+ this.clazz = clazz;
+ }
+
+ @Override
+ public boolean matchesSafely(final Class<?> arg) {
+ return clazz == arg;
+ }
+
+ @Override
+ public void describeTo(final Description description) {
+ description.appendValue(clazz);
+ }
+ }
+
+ return new ClassEqualsMatcher(operand);
+ }
+
+ @Factory
+ public static Matcher<File> existsAndNotEmpty() {
+
+ return new TypeSafeMatcher<File>() {
+
+ @Override
+ public void describeTo(final Description arg0) {
+ arg0.appendText("exists and is not empty");
+ }
+
+ @Override
+ public boolean matchesSafely(final File f) {
+ return f.exists() && f.length() > 0;
+ }
+ };
+ }
+
+ @Factory
+ public static Matcher<String> matches(final String regex) {
+ return new TypeSafeMatcher<String>() {
+
+ @Override
+ public void describeTo(final Description description) {
+ description.appendText("string matching " + regex);
+ }
+
+ @Override
+ public boolean matchesSafely(final String str) {
+ return str.matches(regex);
+ }
+ };
+ }
+
+ @Factory
+ public static <X> Matcher<Class<X>> anySubclassOf(final Class<X> cls) {
+ return new TypeSafeMatcher<Class<X>>() {
+
+ @Override
+ public void describeTo(final Description arg0) {
+ arg0.appendText("is subclass of ").appendText(cls.getName());
+ }
+
+ @Override
+ public boolean matchesSafely(final Class<X> item) {
+ return cls.isAssignableFrom(item);
+ }
+ };
+ }
+
+ @Factory
+ public static <T> Matcher<List<T>> sameContentsAs(final List<T> expected) {
+ return new TypeSafeMatcher<List<T>>() {
+
+ @Override
+ public void describeTo(final Description description) {
+ description.appendText("same sequence as " + expected);
+ }
+
+ @Override
+ public boolean matchesSafely(final List<T> actual) {
+ return actual.containsAll(expected) && expected.containsAll(actual);
+ }
+ };
+ }
+
+ @Factory
+ public static <T> Matcher<List<T>> listContaining(final T t) {
+ return new TypeSafeMatcher<List<T>>() {
+
+ @Override
+ public void describeTo(Description arg0) {
+ arg0.appendText("list containing ").appendValue(t);
+ }
+
+ @Override
+ public boolean matchesSafely(List<T> arg0) {
+ return arg0.contains(t);
+ }
+ };
+ }
+
+ @Factory
+ public static <T> Matcher<List<T>> listContainingAll(final T... items) {
+ return new TypeSafeMatcher<List<T>>() {
+
+ @Override
+ public void describeTo(Description arg0) {
+ arg0.appendText("has items ").appendValue(items);
+
+ }
+
+ @Override
+ public boolean matchesSafely(List<T> arg0) {
+ return arg0.containsAll(Arrays.asList(items));
+ }
+ };
+ }
+
+ @Factory
+ public static Matcher<List<Object>> containsObjectOfType(final Class<?> cls) {
+ return new TypeSafeMatcher<List<Object>>() {
+
+ @Override
+ public void describeTo(final Description desc) {
+ desc.appendText("contains instance of type " + cls.getName());
+ }
+
+ @Override
+ public boolean matchesSafely(final List<Object> items) {
+ for (final Object object : items) {
+ if (cls.isAssignableFrom(object.getClass())) {
+ return true;
+ }
+ }
+ return false;
+ }
+ };
+ }
+
+ @Factory
+ public static Matcher<String> startsWith(final String expected) {
+ return new TypeSafeMatcher<String>() {
+
+ @Override
+ public void describeTo(Description description) {
+ description.appendText(" starts with '" + expected + "'");
+ }
+
+ @Override
+ public boolean matchesSafely(String actual) {
+ return actual.startsWith(expected);
+ }
+ };
+ }
+
+ @Factory
+ public static Matcher<String> contains(final String expected) {
+ return new TypeSafeMatcher<String>() {
+
+ @Override
+ public void describeTo(Description description) {
+ description.appendText(" contains '" + expected + "'");
+ }
+
+ @Override
+ public boolean matchesSafely(String actual) {
+ return actual.contains(expected);
+ }
+ };
+ }
+
+
+ @Factory
+ public static Matcher<File> equalsFile(final File file) throws IOException {
+ final String canonicalPath = file.getCanonicalPath();
+ return new TypeSafeMatcher<File>() {
+
+ @Override
+ public void describeTo(Description arg0) {
+ arg0.appendText("file '" + canonicalPath + "'");
+ }
+
+ @Override
+ public boolean matchesSafely(File arg0) {
+ try {
+ return arg0.getCanonicalPath().equals(canonicalPath);
+ } catch (IOException e) {
+ return false;
+ }
+ }
+ };
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/isis/blob/e4735c72/framework/core/metamodel/src/main/java/org/apache/isis/core/commons/matchers/package-info.java
----------------------------------------------------------------------
diff --git a/framework/core/metamodel/src/main/java/org/apache/isis/core/commons/matchers/package-info.java b/framework/core/metamodel/src/main/java/org/apache/isis/core/commons/matchers/package-info.java
new file mode 100644
index 0000000..ffa03c2
--- /dev/null
+++ b/framework/core/metamodel/src/main/java/org/apache/isis/core/commons/matchers/package-info.java
@@ -0,0 +1,24 @@
+/*
+ * 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.
+ */
+
+/**
+ * Provides a set of Hamcrest {@link org.hamcrest.Matcher}s for various
+ * purposes.
+ */
+package org.apache.isis.core.commons.matchers;
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/isis/blob/e4735c72/framework/core/metamodel/src/main/java/org/apache/isis/core/commons/resource/ResourceStreamSource.java
----------------------------------------------------------------------
diff --git a/framework/core/metamodel/src/main/java/org/apache/isis/core/commons/resource/ResourceStreamSource.java b/framework/core/metamodel/src/main/java/org/apache/isis/core/commons/resource/ResourceStreamSource.java
new file mode 100644
index 0000000..d1d7e84
--- /dev/null
+++ b/framework/core/metamodel/src/main/java/org/apache/isis/core/commons/resource/ResourceStreamSource.java
@@ -0,0 +1,41 @@
+/*
+ * 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.isis.core.commons.resource;
+
+import java.io.InputStream;
+import java.io.OutputStream;
+
+public interface ResourceStreamSource {
+
+ /**
+ * Returns an {@link InputStream} opened for the resource path, or
+ * <tt>null</tt> otherwise.
+ */
+ public InputStream readResource(String resourcePath);
+
+ /**
+ * Returns an {@link OutputStream} opened to write to the resource, or
+ * <tt>null</tt> otherwise.
+ */
+ public OutputStream writeResource(String resourcePath);
+
+ public String getName();
+
+}
http://git-wip-us.apache.org/repos/asf/isis/blob/e4735c72/framework/core/metamodel/src/main/java/org/apache/isis/core/commons/resource/ResourceStreamSourceAbstract.java
----------------------------------------------------------------------
diff --git a/framework/core/metamodel/src/main/java/org/apache/isis/core/commons/resource/ResourceStreamSourceAbstract.java b/framework/core/metamodel/src/main/java/org/apache/isis/core/commons/resource/ResourceStreamSourceAbstract.java
new file mode 100644
index 0000000..6f2c991
--- /dev/null
+++ b/framework/core/metamodel/src/main/java/org/apache/isis/core/commons/resource/ResourceStreamSourceAbstract.java
@@ -0,0 +1,69 @@
+/*
+ * 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.isis.core.commons.resource;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+import org.apache.log4j.Logger;
+
+public abstract class ResourceStreamSourceAbstract implements ResourceStreamSource {
+
+ private static Logger LOG = Logger.getLogger(ResourceStreamSourceAbstract.class);
+
+ @Override
+ public final InputStream readResource(final String resourcePath) {
+
+ try {
+ final InputStream resourceStream = doReadResource(resourcePath);
+ if (resourceStream != null) {
+ return resourceStream;
+ }
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("could not load resource path '" + resourcePath + "' from " + getName());
+ }
+ } catch (final IOException e) {
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("could not load resource path '" + resourcePath + "' from " + getName() + ", exception: " + e.getMessage());
+ }
+ }
+ return null;
+ }
+
+ /**
+ * Mandatory hook method; subclasses can return either <tt>null</tt> or
+ * throw an exception if the resource could not be found.
+ */
+ protected abstract InputStream doReadResource(String resourcePath) throws IOException;
+
+ /**
+ * Default implementation returns <tt>null</tt> (that is, not supported).
+ */
+ @Override
+ public OutputStream writeResource(final String resourcePath) {
+ return null;
+ }
+
+ @Override
+ public String toString() {
+ return getName();
+ }
+}
http://git-wip-us.apache.org/repos/asf/isis/blob/e4735c72/framework/core/metamodel/src/main/java/org/apache/isis/core/commons/resource/ResourceStreamSourceChainOfResponsibility.java
----------------------------------------------------------------------
diff --git a/framework/core/metamodel/src/main/java/org/apache/isis/core/commons/resource/ResourceStreamSourceChainOfResponsibility.java b/framework/core/metamodel/src/main/java/org/apache/isis/core/commons/resource/ResourceStreamSourceChainOfResponsibility.java
new file mode 100644
index 0000000..ee3e46a
--- /dev/null
+++ b/framework/core/metamodel/src/main/java/org/apache/isis/core/commons/resource/ResourceStreamSourceChainOfResponsibility.java
@@ -0,0 +1,89 @@
+/*
+ * 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.isis.core.commons.resource;
+
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.log4j.Logger;
+
+public class ResourceStreamSourceChainOfResponsibility extends ResourceStreamSourceAbstract {
+
+ private static Logger LOG = Logger.getLogger(ResourceStreamSourceChainOfResponsibility.class);
+
+ private final List<ResourceStreamSource> resourceStreamSources = new ArrayList<ResourceStreamSource>();
+
+ public ResourceStreamSourceChainOfResponsibility(final ResourceStreamSource... resourceStreamSources) {
+ for (final ResourceStreamSource rss : resourceStreamSources) {
+ addResourceStreamSource(rss);
+ }
+ }
+
+ public void addResourceStreamSource(final ResourceStreamSource rss) {
+ this.resourceStreamSources.add(rss);
+ }
+
+ @Override
+ protected InputStream doReadResource(final String resourcePath) {
+ for (final ResourceStreamSource rss : resourceStreamSources) {
+ final InputStream resourceStream = rss.readResource(resourcePath);
+ if (resourceStream != null) {
+ return resourceStream;
+ }
+ }
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("could not load resource path '" + resourcePath + "' from " + getName());
+ }
+ return null;
+ }
+
+ @Override
+ public OutputStream writeResource(final String resourcePath) {
+ for (final ResourceStreamSource rss : resourceStreamSources) {
+ final OutputStream os = rss.writeResource(resourcePath);
+ if (os != null) {
+ return os;
+ }
+ }
+ return null;
+ }
+
+ @Override
+ public String getName() {
+ return "chain [" + resourceStreamNames() + "]";
+ }
+
+ private String resourceStreamNames() {
+ final StringBuilder buf = new StringBuilder();
+ boolean first = true;
+ for (final ResourceStreamSource rss : resourceStreamSources) {
+ if (first) {
+ first = false;
+ } else {
+ buf.append(", ");
+ }
+ buf.append(rss.getName());
+ }
+ return buf.toString();
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/isis/blob/e4735c72/framework/core/metamodel/src/main/java/org/apache/isis/core/commons/resource/ResourceStreamSourceComposite.java
----------------------------------------------------------------------
diff --git a/framework/core/metamodel/src/main/java/org/apache/isis/core/commons/resource/ResourceStreamSourceComposite.java b/framework/core/metamodel/src/main/java/org/apache/isis/core/commons/resource/ResourceStreamSourceComposite.java
new file mode 100644
index 0000000..ffed13a
--- /dev/null
+++ b/framework/core/metamodel/src/main/java/org/apache/isis/core/commons/resource/ResourceStreamSourceComposite.java
@@ -0,0 +1,89 @@
+/*
+ * 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.isis.core.commons.resource;
+
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.SequenceInputStream;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Vector;
+
+import org.apache.log4j.Logger;
+
+public class ResourceStreamSourceComposite extends ResourceStreamSourceAbstract {
+
+ private static Logger LOG = Logger.getLogger(ResourceStreamSourceComposite.class);
+
+ private final List<ResourceStreamSource> resourceStreamSources = new ArrayList<ResourceStreamSource>();
+
+ public ResourceStreamSourceComposite(final ResourceStreamSource... resourceStreamSources) {
+ for (final ResourceStreamSource rss : resourceStreamSources) {
+ addResourceStreamSource(rss);
+ }
+ }
+
+ public void addResourceStreamSource(final ResourceStreamSource rss) {
+ this.resourceStreamSources.add(rss);
+ }
+
+ @Override
+ protected InputStream doReadResource(final String resourcePath) {
+ Vector<InputStream> compositionStreams = new Vector<InputStream>();
+ for (final ResourceStreamSource rss : resourceStreamSources) {
+ final InputStream resourceStream = rss.readResource(resourcePath);
+ if (resourceStream != null) {
+ compositionStreams.add(resourceStream);
+ }
+ }
+ if (!compositionStreams.isEmpty()) {
+ return new SequenceInputStream(compositionStreams.elements());
+ }
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("could not load resource path '" + resourcePath + "' from " + getName());
+ }
+ return null;
+ }
+
+ @Override
+ public OutputStream writeResource(final String resourcePath) {
+ return null; // No support for writing resources
+ }
+
+ @Override
+ public String getName() {
+ return "composite [" + resourceStreamNames() + "]";
+ }
+
+ private String resourceStreamNames() {
+ final StringBuilder buf = new StringBuilder();
+ boolean first = true;
+ for (final ResourceStreamSource rss : resourceStreamSources) {
+ if (first) {
+ first = false;
+ } else {
+ buf.append(", ");
+ }
+ buf.append(rss.getName());
+ }
+ return buf.toString();
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/isis/blob/e4735c72/framework/core/metamodel/src/main/java/org/apache/isis/core/commons/resource/ResourceStreamSourceContextLoaderClassPath.java
----------------------------------------------------------------------
diff --git a/framework/core/metamodel/src/main/java/org/apache/isis/core/commons/resource/ResourceStreamSourceContextLoaderClassPath.java b/framework/core/metamodel/src/main/java/org/apache/isis/core/commons/resource/ResourceStreamSourceContextLoaderClassPath.java
new file mode 100644
index 0000000..91ed777
--- /dev/null
+++ b/framework/core/metamodel/src/main/java/org/apache/isis/core/commons/resource/ResourceStreamSourceContextLoaderClassPath.java
@@ -0,0 +1,62 @@
+/*
+ * 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.isis.core.commons.resource;
+
+import java.io.InputStream;
+
+import org.apache.isis.core.commons.lang.PathUtils;
+
+/**
+ * Loads the properties from the ContextClassLoader.
+ *
+ * <p>
+ * If this class is on the system class path, then the class loader obtained
+ * from this.getClassLoader() won't be able to load resources from the
+ * application class path.
+ */
+public class ResourceStreamSourceContextLoaderClassPath extends ResourceStreamSourceAbstract {
+
+ public static ResourceStreamSourceContextLoaderClassPath create() {
+ return create("");
+ }
+
+ public static ResourceStreamSourceContextLoaderClassPath create(final String prefix) {
+ return new ResourceStreamSourceContextLoaderClassPath(prefix);
+ }
+
+ private final String prefix;
+
+ private ResourceStreamSourceContextLoaderClassPath(final String prefix) {
+ this.prefix = prefix;
+ }
+
+ @Override
+ protected InputStream doReadResource(final String resourcePath) {
+ final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
+ final String path = PathUtils.combine(prefix, resourcePath);
+ return classLoader.getResourceAsStream(path);
+ }
+
+ @Override
+ public String getName() {
+ return "context loader classpath";
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/isis/blob/e4735c72/framework/core/metamodel/src/main/java/org/apache/isis/core/commons/resource/ResourceStreamSourceCurrentClassClassPath.java
----------------------------------------------------------------------
diff --git a/framework/core/metamodel/src/main/java/org/apache/isis/core/commons/resource/ResourceStreamSourceCurrentClassClassPath.java b/framework/core/metamodel/src/main/java/org/apache/isis/core/commons/resource/ResourceStreamSourceCurrentClassClassPath.java
new file mode 100644
index 0000000..c5fddc9
--- /dev/null
+++ b/framework/core/metamodel/src/main/java/org/apache/isis/core/commons/resource/ResourceStreamSourceCurrentClassClassPath.java
@@ -0,0 +1,46 @@
+/*
+ * 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.isis.core.commons.resource;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+/**
+ * Load from this class' ClassLoader.
+ *
+ * <p>
+ * This is useful if (a) the contextClassLoader is not available and (b) the
+ * contextClassLoader does not load from this classes ClassLoader (for example
+ * when running under Ant while running unit tests as part of an automated
+ * build).
+ */
+public class ResourceStreamSourceCurrentClassClassPath extends ResourceStreamSourceAbstract {
+
+ @Override
+ protected InputStream doReadResource(final String resourcePath) throws IOException {
+ return getClass().getClassLoader().getResourceAsStream(resourcePath);
+ }
+
+ @Override
+ public String getName() {
+ return "current class' classpath";
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/isis/blob/e4735c72/framework/core/metamodel/src/main/java/org/apache/isis/core/commons/resource/ResourceStreamSourceFileSystem.java
----------------------------------------------------------------------
diff --git a/framework/core/metamodel/src/main/java/org/apache/isis/core/commons/resource/ResourceStreamSourceFileSystem.java b/framework/core/metamodel/src/main/java/org/apache/isis/core/commons/resource/ResourceStreamSourceFileSystem.java
new file mode 100644
index 0000000..1c497fb
--- /dev/null
+++ b/framework/core/metamodel/src/main/java/org/apache/isis/core/commons/resource/ResourceStreamSourceFileSystem.java
@@ -0,0 +1,65 @@
+/*
+ * 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.isis.core.commons.resource;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+public class ResourceStreamSourceFileSystem extends ResourceStreamSourceAbstract {
+
+ /**
+ * Factory method to guard against nulls
+ */
+ public static ResourceStreamSource create(final String directory2) {
+ return directory2 != null ? new ResourceStreamSourceFileSystem(directory2) : null;
+ }
+
+ private final String directory;
+
+ public ResourceStreamSourceFileSystem(final String directory) {
+ this.directory = directory;
+ }
+
+ @Override
+ protected InputStream doReadResource(final String resourcePath) throws FileNotFoundException {
+ final File file = new File(directory, resourcePath);
+ return new FileInputStream(file);
+ }
+
+ @Override
+ public OutputStream writeResource(final String resourcePath) {
+ final File file = new File(directory, resourcePath);
+ try {
+ return new FileOutputStream(file);
+ } catch (final FileNotFoundException e) {
+ return null;
+ }
+ }
+
+ @Override
+ public String getName() {
+ return "file system (directory '" + directory + "')";
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/isis/blob/e4735c72/framework/core/metamodel/src/main/java/org/apache/isis/core/commons/resource/package-info.java
----------------------------------------------------------------------
diff --git a/framework/core/metamodel/src/main/java/org/apache/isis/core/commons/resource/package-info.java b/framework/core/metamodel/src/main/java/org/apache/isis/core/commons/resource/package-info.java
new file mode 100644
index 0000000..ae647b8
--- /dev/null
+++ b/framework/core/metamodel/src/main/java/org/apache/isis/core/commons/resource/package-info.java
@@ -0,0 +1,34 @@
+/*
+ * 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.
+ */
+
+/**
+ * Provides an {@link org.apache.isis.core.commons.resource.ResourceStreamSource abstraction}
+ * over {@link java.io.InputStream}s for accessing
+ * resources.
+ *
+ * <p>
+ * The principle usage is to allow the easy searching of a single resource (eg
+ * <tt>isis.properties</tt> file) in {@link org.apache.isis.core.commons.resource.ResourceStreamSourceContextLoaderClassPath multiple}
+ * {@link org.apache.isis.core.commons.resource.ResourceStreamSourceCurrentClassClassPath locations}
+ * and {@link org.apache.isis.core.commons.resource.ResourceStreamSourceFileSystem technologies}
+ * on the classpath through
+ * the use of a {@link org.apache.isis.core.commons.resource.ResourceStreamSourceChainOfResponsibility composite}
+ * pattern.
+ */
+package org.apache.isis.core.commons.resource;
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/isis/blob/e4735c72/framework/core/metamodel/src/main/java/org/apache/isis/core/commons/url/UrlEncodingUtils.java
----------------------------------------------------------------------
diff --git a/framework/core/metamodel/src/main/java/org/apache/isis/core/commons/url/UrlEncodingUtils.java b/framework/core/metamodel/src/main/java/org/apache/isis/core/commons/url/UrlEncodingUtils.java
new file mode 100644
index 0000000..1fb8e0a
--- /dev/null
+++ b/framework/core/metamodel/src/main/java/org/apache/isis/core/commons/url/UrlEncodingUtils.java
@@ -0,0 +1,58 @@
+/*
+ * 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.isis.core.commons.url;
+
+import java.io.UnsupportedEncodingException;
+import java.net.URLDecoder;
+import java.net.URLEncoder;
+
+import com.google.common.base.Charsets;
+import com.google.common.base.Function;
+
+public final class UrlEncodingUtils {
+
+ public final static Function<String, String> FUNCTION = new Function<String, String>() {
+
+ @Override
+ public String apply(final String input) {
+ try {
+ return URLDecoder.decode(input, "UTF-8");
+ } catch (final UnsupportedEncodingException e) {
+ return "";
+ }
+ }
+ };
+
+ private UrlEncodingUtils() {
+ }
+
+ public static String urlDecode(final String string) {
+ return FUNCTION.apply(string);
+ }
+
+ public static String urlEncode(final String str) {
+ try {
+ return URLEncoder.encode(str, Charsets.UTF_8.name());
+ } catch (final UnsupportedEncodingException e) {
+ // shouldn't happen
+ throw new RuntimeException(e);
+ }
+ }
+
+}