Lets turn those util classes from apt not being public as otherwise end users may see and use them
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/cacef351 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/cacef351 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/cacef351 Branch: refs/heads/master Commit: cacef3519b0b81cb1dd607536da1705a14137083 Parents: 5f5a29b Author: Claus Ibsen <davscl...@apache.org> Authored: Fri Nov 7 09:10:32 2014 +0100 Committer: Claus Ibsen <davscl...@apache.org> Committed: Fri Nov 7 13:25:20 2014 +0100 ---------------------------------------------------------------------- .../camel/tools/apt/CollectionStringBuffer.java | 59 ++++++ .../tools/apt/EndpointAnnotationProcessor.java | 5 +- .../java/org/apache/camel/tools/apt/Func1.java | 24 +++ .../camel/tools/apt/JsonSchemaHelper.java | 184 ++++++++++++++++++ .../org/apache/camel/tools/apt/Strings.java | 81 ++++++++ .../tools/apt/util/CollectionStringBuffer.java | 59 ------ .../org/apache/camel/tools/apt/util/Func1.java | 24 --- .../camel/tools/apt/util/JsonSchemaHelper.java | 186 ------------------- .../apache/camel/tools/apt/util/Strings.java | 81 -------- 9 files changed, 349 insertions(+), 354 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/cacef351/tooling/apt/src/main/java/org/apache/camel/tools/apt/CollectionStringBuffer.java ---------------------------------------------------------------------- diff --git a/tooling/apt/src/main/java/org/apache/camel/tools/apt/CollectionStringBuffer.java b/tooling/apt/src/main/java/org/apache/camel/tools/apt/CollectionStringBuffer.java new file mode 100644 index 0000000..50f846f --- /dev/null +++ b/tooling/apt/src/main/java/org/apache/camel/tools/apt/CollectionStringBuffer.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.camel.tools.apt; + +/** + /** + * A little helper class for converting a collection of values to a (usually comma separated) string. + */ +class CollectionStringBuffer { + + private final StringBuilder buffer = new StringBuilder(); + private String separator; + private boolean first = true; + + public CollectionStringBuffer() { + this(", "); + } + + public CollectionStringBuffer(String separator) { + this.separator = separator; + } + + @Override + public String toString() { + return buffer.toString(); + } + + public void append(Object value) { + if (first) { + first = false; + } else { + buffer.append(separator); + } + buffer.append(value); + } + + public String getSeparator() { + return separator; + } + + public void setSeparator(String separator) { + this.separator = separator; + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/cacef351/tooling/apt/src/main/java/org/apache/camel/tools/apt/EndpointAnnotationProcessor.java ---------------------------------------------------------------------- diff --git a/tooling/apt/src/main/java/org/apache/camel/tools/apt/EndpointAnnotationProcessor.java b/tooling/apt/src/main/java/org/apache/camel/tools/apt/EndpointAnnotationProcessor.java index 548387e..0db4b60 100644 --- a/tooling/apt/src/main/java/org/apache/camel/tools/apt/EndpointAnnotationProcessor.java +++ b/tooling/apt/src/main/java/org/apache/camel/tools/apt/EndpointAnnotationProcessor.java @@ -48,11 +48,8 @@ import javax.tools.StandardLocation; import org.apache.camel.spi.UriEndpoint; import org.apache.camel.spi.UriParam; import org.apache.camel.spi.UriParams; -import org.apache.camel.tools.apt.util.Func1; -import org.apache.camel.tools.apt.util.JsonSchemaHelper; -import org.apache.camel.tools.apt.util.Strings; -import static org.apache.camel.tools.apt.util.Strings.canonicalClassName; +import static org.apache.camel.tools.apt.Strings.canonicalClassName; /** * Processes all Camel {@link UriEndpoint}s and generate json schema and html documentation for the endpoint/component. http://git-wip-us.apache.org/repos/asf/camel/blob/cacef351/tooling/apt/src/main/java/org/apache/camel/tools/apt/Func1.java ---------------------------------------------------------------------- diff --git a/tooling/apt/src/main/java/org/apache/camel/tools/apt/Func1.java b/tooling/apt/src/main/java/org/apache/camel/tools/apt/Func1.java new file mode 100644 index 0000000..59830ae --- /dev/null +++ b/tooling/apt/src/main/java/org/apache/camel/tools/apt/Func1.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. + */ +package org.apache.camel.tools.apt; + +/** + * Represents a function with 1 argument + */ +interface Func1<T1, R> { + R call(T1 t1); +} http://git-wip-us.apache.org/repos/asf/camel/blob/cacef351/tooling/apt/src/main/java/org/apache/camel/tools/apt/JsonSchemaHelper.java ---------------------------------------------------------------------- diff --git a/tooling/apt/src/main/java/org/apache/camel/tools/apt/JsonSchemaHelper.java b/tooling/apt/src/main/java/org/apache/camel/tools/apt/JsonSchemaHelper.java new file mode 100644 index 0000000..ba60328 --- /dev/null +++ b/tooling/apt/src/main/java/org/apache/camel/tools/apt/JsonSchemaHelper.java @@ -0,0 +1,184 @@ +/** + * 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.camel.tools.apt; + +import java.util.Set; + +/** + * A helper class for <a href="http://json-schema.org/">JSON schema</a>. + */ +final class JsonSchemaHelper { + + private JsonSchemaHelper() { + } + + public static String toJson(String name, String type, String description, boolean enumType, Set<String> enums) { + String typeName = JsonSchemaHelper.getType(type, enumType); + + StringBuilder sb = new StringBuilder(); + sb.append(Strings.doubleQuote(name)); + sb.append(": { \"type\": "); + + if ("enum".equals(typeName)) { + sb.append(Strings.doubleQuote("string")); + sb.append(", \"javaType\": \"" + type + "\""); + CollectionStringBuffer enumValues = new CollectionStringBuffer(); + for (Object value : enums) { + enumValues.append(Strings.doubleQuote(value.toString())); + } + sb.append(", \"enum\": [ "); + sb.append(enumValues.toString()); + sb.append(" ]"); + } else if ("array".equals(typeName)) { + sb.append(Strings.doubleQuote("array")); + sb.append(", \"javaType\": \"" + type + "\""); + } else { + sb.append(Strings.doubleQuote(typeName)); + sb.append(", \"javaType\": \"" + type + "\""); + } + + if (!Strings.isNullOrEmpty(description)) { + sb.append(", \"description\": "); + String text = sanitizeDescription(description); + sb.append(Strings.doubleQuote(text)); + } + + sb.append(" }"); + return sb.toString(); + } + + /** + * Gets the JSon schema type. + * + * @param type the java type + * @return the json schema type, is never null, but returns <tt>object</tt> as the generic type + */ + public static String getType(String type, boolean enumType) { + if (enumType) { + return "enum"; + } + + String primitive = getPrimitiveType(type); + if (primitive != null) { + return primitive; + } + + return "object"; + } + + /** + * Gets the JSon schema primitive type. + * + * @param name the java type + * @return the json schema primitive type, or <tt>null</tt> if not a primitive + */ + public static String getPrimitiveType(String name) { + + // special for byte[] or Object[] as its common to use + if ("java.lang.byte[]".equals(name) || "byte[]".equals(name)) { + return "string"; + } else if ("java.lang.Byte[]".equals(name) || "Byte[]".equals(name)) { + return "array"; + } else if ("java.lang.Object[]".equals(name) || "Object[]".equals(name)) { + return "array"; + } else if ("java.lang.String[]".equals(name) || "String[]".equals(name)) { + return "array"; + // and these is common as well + } else if ("java.lang.String".equals(name) || "String".equals(name)) { + return "string"; + } else if ("java.lang.Boolean".equals(name) || "Boolean".equals(name)) { + return "boolean"; + } else if ("boolean".equals(name)) { + return "boolean"; + } else if ("java.lang.Integer".equals(name) || "Integer".equals(name)) { + return "integer"; + } else if ("int".equals(name)) { + return "integer"; + } else if ("java.lang.Long".equals(name) || "Long".equals(name)) { + return "integer"; + } else if ("long".equals(name)) { + return "integer"; + } else if ("java.lang.Short".equals(name) || "Short".equals(name)) { + return "integer"; + } else if ("short".equals(name)) { + return "integer"; + } else if ("java.lang.Byte".equals(name) || "Byte".equals(name)) { + return "integer"; + } else if ("byte".equals(name)) { + return "integer"; + } else if ("java.lang.Float".equals(name) || "Float".equals(name)) { + return "number"; + } else if ("float".equals(name)) { + return "number"; + } else if ("java.lang.Double".equals(name) || "Double".equals(name)) { + return "number"; + } else if ("double".equals(name)) { + return "number"; + } + + return null; + } + + /** + * Sanitizes the javadoc to removed invalid characters so it can be used as json description + * + * @param javadoc the javadoc + * @return the text that is valid as json + */ + public static String sanitizeDescription(String javadoc) { + // lets just use what java accepts as identifiers + StringBuilder sb = new StringBuilder(); + + // split into lines + String[] lines = javadoc.split("\n"); + + boolean first = true; + for (String line : lines) { + line = line.trim(); + + // skip lines that are javadoc references + if (line.startsWith("@")) { + continue; + } + + // remove all HTML tags + line = line.replaceAll("\\</?\\w+\\/?>", ""); + + // we are starting from a new line, so add a whitespace + if (!first) { + sb.append(' '); + } + + for (char c : line.toCharArray()) { + if (Character.isJavaIdentifierPart(c) || '.' == c) { + sb.append(c); + } else if (Character.isWhitespace(c)) { + // always use space as whitespace, also for line feeds etc + sb.append(' '); + } + } + + first = false; + } + + // remove double whitespaces, and trim + String s = sb.toString(); + s = s.replaceAll("\\s+", " "); + return s.trim(); + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/cacef351/tooling/apt/src/main/java/org/apache/camel/tools/apt/Strings.java ---------------------------------------------------------------------- diff --git a/tooling/apt/src/main/java/org/apache/camel/tools/apt/Strings.java b/tooling/apt/src/main/java/org/apache/camel/tools/apt/Strings.java new file mode 100644 index 0000000..79e318a --- /dev/null +++ b/tooling/apt/src/main/java/org/apache/camel/tools/apt/Strings.java @@ -0,0 +1,81 @@ +/** + * 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.camel.tools.apt; + +/** + * Some String helper methods + */ +final class Strings { + + private Strings() { + //Helper class + } + + /** + * Returns true if the given text is null or empty string + */ + public static boolean isNullOrEmpty(String text) { + return text == null || text.length() == 0; + } + + /** + * Returns the value or the defaultValue if it is null + */ + public static String getOrElse(String text, String defaultValue) { + return (text != null) ? text : defaultValue; + } + + /** + * Returns the canonical class name by removing any generic type information. + */ + public static String canonicalClassName(String className) { + // remove generics + int pos = className.indexOf('<'); + if (pos != -1) { + return className.substring(0, pos); + } else { + return className; + } + } + + /** + * Returns the text wrapped double quotes + */ + public static String doubleQuote(String text) { + return quote(text, "\""); + } + + /** + * Returns the text wrapped single quotes + */ + public static String singleQuote(String text) { + return quote(text, "'"); + } + + /** + * Wraps the text in the given quote text + * + * @param text the text to wrap in quotes + * @param quote the quote text added to the prefix and postfix of the text + * + * @return the text wrapped in the given quotes + */ + public static String quote(String text, String quote) { + return quote + text + quote; + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/cacef351/tooling/apt/src/main/java/org/apache/camel/tools/apt/util/CollectionStringBuffer.java ---------------------------------------------------------------------- diff --git a/tooling/apt/src/main/java/org/apache/camel/tools/apt/util/CollectionStringBuffer.java b/tooling/apt/src/main/java/org/apache/camel/tools/apt/util/CollectionStringBuffer.java deleted file mode 100644 index 01fbddd..0000000 --- a/tooling/apt/src/main/java/org/apache/camel/tools/apt/util/CollectionStringBuffer.java +++ /dev/null @@ -1,59 +0,0 @@ -/** - * 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.camel.tools.apt.util; - -/** - /** - * A little helper class for converting a collection of values to a (usually comma separated) string. - */ -public class CollectionStringBuffer { - - private final StringBuilder buffer = new StringBuilder(); - private String separator; - private boolean first = true; - - public CollectionStringBuffer() { - this(", "); - } - - public CollectionStringBuffer(String separator) { - this.separator = separator; - } - - @Override - public String toString() { - return buffer.toString(); - } - - public void append(Object value) { - if (first) { - first = false; - } else { - buffer.append(separator); - } - buffer.append(value); - } - - public String getSeparator() { - return separator; - } - - public void setSeparator(String separator) { - this.separator = separator; - } - -} http://git-wip-us.apache.org/repos/asf/camel/blob/cacef351/tooling/apt/src/main/java/org/apache/camel/tools/apt/util/Func1.java ---------------------------------------------------------------------- diff --git a/tooling/apt/src/main/java/org/apache/camel/tools/apt/util/Func1.java b/tooling/apt/src/main/java/org/apache/camel/tools/apt/util/Func1.java deleted file mode 100644 index cdd3199..0000000 --- a/tooling/apt/src/main/java/org/apache/camel/tools/apt/util/Func1.java +++ /dev/null @@ -1,24 +0,0 @@ -/** - * 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.camel.tools.apt.util; - -/** - * Represents a function with 1 argument - */ -public interface Func1<T1, R> { - R call(T1 t1); -} http://git-wip-us.apache.org/repos/asf/camel/blob/cacef351/tooling/apt/src/main/java/org/apache/camel/tools/apt/util/JsonSchemaHelper.java ---------------------------------------------------------------------- diff --git a/tooling/apt/src/main/java/org/apache/camel/tools/apt/util/JsonSchemaHelper.java b/tooling/apt/src/main/java/org/apache/camel/tools/apt/util/JsonSchemaHelper.java deleted file mode 100644 index 5646dee..0000000 --- a/tooling/apt/src/main/java/org/apache/camel/tools/apt/util/JsonSchemaHelper.java +++ /dev/null @@ -1,186 +0,0 @@ -/** - * 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.camel.tools.apt.util; - -import java.util.Set; - -import static org.apache.camel.tools.apt.util.Strings.doubleQuote; - -/** - * A helper class for <a href="http://json-schema.org/">JSON schema</a>. - */ -public final class JsonSchemaHelper { - - private JsonSchemaHelper() { - } - - public static String toJson(String name, String type, String description, boolean enumType, Set<String> enums) { - String typeName = JsonSchemaHelper.getType(type, enumType); - - StringBuilder sb = new StringBuilder(); - sb.append(doubleQuote(name)); - sb.append(": { \"type\": "); - - if ("enum".equals(typeName)) { - sb.append(doubleQuote("string")); - sb.append(", \"javaType\": \"" + type + "\""); - CollectionStringBuffer enumValues = new CollectionStringBuffer(); - for (Object value : enums) { - enumValues.append(doubleQuote(value.toString())); - } - sb.append(", \"enum\": [ "); - sb.append(enumValues.toString()); - sb.append(" ]"); - } else if ("array".equals(typeName)) { - sb.append(doubleQuote("array")); - sb.append(", \"javaType\": \"" + type + "\""); - } else { - sb.append(doubleQuote(typeName)); - sb.append(", \"javaType\": \"" + type + "\""); - } - - if (!Strings.isNullOrEmpty(description)) { - sb.append(", \"description\": "); - String text = sanitizeDescription(description); - sb.append(doubleQuote(text)); - } - - sb.append(" }"); - return sb.toString(); - } - - /** - * Gets the JSon schema type. - * - * @param type the java type - * @return the json schema type, is never null, but returns <tt>object</tt> as the generic type - */ - public static String getType(String type, boolean enumType) { - if (enumType) { - return "enum"; - } - - String primitive = getPrimitiveType(type); - if (primitive != null) { - return primitive; - } - - return "object"; - } - - /** - * Gets the JSon schema primitive type. - * - * @param name the java type - * @return the json schema primitive type, or <tt>null</tt> if not a primitive - */ - public static String getPrimitiveType(String name) { - - // special for byte[] or Object[] as its common to use - if ("java.lang.byte[]".equals(name) || "byte[]".equals(name)) { - return "string"; - } else if ("java.lang.Byte[]".equals(name) || "Byte[]".equals(name)) { - return "array"; - } else if ("java.lang.Object[]".equals(name) || "Object[]".equals(name)) { - return "array"; - } else if ("java.lang.String[]".equals(name) || "String[]".equals(name)) { - return "array"; - // and these is common as well - } else if ("java.lang.String".equals(name) || "String".equals(name)) { - return "string"; - } else if ("java.lang.Boolean".equals(name) || "Boolean".equals(name)) { - return "boolean"; - } else if ("boolean".equals(name)) { - return "boolean"; - } else if ("java.lang.Integer".equals(name) || "Integer".equals(name)) { - return "integer"; - } else if ("int".equals(name)) { - return "integer"; - } else if ("java.lang.Long".equals(name) || "Long".equals(name)) { - return "integer"; - } else if ("long".equals(name)) { - return "integer"; - } else if ("java.lang.Short".equals(name) || "Short".equals(name)) { - return "integer"; - } else if ("short".equals(name)) { - return "integer"; - } else if ("java.lang.Byte".equals(name) || "Byte".equals(name)) { - return "integer"; - } else if ("byte".equals(name)) { - return "integer"; - } else if ("java.lang.Float".equals(name) || "Float".equals(name)) { - return "number"; - } else if ("float".equals(name)) { - return "number"; - } else if ("java.lang.Double".equals(name) || "Double".equals(name)) { - return "number"; - } else if ("double".equals(name)) { - return "number"; - } - - return null; - } - - /** - * Sanitizes the javadoc to removed invalid characters so it can be used as json description - * - * @param javadoc the javadoc - * @return the text that is valid as json - */ - public static String sanitizeDescription(String javadoc) { - // lets just use what java accepts as identifiers - StringBuilder sb = new StringBuilder(); - - // split into lines - String[] lines = javadoc.split("\n"); - - boolean first = true; - for (String line : lines) { - line = line.trim(); - - // skip lines that are javadoc references - if (line.startsWith("@")) { - continue; - } - - // remove all HTML tags - line = line.replaceAll("\\</?\\w+\\/?>", ""); - - // we are starting from a new line, so add a whitespace - if (!first) { - sb.append(' '); - } - - for (char c : line.toCharArray()) { - if (Character.isJavaIdentifierPart(c) || '.' == c) { - sb.append(c); - } else if (Character.isWhitespace(c)) { - // always use space as whitespace, also for line feeds etc - sb.append(' '); - } - } - - first = false; - } - - // remove double whitespaces, and trim - String s = sb.toString(); - s = s.replaceAll("\\s+", " "); - return s.trim(); - } - -} http://git-wip-us.apache.org/repos/asf/camel/blob/cacef351/tooling/apt/src/main/java/org/apache/camel/tools/apt/util/Strings.java ---------------------------------------------------------------------- diff --git a/tooling/apt/src/main/java/org/apache/camel/tools/apt/util/Strings.java b/tooling/apt/src/main/java/org/apache/camel/tools/apt/util/Strings.java deleted file mode 100644 index f07bb79..0000000 --- a/tooling/apt/src/main/java/org/apache/camel/tools/apt/util/Strings.java +++ /dev/null @@ -1,81 +0,0 @@ -/** - * 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.camel.tools.apt.util; - -/** - * Some String helper methods - */ -public final class Strings { - - private Strings() { - //Helper class - } - - /** - * Returns true if the given text is null or empty string - */ - public static boolean isNullOrEmpty(String text) { - return text == null || text.length() == 0; - } - - /** - * Returns the value or the defaultValue if it is null - */ - public static String getOrElse(String text, String defaultValue) { - return (text != null) ? text : defaultValue; - } - - /** - * Returns the canonical class name by removing any generic type information. - */ - public static String canonicalClassName(String className) { - // remove generics - int pos = className.indexOf('<'); - if (pos != -1) { - return className.substring(0, pos); - } else { - return className; - } - } - - /** - * Returns the text wrapped double quotes - */ - public static String doubleQuote(String text) { - return quote(text, "\""); - } - - /** - * Returns the text wrapped single quotes - */ - public static String singleQuote(String text) { - return quote(text, "'"); - } - - /** - * Wraps the text in the given quote text - * - * @param text the text to wrap in quotes - * @param quote the quote text added to the prefix and postfix of the text - * - * @return the text wrapped in the given quotes - */ - public static String quote(String text, String quote) { - return quote + text + quote; - } - -}