From cf965fda534cc562368c9f2a3f34475e2519fcdc Mon Sep 17 00:00:00 2001 From: Svjatoslav Agejenko Date: Sun, 28 Jul 2013 20:35:35 +0300 Subject: [PATCH] fixed copyright headers --- .../commons/commandline/CLIHelper.java | 2 +- .../commandline/parameterparser/Argument.java | 2 +- .../parameterparser/Parameter.java | 20 +-- .../commandline/parameterparser/Parser.java | 14 +- .../arguments/ExistingDirectory.java | 2 +- .../arguments/ExistingFile.java | 2 +- .../arguments/IntegerArgument.java | 2 +- .../arguments/NonExistingDirectory.java | 2 +- .../arguments/NonExistingFile.java | 2 +- .../arguments/StringArgument.java | 2 +- .../commons/data/BitInputStream.java | 10 +- .../commons/data/BitOutputStream.java | 5 +- .../commons/data/EnhancedDataInputStream.java | 2 +- .../data/EnhancedDataOutputStream.java | 2 +- .../svjatoslav/commons/data/HexConverter.java | 23 +++- .../commons/data/xml/XmlElement.java | 9 ++ .../commons/data/xml/XmlHelper.java | 8 ++ .../commons/file/CommonPathResolver.java | 6 +- .../commons/file/FilePathParser.java | 2 +- .../eu/svjatoslav/commons/file/IOHelper.java | 2 +- .../commons/gui/dialog/ExceptionDialog.java | 26 ++-- .../commons/string/CuttableString.java | 9 ++ .../commons/string/WildCardMatcher.java | 2 +- .../tokenizer/InvalidSyntaxException.java | 20 +++ .../commons/string/tokenizer/Terminator.java | 31 +++++ .../commons/string/tokenizer/Tokenizer.java | 129 ++++++++++++++++++ .../string/tokenizer/TokenizerMatch.java | 21 +++ 27 files changed, 292 insertions(+), 65 deletions(-) create mode 100644 src/main/java/eu/svjatoslav/commons/string/tokenizer/InvalidSyntaxException.java create mode 100644 src/main/java/eu/svjatoslav/commons/string/tokenizer/Terminator.java create mode 100644 src/main/java/eu/svjatoslav/commons/string/tokenizer/Tokenizer.java create mode 100644 src/main/java/eu/svjatoslav/commons/string/tokenizer/TokenizerMatch.java diff --git a/src/main/java/eu/svjatoslav/commons/commandline/CLIHelper.java b/src/main/java/eu/svjatoslav/commons/commandline/CLIHelper.java index a232246..5537df8 100755 --- a/src/main/java/eu/svjatoslav/commons/commandline/CLIHelper.java +++ b/src/main/java/eu/svjatoslav/commons/commandline/CLIHelper.java @@ -1,6 +1,6 @@ /* * Svjatoslav Commons - shared library of common functionality. - * Copyright (C) 2012, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu + * Copyright ©2012-2013, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu * * This program is free software; you can redistribute it and/or * modify it under the terms of version 2 of the GNU General Public License diff --git a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/Argument.java b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/Argument.java index d969f16..36d2c92 100755 --- a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/Argument.java +++ b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/Argument.java @@ -1,6 +1,6 @@ /* * Svjatoslav Commons - shared library of common functionality. - * Copyright (C) 2012, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu + * Copyright ©2012-2013, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu * * This program is free software; you can redistribute it and/or * modify it under the terms of version 2 of the GNU General Public License diff --git a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/Parameter.java b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/Parameter.java index 1575fcf..1e738a5 100755 --- a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/Parameter.java +++ b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/Parameter.java @@ -1,6 +1,6 @@ /* * Svjatoslav Commons - shared library of common functionality. - * Copyright (C) 2012, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu + * Copyright ©2012-2013, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu * * This program is free software; you can redistribute it and/or * modify it under the terms of version 2 of the GNU General Public License @@ -51,9 +51,8 @@ public class Parameter { // save aliases { final ArrayList aliasesList = new ArrayList(); - for (final String alias : aliases) { + for (final String alias : aliases) aliasesList.add(alias); - } this.aliases = aliasesList; } @@ -112,26 +111,23 @@ public class Parameter { } public File getArgumentAsFile() { - if (arguments.size() != 1) { + if (arguments.size() != 1) throw new RuntimeException("Parameter " + description + " shall have exactly 1 argument."); - } return new File(arguments.get(0)); } public int getArgumentAsInteger() { - if (arguments.size() != 1) { + if (arguments.size() != 1) throw new RuntimeException("Parameter " + description + " shall have exactly 1 argument."); - } return Integer.parseInt(arguments.get(0)); } public String getArgumentAsString() { - if (arguments.size() != 1) { + if (arguments.size() != 1) throw new RuntimeException("Parameter " + description + " shall have exactly 1 argument."); - } return arguments.get(0); } @@ -149,9 +145,8 @@ public class Parameter { public List getArgumentsAsIntegers() { final ArrayList result = new ArrayList(); - for (final String argument : arguments) { + for (final String argument : arguments) result.add(Integer.valueOf(argument)); - } return result; } @@ -169,9 +164,8 @@ public class Parameter { if (enableArguments) { buffer.append(" (" + argumentType.describeFormat() + ")"); - if (enableMultipleArguments) { + if (enableMultipleArguments) buffer.append("..."); - } } buffer.append("\n"); diff --git a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/Parser.java b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/Parser.java index d2f623f..a72984f 100755 --- a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/Parser.java +++ b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/Parser.java @@ -1,6 +1,6 @@ /* * Svjatoslav Commons - shared library of common functionality. - * Copyright (C) 2012, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu + * Copyright ©2012-2013, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu * * This program is free software; you can redistribute it and/or * modify it under the terms of version 2 of the GNU General Public License @@ -30,10 +30,9 @@ public class Parser { */ public Parameter getParameterByAlias(final String alias) { - for (final Parameter parameter : parameters) { + for (final Parameter parameter : parameters) if (parameter.matchesAlias(alias)) return parameter; - } return null; } @@ -60,10 +59,9 @@ public class Parser { return false; } else { - if (currentParameter != null) { + if (currentParameter != null) if (!currentParameter.noMoreArguments()) return false; - } parameterForAlias.setParameterSpecified(true); currentParameter = parameterForAlias; @@ -73,21 +71,19 @@ public class Parser { // check if any mandatory parameters are missing - for (final Parameter parameter : parameters) { + for (final Parameter parameter : parameters) if (parameter.isMandatory() && (!parameter.isParameterSpecified())) { System.out.println("Error! Mandatory parameter (" + parameter.getAliases() + ") is not specified."); return false; } - } return true; } public void showHelp() { - for (final Parameter parameter : parameters) { + for (final Parameter parameter : parameters) System.out.println(parameter.getHelp()); - } } } diff --git a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/arguments/ExistingDirectory.java b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/arguments/ExistingDirectory.java index 25c521d..07341dd 100755 --- a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/arguments/ExistingDirectory.java +++ b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/arguments/ExistingDirectory.java @@ -1,6 +1,6 @@ /* * Svjatoslav Commons - shared library of common functionality. - * Copyright (C) 2012, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu + * Copyright ©2012-2013, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu * * This program is free software; you can redistribute it and/or * modify it under the terms of version 2 of the GNU General Public License diff --git a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/arguments/ExistingFile.java b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/arguments/ExistingFile.java index d46f05d..676663f 100755 --- a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/arguments/ExistingFile.java +++ b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/arguments/ExistingFile.java @@ -1,6 +1,6 @@ /* * Svjatoslav Commons - shared library of common functionality. - * Copyright (C) 2012, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu + * Copyright ©2012-2013, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu * * This program is free software; you can redistribute it and/or * modify it under the terms of version 2 of the GNU General Public License diff --git a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/arguments/IntegerArgument.java b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/arguments/IntegerArgument.java index 50f1ece..1d29046 100755 --- a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/arguments/IntegerArgument.java +++ b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/arguments/IntegerArgument.java @@ -1,6 +1,6 @@ /* * Svjatoslav Commons - shared library of common functionality. - * Copyright (C) 2012, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu + * Copyright ©2012-2013, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu * * This program is free software; you can redistribute it and/or * modify it under the terms of version 2 of the GNU General Public License diff --git a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/arguments/NonExistingDirectory.java b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/arguments/NonExistingDirectory.java index 3190e9d..005077b 100755 --- a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/arguments/NonExistingDirectory.java +++ b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/arguments/NonExistingDirectory.java @@ -1,6 +1,6 @@ /* * Svjatoslav Commons - shared library of common functionality. - * Copyright (C) 2012, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu + * Copyright ©2012-2013, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu * * This program is free software; you can redistribute it and/or * modify it under the terms of version 2 of the GNU General Public License diff --git a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/arguments/NonExistingFile.java b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/arguments/NonExistingFile.java index 1f68bf8..e4ddeb4 100755 --- a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/arguments/NonExistingFile.java +++ b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/arguments/NonExistingFile.java @@ -1,6 +1,6 @@ /* * Svjatoslav Commons - shared library of common functionality. - * Copyright (C) 2012, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu + * Copyright ©2012-2013, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu * * This program is free software; you can redistribute it and/or * modify it under the terms of version 2 of the GNU General Public License diff --git a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/arguments/StringArgument.java b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/arguments/StringArgument.java index 0da8cb7..dd9f588 100755 --- a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/arguments/StringArgument.java +++ b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/arguments/StringArgument.java @@ -1,6 +1,6 @@ /* * Svjatoslav Commons - shared library of common functionality. - * Copyright (C) 2012, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu + * Copyright ©2012-2013, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu * * This program is free software; you can redistribute it and/or * modify it under the terms of version 2 of the GNU General Public License diff --git a/src/main/java/eu/svjatoslav/commons/data/BitInputStream.java b/src/main/java/eu/svjatoslav/commons/data/BitInputStream.java index 3ecff98..6564417 100755 --- a/src/main/java/eu/svjatoslav/commons/data/BitInputStream.java +++ b/src/main/java/eu/svjatoslav/commons/data/BitInputStream.java @@ -1,6 +1,6 @@ /* * Svjatoslav Commons - shared library of common functionality. - * Copyright (C) 2012, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu + * Copyright ©2012-2013, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu * * This program is free software; you can redistribute it and/or * modify it under the terms of version 2 of the GNU General Public License @@ -44,9 +44,8 @@ public class BitInputStream { final int currentBit = currentByte & mask; - if (currentBit != 0) { + if (currentBit != 0) readableByte = readableByte | 1; - } currentBytePointer--; } @@ -54,11 +53,10 @@ public class BitInputStream { } public int readIntegerCompressed8() throws IOException { - if (readBits(1) == 0) { + if (readBits(1) == 0) return readBits(8); - } else { + else return readBits(32); - } } } diff --git a/src/main/java/eu/svjatoslav/commons/data/BitOutputStream.java b/src/main/java/eu/svjatoslav/commons/data/BitOutputStream.java index 6348d0a..293788f 100755 --- a/src/main/java/eu/svjatoslav/commons/data/BitOutputStream.java +++ b/src/main/java/eu/svjatoslav/commons/data/BitOutputStream.java @@ -1,6 +1,6 @@ /* * Svjatoslav Commons - shared library of common functionality. - * Copyright (C) 2012, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu + * Copyright ©2012-2013, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu * * This program is free software; you can redistribute it and/or * modify it under the terms of version 2 of the GNU General Public License @@ -46,9 +46,8 @@ public class BitOutputStream { final int currentBit = data & mask; currentByte = currentByte << 1; - if (currentBit != 0) { + if (currentBit != 0) currentByte = currentByte | 1; - } currentBytePointer++; if (currentBytePointer == 8) { diff --git a/src/main/java/eu/svjatoslav/commons/data/EnhancedDataInputStream.java b/src/main/java/eu/svjatoslav/commons/data/EnhancedDataInputStream.java index 4adc080..e093619 100644 --- a/src/main/java/eu/svjatoslav/commons/data/EnhancedDataInputStream.java +++ b/src/main/java/eu/svjatoslav/commons/data/EnhancedDataInputStream.java @@ -1,6 +1,6 @@ /* * Svjatoslav Commons - shared library of common functionality. - * Copyright (C) 2012, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu + * Copyright ©2012-2013, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu * * This program is free software; you can redistribute it and/or * modify it under the terms of version 2 of the GNU General Public License diff --git a/src/main/java/eu/svjatoslav/commons/data/EnhancedDataOutputStream.java b/src/main/java/eu/svjatoslav/commons/data/EnhancedDataOutputStream.java index 2598215..24e8029 100644 --- a/src/main/java/eu/svjatoslav/commons/data/EnhancedDataOutputStream.java +++ b/src/main/java/eu/svjatoslav/commons/data/EnhancedDataOutputStream.java @@ -1,6 +1,6 @@ /* * Svjatoslav Commons - shared library of common functionality. - * Copyright (C) 2012, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu + * Copyright ©2012-2013, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu * * This program is free software; you can redistribute it and/or * modify it under the terms of version 2 of the GNU General Public License diff --git a/src/main/java/eu/svjatoslav/commons/data/HexConverter.java b/src/main/java/eu/svjatoslav/commons/data/HexConverter.java index 52b16f4..e2f2007 100644 --- a/src/main/java/eu/svjatoslav/commons/data/HexConverter.java +++ b/src/main/java/eu/svjatoslav/commons/data/HexConverter.java @@ -1,19 +1,28 @@ +/* + * Svjatoslav Commons - shared library of common functionality. + * Copyright ©2012-2013, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of version 2 of the GNU General Public License + * as published by the Free Software Foundation. + */ + package eu.svjatoslav.commons.data; public class HexConverter { - static final String hexCodes = "0123456789ABCDEF"; + static final String hexCharacters = "0123456789ABCDEF"; - public static String byteArrayToHex(final byte[] raw) { + public static String byteArrayToHex(final byte[] bytes) { - if (raw == null) + if (bytes == null) return null; - final StringBuilder result = new StringBuilder(2 * raw.length); + final StringBuilder result = new StringBuilder(2 * bytes.length); - for (final byte b : raw) - result.append(hexCodes.charAt((b & 0xF0) >> 4)).append( - hexCodes.charAt((b & 0x0F))); + for (final byte b : bytes) + result.append(hexCharacters.charAt((b & 0xF0) >> 4)).append( + hexCharacters.charAt((b & 0x0F))); return result.toString(); } diff --git a/src/main/java/eu/svjatoslav/commons/data/xml/XmlElement.java b/src/main/java/eu/svjatoslav/commons/data/xml/XmlElement.java index 7b807ac..71e8a95 100644 --- a/src/main/java/eu/svjatoslav/commons/data/xml/XmlElement.java +++ b/src/main/java/eu/svjatoslav/commons/data/xml/XmlElement.java @@ -1,3 +1,12 @@ +/* + * Svjatoslav Commons - shared library of common functionality. + * Copyright ©2012-2013, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of version 2 of the GNU General Public License + * as published by the Free Software Foundation. + */ + package eu.svjatoslav.commons.data.xml; import java.util.ArrayList; diff --git a/src/main/java/eu/svjatoslav/commons/data/xml/XmlHelper.java b/src/main/java/eu/svjatoslav/commons/data/xml/XmlHelper.java index 94b977d..355ec2c 100644 --- a/src/main/java/eu/svjatoslav/commons/data/xml/XmlHelper.java +++ b/src/main/java/eu/svjatoslav/commons/data/xml/XmlHelper.java @@ -1,3 +1,11 @@ +/* + * Svjatoslav Commons - shared library of common functionality. + * Copyright ©2012-2013, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of version 2 of the GNU General Public License + * as published by the Free Software Foundation. + */ package eu.svjatoslav.commons.data.xml; import java.io.IOException; diff --git a/src/main/java/eu/svjatoslav/commons/file/CommonPathResolver.java b/src/main/java/eu/svjatoslav/commons/file/CommonPathResolver.java index 81f9213..1fa1cfd 100644 --- a/src/main/java/eu/svjatoslav/commons/file/CommonPathResolver.java +++ b/src/main/java/eu/svjatoslav/commons/file/CommonPathResolver.java @@ -1,6 +1,6 @@ /* * Svjatoslav Commons - shared library of common functionality. - * Copyright (C) 2012, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu + * Copyright ©2012-2013, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu * * This program is free software; you can redistribute it and/or * modify it under the terms of version 2 of the GNU General Public License @@ -13,6 +13,10 @@ import java.io.File; public class CommonPathResolver { + /** + * This method tries to guess user desktop directory. Implementation is + * pretty lousy. Need to improve it some day. + */ public static File getDesktopDirectory() { String desktopPath = System.getProperty("user.home") + "/Desktop"; diff --git a/src/main/java/eu/svjatoslav/commons/file/FilePathParser.java b/src/main/java/eu/svjatoslav/commons/file/FilePathParser.java index 3fa1894..ac3856b 100755 --- a/src/main/java/eu/svjatoslav/commons/file/FilePathParser.java +++ b/src/main/java/eu/svjatoslav/commons/file/FilePathParser.java @@ -1,6 +1,6 @@ /* * Svjatoslav Commons - shared library of common functionality. - * Copyright (C) 2012, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu + * Copyright ©2012-2013, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu * * This program is free software; you can redistribute it and/or * modify it under the terms of version 2 of the GNU General Public License diff --git a/src/main/java/eu/svjatoslav/commons/file/IOHelper.java b/src/main/java/eu/svjatoslav/commons/file/IOHelper.java index 97d3b44..a6dd05f 100644 --- a/src/main/java/eu/svjatoslav/commons/file/IOHelper.java +++ b/src/main/java/eu/svjatoslav/commons/file/IOHelper.java @@ -1,6 +1,6 @@ /* * Svjatoslav Commons - shared library of common functionality. - * Copyright (C) 2012, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu + * Copyright ©2012-2013, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu * * This program is free software; you can redistribute it and/or * modify it under the terms of version 2 of the GNU General Public License diff --git a/src/main/java/eu/svjatoslav/commons/gui/dialog/ExceptionDialog.java b/src/main/java/eu/svjatoslav/commons/gui/dialog/ExceptionDialog.java index a80e6e3..c951916 100755 --- a/src/main/java/eu/svjatoslav/commons/gui/dialog/ExceptionDialog.java +++ b/src/main/java/eu/svjatoslav/commons/gui/dialog/ExceptionDialog.java @@ -1,6 +1,6 @@ /* * Svjatoslav Commons - shared library of common functionality. - * Copyright (C) 2012, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu + * Copyright ©2012-2013, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu * * This program is free software; you can redistribute it and/or * modify it under the terms of version 2 of the GNU General Public License @@ -19,6 +19,18 @@ import javax.swing.JPanel; public class ExceptionDialog { + /** + * This method is for testing + */ + public static void main(final String[] args) { + + final Throwable cause = new Throwable("details....."); + + final Exception exception = new Exception("test", cause); + + new ExceptionDialog(exception); + } + public ExceptionDialog(final Exception exception) { showException(exception); } @@ -91,16 +103,4 @@ public class ExceptionDialog { } - /** - * This method is for testing - */ - public static void main(final String[] args) { - - final Throwable cause = new Throwable("details....."); - - final Exception exception = new Exception("test", cause); - - new ExceptionDialog(exception); - } - } diff --git a/src/main/java/eu/svjatoslav/commons/string/CuttableString.java b/src/main/java/eu/svjatoslav/commons/string/CuttableString.java index 2adeba2..6fd0707 100644 --- a/src/main/java/eu/svjatoslav/commons/string/CuttableString.java +++ b/src/main/java/eu/svjatoslav/commons/string/CuttableString.java @@ -1,3 +1,12 @@ +/* + * Svjatoslav Commons - shared library of common functionality. + * Copyright ©2012-2013, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of version 2 of the GNU General Public License + * as published by the Free Software Foundation. + */ + package eu.svjatoslav.commons.string; public class CuttableString { diff --git a/src/main/java/eu/svjatoslav/commons/string/WildCardMatcher.java b/src/main/java/eu/svjatoslav/commons/string/WildCardMatcher.java index 0da2309..6e3031d 100755 --- a/src/main/java/eu/svjatoslav/commons/string/WildCardMatcher.java +++ b/src/main/java/eu/svjatoslav/commons/string/WildCardMatcher.java @@ -1,6 +1,6 @@ /* * Svjatoslav Commons - shared library of common functionality. - * Copyright (C) 2012, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu + * Copyright ©2012-2013, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu * * This program is free software; you can redistribute it and/or * modify it under the terms of version 2 of the GNU General Public License diff --git a/src/main/java/eu/svjatoslav/commons/string/tokenizer/InvalidSyntaxException.java b/src/main/java/eu/svjatoslav/commons/string/tokenizer/InvalidSyntaxException.java new file mode 100644 index 0000000..0856b5f --- /dev/null +++ b/src/main/java/eu/svjatoslav/commons/string/tokenizer/InvalidSyntaxException.java @@ -0,0 +1,20 @@ +/* + * Svjatoslav Commons - shared library of common functionality. + * Copyright ©2012-2013, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of version 2 of the GNU General Public License + * as published by the Free Software Foundation. + */ + +package eu.svjatoslav.commons.string.tokenizer; + +public class InvalidSyntaxException extends Exception { + + private static final long serialVersionUID = 88294980027680555L; + + public InvalidSyntaxException(final String cause) { + super(cause); + } + +} diff --git a/src/main/java/eu/svjatoslav/commons/string/tokenizer/Terminator.java b/src/main/java/eu/svjatoslav/commons/string/tokenizer/Terminator.java new file mode 100644 index 0000000..14fcbb5 --- /dev/null +++ b/src/main/java/eu/svjatoslav/commons/string/tokenizer/Terminator.java @@ -0,0 +1,31 @@ +/* + * Svjatoslav Commons - shared library of common functionality. + * Copyright ©2012-2013, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of version 2 of the GNU General Public License + * as published by the Free Software Foundation. + */ + +package eu.svjatoslav.commons.string.tokenizer; + +public class Terminator { + + String startSequence; + String endSequence; + boolean ignoreTerminator; + + public Terminator(final String startPattern, final boolean ignoreTerminator) { + startSequence = startPattern; + this.ignoreTerminator = ignoreTerminator; + } + + public Terminator(final String startSequence, final String endSequence, + final boolean ignoreTerminator) { + + this.startSequence = startSequence; + this.endSequence = endSequence; + this.ignoreTerminator = ignoreTerminator; + } + +} diff --git a/src/main/java/eu/svjatoslav/commons/string/tokenizer/Tokenizer.java b/src/main/java/eu/svjatoslav/commons/string/tokenizer/Tokenizer.java new file mode 100644 index 0000000..dc0cb9c --- /dev/null +++ b/src/main/java/eu/svjatoslav/commons/string/tokenizer/Tokenizer.java @@ -0,0 +1,129 @@ +/* + * Svjatoslav Commons - shared library of common functionality. + * Copyright ©2012-2013, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of version 2 of the GNU General Public License + * as published by the Free Software Foundation. + */ + +package eu.svjatoslav.commons.string.tokenizer; + +import java.util.ArrayList; +import java.util.List; +import java.util.Stack; + +public class Tokenizer { + + private final List terminators = new ArrayList(); + private final String source; + + Stack tokenIndexes = new Stack(); + + private int currentIndex = 0; + + public Tokenizer(final String source) { + this.source = source; + } + + public void addTerminator(final String startSequence, + final boolean ignoreTerminator) { + terminators.add(new Terminator(startSequence, ignoreTerminator)); + } + + public void addTerminator(final String startSequence, + final String endSequence, final boolean ignoreTerminator) { + terminators.add(new Terminator(startSequence, endSequence, + ignoreTerminator)); + } + + public void expectNextToken(final String value) + throws InvalidSyntaxException { + final TokenizerMatch match = getNextToken(); + if (!value.equals(match.token)) + throw new InvalidSyntaxException("Expected \"" + value + + "\" but got \"" + match.token + "\" instead."); + } + + public TokenizerMatch getNextToken() { + tokenIndexes.push(currentIndex); + final StringBuffer result = new StringBuffer(); + + while (true) { + if (currentIndex >= source.length()) + return null; + + boolean accumulateCurrentChar = true; + + findTerminator: for (final Terminator terminator : terminators) + if (sequenceMatches(terminator.startSequence)) + + if (terminator.ignoreTerminator) { + currentIndex += terminator.startSequence.length(); + + if (terminator.endSequence != null) + skipUntilSequence(terminator.endSequence); + + if (result.length() > 0) + return new TokenizerMatch(result.toString(), + terminator); + else { + accumulateCurrentChar = false; + break findTerminator; + } + } else if (result.length() > 0) + return new TokenizerMatch(result.toString(), terminator); + else { + currentIndex += terminator.startSequence.length(); + return new TokenizerMatch(terminator.startSequence, + terminator); + } + + if (accumulateCurrentChar) { + result.append(source.charAt(currentIndex)); + currentIndex++; + } + } + + } + + public boolean probeNextToken(final String token) { + if (token.equals(getNextToken().token)) + return true; + + unreadToken(); + return false; + } + + public boolean sequenceMatches(final String sequence) { + if ((currentIndex + sequence.length()) > source.length()) + return false; + + for (int i = 0; i < sequence.length(); i++) + if (sequence.charAt(i) != source.charAt(i + currentIndex)) + return false; + + return true; + } + + public void skipUntilDataEnd() { + tokenIndexes.push(currentIndex); + currentIndex = source.length(); + } + + public void skipUntilSequence(final String sequence) { + while (currentIndex < source.length()) { + if (sequenceMatches(sequence)) { + currentIndex += sequence.length(); + return; + } + + currentIndex++; + } + } + + public void unreadToken() { + currentIndex = tokenIndexes.pop(); + } + +} diff --git a/src/main/java/eu/svjatoslav/commons/string/tokenizer/TokenizerMatch.java b/src/main/java/eu/svjatoslav/commons/string/tokenizer/TokenizerMatch.java new file mode 100644 index 0000000..439eb2a --- /dev/null +++ b/src/main/java/eu/svjatoslav/commons/string/tokenizer/TokenizerMatch.java @@ -0,0 +1,21 @@ +/* + * Svjatoslav Commons - shared library of common functionality. + * Copyright ©2012-2013, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of version 2 of the GNU General Public License + * as published by the Free Software Foundation. + */ + +package eu.svjatoslav.commons.string.tokenizer; + +public class TokenizerMatch { + + public String token; + public Terminator terminator; + + public TokenizerMatch(final String token, final Terminator terminator) { + this.token = token; + this.terminator = terminator; + } +} -- 2.20.1