"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
-<!-- 2023-06-11 Sun 21:23 -->
+<!-- 2024-10-28 ma 23:31 -->
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Svjatoslav Commons - Java library of commonly used functions</title>
<h2>Table of Contents</h2>
<div id="text-table-of-contents" role="doc-toc">
<ul>
-<li><a href="#orgf8795c6">1. General</a>
+<li><a href="#org688caae">1. General</a>
<ul>
-<li><a href="#org85c626e">1.1. Source code</a></li>
+<li><a href="#org0b426f5">1.1. Source code</a></li>
</ul>
</li>
-<li><a href="#org5358341">2. Library contents</a></li>
-<li><a href="#orgd60f7bf">3. Usage</a></li>
+<li><a href="#org7fdb5b1">2. Library contents</a></li>
+<li><a href="#org79e0c3e">3. Usage</a></li>
</ul>
</div>
</div>
-<div id="outline-container-orgf8795c6" class="outline-2">
-<h2 id="orgf8795c6"><span class="section-number-2">1.</span> General</h2>
+<div id="outline-container-org688caae" class="outline-2">
+<h2 id="org688caae"><span class="section-number-2">1.</span> General</h2>
<div class="outline-text-2" id="text-1">
<ul class="org-ul">
<li>This program is free software: released under Creative Commons Zero
</ul>
</div>
-<div id="outline-container-org85c626e" class="outline-3">
-<h3 id="org85c626e"><span class="section-number-3">1.1.</span> Source code</h3>
+<div id="outline-container-org0b426f5" class="outline-3">
+<h3 id="org0b426f5"><span class="section-number-3">1.1.</span> Source code</h3>
<div class="outline-text-3" id="text-1-1">
<ul class="org-ul">
<li><a href="https://www2.svjatoslav.eu/gitweb/?p=svjatoslav_commons.git;a=snapshot;h=HEAD;sf=tgz">Download latest snapshot in TAR GZ format</a></li>
</div>
</div>
-<div id="outline-container-org5358341" class="outline-2">
-<h2 id="org5358341"><span class="section-number-2">2.</span> Library contents</h2>
+<div id="outline-container-org7fdb5b1" class="outline-2">
+<h2 id="org7fdb5b1"><span class="section-number-2">2.</span> Library contents</h2>
<div class="outline-text-2" id="text-2">
<ul class="org-ul">
<li>See <a href="https://www3.svjatoslav.eu/projects/svjatoslav_commons/apidocs/">JavaDoc</a>.</li>
eu.svjatoslav.commons.data.BitOutputStream
</pre></li>
-<li><p>
-Slightly enhanced data input and output streams.
-</p>
-<pre class="example">
-eu.svjatoslav.commons.data.EnhancedDataInputStream
-eu.svjatoslav.commons.data.EnhancedDataOutputStream
-</pre></li>
-
<li><p>
Byte array to HEX string converter.
</p>
eu.svjatoslav.commons.data.HexConverter
</pre></li>
-<li><p>
-Utility that tries to quess user desktop directory. (Quite ugly and
-unreliable. Better solution is needed.)
-</p>
-<pre class="example">
-eu.svjatoslav.commons.file.CommonPathResolver
-</pre></li>
-
<li><p>
File path parser.
</p>
</pre></li>
</ul></li>
-<li><p>
-Primitive and simple component to add navigation menus to the web
-sites.
-</p>
-<pre class="example">
-eu.svjatoslav.commons.network.navigation
-</pre></li>
-
<li><p>
String tokenizer.
</p>
</div>
</div>
-<div id="outline-container-orgd60f7bf" class="outline-2">
-<h2 id="orgd60f7bf"><span class="section-number-2">3.</span> Usage</h2>
+<div id="outline-container-org79e0c3e" class="outline-2">
+<h2 id="org79e0c3e"><span class="section-number-2">3.</span> Usage</h2>
<div class="outline-text-2" id="text-3">
<p>
Instructions to embed svjatoslav-commons in your project as a library:
</p>
<div class="org-src-container">
-<pre class="src src-xml"><<span style="color: #7b68ee; font-weight: bold;">dependencies</span>>
+<pre class="src src-xml"><<span style="color: #89b4fa;">dependencies</span>>
...
- <<span style="color: #7b68ee; font-weight: bold;">dependency</span>>
- <<span style="color: #7b68ee; font-weight: bold;">groupId</span>>eu.svjatoslav</<span style="color: #7b68ee; font-weight: bold;">groupId</span>>
- <<span style="color: #7b68ee; font-weight: bold;">artifactId</span>>svjatoslavcommons</<span style="color: #7b68ee; font-weight: bold;">artifactId</span>>
- <<span style="color: #7b68ee; font-weight: bold;">version</span>>1.9</<span style="color: #7b68ee; font-weight: bold;">version</span>>
- </<span style="color: #7b68ee; font-weight: bold;">dependency</span>>
+ <<span style="color: #89b4fa;">dependency</span>>
+ <<span style="color: #89b4fa;">groupId</span>>eu.svjatoslav</<span style="color: #89b4fa;">groupId</span>>
+ <<span style="color: #89b4fa;">artifactId</span>>svjatoslavcommons</<span style="color: #89b4fa;">artifactId</span>>
+ <<span style="color: #89b4fa;">version</span>>1.9</<span style="color: #89b4fa;">version</span>>
+ </<span style="color: #89b4fa;">dependency</span>>
...
-</<span style="color: #7b68ee; font-weight: bold;">dependencies</span>>
+</<span style="color: #89b4fa;">dependencies</span>>
-<<span style="color: #7b68ee; font-weight: bold;">repositories</span>>
+<<span style="color: #89b4fa;">repositories</span>>
...
- <<span style="color: #7b68ee; font-weight: bold;">repository</span>>
- <<span style="color: #7b68ee; font-weight: bold;">id</span>>svjatoslav.eu</<span style="color: #7b68ee; font-weight: bold;">id</span>>
- <<span style="color: #7b68ee; font-weight: bold;">name</span>>Svjatoslav repository</<span style="color: #7b68ee; font-weight: bold;">name</span>>
- <<span style="color: #7b68ee; font-weight: bold;">url</span>>https://www2.svjatoslav.eu/maven/</<span style="color: #7b68ee; font-weight: bold;">url</span>>
- </<span style="color: #7b68ee; font-weight: bold;">repository</span>>
+ <<span style="color: #89b4fa;">repository</span>>
+ <<span style="color: #89b4fa;">id</span>>svjatoslav.eu</<span style="color: #89b4fa;">id</span>>
+ <<span style="color: #89b4fa;">name</span>>Svjatoslav repository</<span style="color: #89b4fa;">name</span>>
+ <<span style="color: #89b4fa;">url</span>>https://www2.svjatoslav.eu/maven/</<span style="color: #89b4fa;">url</span>>
+ </<span style="color: #89b4fa;">repository</span>>
...
-</<span style="color: #7b68ee; font-weight: bold;">repositories</span>>
+</<span style="color: #89b4fa;">repositories</span>>
</pre>
</div>
</div>
</div>
<div id="postamble" class="status">
<p class="author">Author: Svjatoslav Agejenko</p>
-<p class="date">Created: 2023-06-11 Sun 21:23</p>
+<p class="date">Created: 2024-10-28 ma 23:31</p>
<p class="validation"><a href="https://validator.w3.org/check?uri=referer">Validate</a></p>
</div>
</body>
: eu.svjatoslav.commons.data.BitInputStream
: eu.svjatoslav.commons.data.BitOutputStream
-- Slightly enhanced data input and output streams.
- : eu.svjatoslav.commons.data.EnhancedDataInputStream
- : eu.svjatoslav.commons.data.EnhancedDataOutputStream
-
- Byte array to HEX string converter.
: eu.svjatoslav.commons.data.HexConverter
-- Utility that tries to quess user desktop directory. (Quite ugly and
- unreliable. Better solution is needed.)
- : eu.svjatoslav.commons.file.CommonPathResolver
-
- File path parser.
: eu.svjatoslav.commons.file.FilePathParser
and associated program call stack traceback.
: eu.svjatoslav.commons.gui.dialog.ExceptionDialog
-- Primitive and simple component to add navigation menus to the web
- sites.
- : eu.svjatoslav.commons.network.navigation
-
- String tokenizer.
: eu.svjatoslav.commons.string.tokenizer
this.inputStream = inputStream;
}
+ /**
+ * Read bits from the input stream.
+ * @param bitCount Number of bits to read.
+ * @return Read bits.
+ * @throws IOException If an I/O error occurs.
+ */
public int readBits(final int bitCount) throws IOException {
int readableByte = 0;
this.outputStream = outputStream;
}
+ /**
+ * Finish writing the last byte.
+ * @throws IOException If an I/O error occurs.
+ */
public void finishByte() throws IOException {
if (currentBytePointer != 0) {
outputStream.write(currentByte);
}
}
+ /**
+ * Write bits to the output stream.
+ * @param data Data to write.
+ * @param bitCount Number of bits to write.
+ * @throws IOException If an I/O error occurs.
+ */
public void storeBits(final int data, final int bitCount)
throws IOException {
for (int i = bitCount - 1; i >= 0; i--) {
+++ /dev/null
-/*
- * Svjatoslav Commons - shared library of common functionality. Author: Svjatoslav Agejenko.
- * This project is released under Creative Commons Zero (CC0) license.
- */
-package eu.svjatoslav.commons.data;
-
-import java.io.DataInputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.util.ArrayList;
-import java.util.List;
-
-public class EnhancedDataInputStream extends DataInputStream {
-
- public EnhancedDataInputStream(final InputStream in) {
- super(in);
- }
-
- public List<Integer> readIntegerList() throws IOException {
- final int length = readInt();
-
- final List<Integer> result = new ArrayList<>();
-
- for (int i = 0; i < length; i++)
- result.add(readInt());
-
- return result;
- }
-
- public String readString() throws IOException {
-
- final int length = readInt();
- if (length == -1)
- return null;
-
- final byte[] bytes = new byte[length];
- readFully(bytes);
-
- return new String(bytes, "UTF-8");
- }
-
-}
+++ /dev/null
-/*
- * Svjatoslav Commons - shared library of common functionality. Author: Svjatoslav Agejenko.
- * This project is released under Creative Commons Zero (CC0) license.
- */
-package eu.svjatoslav.commons.data;
-
-import java.io.DataOutputStream;
-import java.io.IOException;
-import java.io.OutputStream;
-import java.util.List;
-
-public class EnhancedDataOutputStream extends DataOutputStream {
-
- public EnhancedDataOutputStream(final OutputStream out) {
- super(out);
- }
-
- public void writeIntegerList(final List<Integer> list) throws IOException {
- writeInt(list.size());
-
- for (final Integer integer : list)
- writeInt(integer);
- }
-
- public void writeString(final String string) throws IOException {
- if (string == null) {
- writeInt(-1);
- return;
- }
-
- final byte[] bytes = string.getBytes("UTF-8");
-
- writeInt(bytes.length);
- write(bytes);
- }
-
-}
static final String hexCharacters = "0123456789ABCDEF";
+ /**
+ * Converts a byte array to hexadecimal string.
+ *
+ * @param bytes
+ * Byte array.
+ * @return Hexadecimal string.
+ */
public static String byteArrayToHex(final byte[] bytes) {
if (bytes == null)
public class CommonPathResolver {
+ /**
+ * Returns current user's home directory.
+ *
+ * @return Home directory.
+ */
public static File getHomeDirectory() {
return new File(System.getProperty("user.home"));
}
public class FilePathParser {
+ /**
+ * Returns file extension.
+ * @param file File.
+ * @return File extension. Empty string if a file has no extension.
+ */
public static String getFileExtension(final File file) {
final String fullFileName = file.getName();
return getFileExtension(fullFileName);
}
+ /**
+ * Returns file extension.
+ * @param fullFileName Full file name.
+ * @return File extension. Empty string if a file has no extension.
+ */
public static String getFileExtension(final String fullFileName) {
final int dot = fullFileName.lastIndexOf('.');
String fileExtension;
return fileExtension;
}
+ /**
+ * Returns file name without extension.
+ *
+ * @param file
+ * File.
+ * @return File name without extension.
+ */
public static String getFileNameWithoutExtension(final File file) {
final String fullFileName = file.getName();
return getFileNameWithoutExtension(fullFileName);
}
+ /**
+ * Returns file name without extension.
+ *
+ * @param fullFileName
+ * Full file name.
+ * @return File name without extension.
+ */
public static String getFileNameWithoutExtension(final String fullFileName) {
final int dot = fullFileName.lastIndexOf('.');
String fileName;
return fileName;
}
+ /**
+ * Returns file size description.
+ *
+ * @param fileSize
+ * File size in bytes.
+ * @return File size description.
+ */
public static String getFileSizeDescription(long fileSize) {
String suffix = "b";
import static javax.swing.BoxLayout.Y_AXIS;
+/**
+ * Helper class to show graphical exception dialog.
+ * <p>
+ * Dialog contains exception details and stack trace.
+ */
public class ExceptionDialog {
public ExceptionDialog(final Exception exception) {
final JPanel topPanel = new JPanel();
topPanel.setLayout(new BoxLayout(topPanel, Y_AXIS));
- // add exception type
+ // add an exception type
topPanel.add(
new JLabel("Exception type: " + exception.getClass().getCanonicalName()));
- // add error message
+ // add an error message
topPanel.add(new JLabel("Error message: " + exception.getMessage()));
- // add cause message
+ // add a cause message
Throwable cause = exception.getCause();
if (cause != null && cause.getMessage() != null)
topPanel.add(new JLabel("Cause: " + cause.getMessage()));
+++ /dev/null
-/*
- * Svjatoslav Commons - shared library of common functionality. Author: Svjatoslav Agejenko.
- * This project is released under Creative Commons Zero (CC0) license.
- */
-package eu.svjatoslav.commons.network.navigation;
-
-import javax.servlet.http.HttpServletRequest;
-import java.net.MalformedURLException;
-import java.net.URL;
-
-public class Navigation {
-
- private NavigationItem root = new NavigationItem(null, null, null);
-
- public NavigationItem getRoot(){
- return root;
- }
-
- public NavigationItem getSelectedItem(final HttpServletRequest request) {
- final String requestUrl = request.getRequestURL().toString();
-
- final String requestPath;
- try {
- requestPath = new URL(requestUrl).getPath();
- } catch (MalformedURLException e) {
- throw new IllegalArgumentException("Illegal request URL provided." , e);
- }
-
- @SuppressWarnings("unchecked") final NavigationItem match = root.getMatch(requestPath);
-
- if (match != null)
- return match;
-
- return root.getDefaultNavigationItem();
- }
-
- @SuppressWarnings("unchecked")
- public String getTopMenu(final HttpServletRequest request) {
-
- final NavigationItem selectedItem = getSelectedItem(request);
- final StringBuilder result = new StringBuilder();
-
- result.append("<div class=\"navigationMenu\">");
-
- for (final NavigationItem item : root.getChildren()) {
-
- if (item == selectedItem)
- result.append("<a class=\"menuItemSelected\" href=\""
- + item.getUrl() + "\">");
- else
- result.append("<a class=\"menuItem\" href=\""
- + item.getUrl() + "\">");
-
- result.append("<div>" + item.getTitle() + "</div>");
- result.append("</a>");
- }
-
- result.append("</div>");
-
- return result.toString();
- }
-
-}
+++ /dev/null
-/*
- * Svjatoslav Commons - shared library of common functionality. Author: Svjatoslav Agejenko.
- * This project is released under Creative Commons Zero (CC0) license.
- */
-package eu.svjatoslav.commons.network.navigation;
-
-import eu.svjatoslav.commons.string.GlobMatcher;
-
-import java.util.ArrayList;
-import java.util.List;
-
-public class NavigationItem {
-
- private final ArrayList<NavigationItem> children = new ArrayList<>();
- private final String url;
- private String pattern;
- private String title;
- private boolean isDefault;
-
- public NavigationItem(final String url, String pattern, String title ) {
- this.url = url;
- this.pattern = pattern;
- this.title = title;
- }
-
- public NavigationItem setDefault(){
- isDefault = true;
- return this;
- }
-
- public boolean isDefault(){
- return isDefault;
- }
-
- public void add(final NavigationItem navigationItem) {
- children.add(navigationItem);
- }
-
- public String getUrl() {
- return url;
- }
-
- NavigationItem getMatch(final String requestPath) {
- if (matchesUrl(requestPath))
- return this;
-
- for (final NavigationItem childNavigationItem : children) {
- final NavigationItem match = childNavigationItem.getMatch(requestPath);
-
- if (match != null)
- return match;
- }
- return null;
- }
-
- public List<NavigationItem> getChildren() {
- return children;
- }
-
- public String getTitle() {
- return title;
- }
-
- public boolean matchesUrl(final String url) {
- return GlobMatcher.match(url, pattern);
- }
-
- public NavigationItem getDefaultNavigationItem() {
- for (NavigationItem child : children)
- if (child.isDefault())
- return child;
- return null;
- }
-}
*/
package eu.svjatoslav.commons.string;
+/**
+ * GlobMatcher - class for matching strings against wildcard expressions.
+ * <p>
+ * <pre>
+ * Wildcard expressions:
+ * * -- corresponds to any amount of characters.
+ * ? -- corresponds to any single character.
+ * </pre>
+ */
+
public class GlobMatcher {
/**
return this;
}
+ /**
+ * Check if the string has a suffix.
+ * @param suffix to check
+ * @return true if the string has the suffix
+ */
public boolean hasSuffix(String suffix) {
return contains(suffix, getLength() - suffix.length());
}
+ /**
+ * Check if the string has a prefix.
+ * @param prefix to check
+ * @return true if the string has the prefix
+ */
public boolean hasPrefix(String prefix) {
return contains(prefix, 0);
}