/*
* 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
/*
* 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
/*
* 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
// save aliases
{
final ArrayList<String> aliasesList = new ArrayList<String>();
- for (final String alias : aliases) {
+ for (final String alias : aliases)
aliasesList.add(alias);
- }
this.aliases = aliasesList;
}
}
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);
}
public List<Integer> getArgumentsAsIntegers() {
final ArrayList<Integer> result = new ArrayList<Integer>();
- for (final String argument : arguments) {
+ for (final String argument : arguments)
result.add(Integer.valueOf(argument));
- }
return result;
}
if (enableArguments) {
buffer.append(" (" + argumentType.describeFormat() + ")");
- if (enableMultipleArguments) {
+ if (enableMultipleArguments)
buffer.append("...");
- }
}
buffer.append("\n");
/*
* 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
*/
public Parameter getParameterByAlias(final String alias) {
- for (final Parameter parameter : parameters) {
+ for (final Parameter parameter : parameters)
if (parameter.matchesAlias(alias))
return parameter;
- }
return null;
}
return false;
} else {
- if (currentParameter != null) {
+ if (currentParameter != null)
if (!currentParameter.noMoreArguments())
return false;
- }
parameterForAlias.setParameterSpecified(true);
currentParameter = parameterForAlias;
// 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());
- }
}
}
/*
* 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
/*
* 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
/*
* 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
/*
* 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
/*
* 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
/*
* 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
/*
* 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
final int currentBit = currentByte & mask;
- if (currentBit != 0) {
+ if (currentBit != 0)
readableByte = readableByte | 1;
- }
currentBytePointer--;
}
}
public int readIntegerCompressed8() throws IOException {
- if (readBits(1) == 0) {
+ if (readBits(1) == 0)
return readBits(8);
- } else {
+ else
return readBits(32);
- }
}
}
/*
* 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
final int currentBit = data & mask;
currentByte = currentByte << 1;
- if (currentBit != 0) {
+ if (currentBit != 0)
currentByte = currentByte | 1;
- }
currentBytePointer++;
if (currentBytePointer == 8) {
/*
* 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
/*
* 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
+/*
+ * 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();
}
+/*
+ * 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;
+/*
+ * 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;
/*
* 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
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";
/*
* 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
/*
* 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
/*
* 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
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);
}
}
- /**
- * 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);
- }
-
}
+/*
+ * 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 {
/*
* 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
--- /dev/null
+/*
+ * 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);
+ }
+
+}
--- /dev/null
+/*
+ * 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;
+ }
+
+}
--- /dev/null
+/*
+ * 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<Terminator> terminators = new ArrayList<Terminator>();
+ private final String source;
+
+ Stack<Integer> tokenIndexes = new Stack<Integer>();
+
+ 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();
+ }
+
+}
--- /dev/null
+/*
+ * 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;
+ }
+}