Compare commits
No commits in common. "0.17" and "0.3" have entirely different histories.
130 changed files with 13340 additions and 3042 deletions
21
.classpath
21
.classpath
|
@ -2,21 +2,18 @@
|
|||
<classpath>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry kind="src" path="test"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-17">
|
||||
<classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.module.container"/>
|
||||
<classpathentry kind="lib" path="lib/hamcrest-core-1.3.jar"/>
|
||||
<classpathentry kind="lib" path="lib/junit-4.11.jar" sourcepath="lib/junit-4.11-sources.jar"/>
|
||||
<classpathentry kind="lib" path="lib/log4j-1.2.17.jar"/>
|
||||
<classpathentry kind="lib" path="lib/slf4j-api-1.7.5.jar" sourcepath="lib/slf4j-api-1.7.5-sources.jar"/>
|
||||
<classpathentry kind="lib" path="lib/slf4j-log4j12-1.7.5.jar"/>
|
||||
<classpathentry kind="lib" path="lib/commons-lang3-3.1.jar" sourcepath="lib/commons-lang3-3.1-sources.jar"/>
|
||||
<classpathentry kind="lib" path="lib/joda-time-2.3.jar"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6">
|
||||
<attributes>
|
||||
<attribute name="module" value="true"/>
|
||||
<attribute name="owner.project.facets" value="java"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.module.container"/>
|
||||
<classpathentry kind="lib" path="lib/Logs/log4j-1.2.17.jar" sourcepath="lib/Logs/log4j-1.2.17-source.zip"/>
|
||||
<classpathentry kind="lib" path="lib/UnitTesting/hamcrest-core-1.3.jar" sourcepath="lib/UnitTesting/hamcrest-core-1.3-sources.jar"/>
|
||||
<classpathentry kind="lib" path="lib/Logs/slf4j-api-1.7.25.jar" sourcepath="lib/Logs/slf4j-api-1.7.25-sources.jar"/>
|
||||
<classpathentry kind="lib" path="lib/Logs/slf4j-log4j12-1.7.25.jar" sourcepath="lib/Logs/slf4j-log4j12-1.7.25-sources.jar"/>
|
||||
<classpathentry kind="lib" path="lib/UnitTesting/junit-4.12.jar" sourcepath="lib/Logs/log4j-1.2.17-source.zip"/>
|
||||
<classpathentry kind="lib" path="lib/commons-io-2.11.0.jar" sourcepath="lib/commons-io-2.11.0-sources.jar"/>
|
||||
<classpathentry kind="lib" path="lib/commons-lang3-3.12.0.jar" sourcepath="lib/commons-lang3-3.12.0-sources.jar"/>
|
||||
<classpathentry kind="lib" path="lib/devinsy-cmdexec-0.17.0.jar" sourcepath="lib/devinsy-cmdexec-0.17.0-sources.zip"/>
|
||||
<classpathentry kind="lib" path="lib/devinsy-strings-0.17.0.jar" sourcepath="lib/devinsy-strings-0.17.0-sources.zip"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
||||
|
|
2
.project
2
.project
|
@ -1,6 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>devinsy-unix</name>
|
||||
<name>devinsy-utils</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
|
|
|
@ -1,29 +1,15 @@
|
|||
eclipse.preferences.version=1
|
||||
org.eclipse.jdt.core.codeComplete.argumentPrefixes=
|
||||
org.eclipse.jdt.core.codeComplete.argumentSuffixes=
|
||||
org.eclipse.jdt.core.codeComplete.fieldPrefixes=
|
||||
org.eclipse.jdt.core.codeComplete.fieldSuffixes=
|
||||
org.eclipse.jdt.core.codeComplete.localPrefixes=
|
||||
org.eclipse.jdt.core.codeComplete.localSuffixes=
|
||||
org.eclipse.jdt.core.codeComplete.staticFieldPrefixes=
|
||||
org.eclipse.jdt.core.codeComplete.staticFieldSuffixes=
|
||||
org.eclipse.jdt.core.codeComplete.staticFinalFieldPrefixes=
|
||||
org.eclipse.jdt.core.codeComplete.staticFinalFieldSuffixes=
|
||||
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
|
||||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=17
|
||||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
|
||||
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
|
||||
org.eclipse.jdt.core.compiler.compliance=17
|
||||
org.eclipse.jdt.core.compiler.compliance=1.6
|
||||
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
|
||||
org.eclipse.jdt.core.compiler.debug.localVariable=generate
|
||||
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
|
||||
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
|
||||
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
|
||||
org.eclipse.jdt.core.compiler.release=enabled
|
||||
org.eclipse.jdt.core.compiler.source=17
|
||||
org.eclipse.jdt.core.compiler.source=1.6
|
||||
org.eclipse.jdt.core.formatter.align_type_members_on_columns=false
|
||||
org.eclipse.jdt.core.formatter.alignment_for_additive_operator=16
|
||||
org.eclipse.jdt.core.formatter.alignment_for_arguments_in_allocation_expression=16
|
||||
org.eclipse.jdt.core.formatter.alignment_for_arguments_in_annotation=0
|
||||
org.eclipse.jdt.core.formatter.alignment_for_arguments_in_enum_constant=16
|
||||
|
@ -31,20 +17,17 @@ org.eclipse.jdt.core.formatter.alignment_for_arguments_in_explicit_constructor_c
|
|||
org.eclipse.jdt.core.formatter.alignment_for_arguments_in_method_invocation=16
|
||||
org.eclipse.jdt.core.formatter.alignment_for_arguments_in_qualified_allocation_expression=16
|
||||
org.eclipse.jdt.core.formatter.alignment_for_assignment=0
|
||||
org.eclipse.jdt.core.formatter.alignment_for_bitwise_operator=16
|
||||
org.eclipse.jdt.core.formatter.alignment_for_binary_expression=16
|
||||
org.eclipse.jdt.core.formatter.alignment_for_compact_if=16
|
||||
org.eclipse.jdt.core.formatter.alignment_for_conditional_expression=80
|
||||
org.eclipse.jdt.core.formatter.alignment_for_enum_constants=49
|
||||
org.eclipse.jdt.core.formatter.alignment_for_expressions_in_array_initializer=16
|
||||
org.eclipse.jdt.core.formatter.alignment_for_logical_operator=16
|
||||
org.eclipse.jdt.core.formatter.alignment_for_method_declaration=0
|
||||
org.eclipse.jdt.core.formatter.alignment_for_multiple_fields=16
|
||||
org.eclipse.jdt.core.formatter.alignment_for_multiplicative_operator=16
|
||||
org.eclipse.jdt.core.formatter.alignment_for_parameters_in_constructor_declaration=16
|
||||
org.eclipse.jdt.core.formatter.alignment_for_parameters_in_method_declaration=16
|
||||
org.eclipse.jdt.core.formatter.alignment_for_resources_in_try=80
|
||||
org.eclipse.jdt.core.formatter.alignment_for_selector_in_method_invocation=16
|
||||
org.eclipse.jdt.core.formatter.alignment_for_string_concatenation=16
|
||||
org.eclipse.jdt.core.formatter.alignment_for_superclass_in_type_declaration=16
|
||||
org.eclipse.jdt.core.formatter.alignment_for_superinterfaces_in_enum_declaration=16
|
||||
org.eclipse.jdt.core.formatter.alignment_for_superinterfaces_in_type_declaration=16
|
||||
|
@ -128,12 +111,11 @@ org.eclipse.jdt.core.formatter.insert_new_line_in_empty_enum_constant=insert
|
|||
org.eclipse.jdt.core.formatter.insert_new_line_in_empty_enum_declaration=insert
|
||||
org.eclipse.jdt.core.formatter.insert_new_line_in_empty_method_body=insert
|
||||
org.eclipse.jdt.core.formatter.insert_new_line_in_empty_type_declaration=insert
|
||||
org.eclipse.jdt.core.formatter.insert_space_after_additive_operator=insert
|
||||
org.eclipse.jdt.core.formatter.insert_space_after_and_in_type_parameter=insert
|
||||
org.eclipse.jdt.core.formatter.insert_space_after_assignment_operator=insert
|
||||
org.eclipse.jdt.core.formatter.insert_space_after_at_in_annotation=do not insert
|
||||
org.eclipse.jdt.core.formatter.insert_space_after_at_in_annotation_type_declaration=do not insert
|
||||
org.eclipse.jdt.core.formatter.insert_space_after_bitwise_operator=insert
|
||||
org.eclipse.jdt.core.formatter.insert_space_after_binary_operator=insert
|
||||
org.eclipse.jdt.core.formatter.insert_space_after_closing_angle_bracket_in_type_arguments=insert
|
||||
org.eclipse.jdt.core.formatter.insert_space_after_closing_angle_bracket_in_type_parameters=insert
|
||||
org.eclipse.jdt.core.formatter.insert_space_after_closing_brace_in_block=insert
|
||||
|
@ -163,8 +145,6 @@ org.eclipse.jdt.core.formatter.insert_space_after_comma_in_superinterfaces=inser
|
|||
org.eclipse.jdt.core.formatter.insert_space_after_comma_in_type_arguments=insert
|
||||
org.eclipse.jdt.core.formatter.insert_space_after_comma_in_type_parameters=insert
|
||||
org.eclipse.jdt.core.formatter.insert_space_after_ellipsis=insert
|
||||
org.eclipse.jdt.core.formatter.insert_space_after_logical_operator=insert
|
||||
org.eclipse.jdt.core.formatter.insert_space_after_multiplicative_operator=insert
|
||||
org.eclipse.jdt.core.formatter.insert_space_after_opening_angle_bracket_in_parameterized_type_reference=do not insert
|
||||
org.eclipse.jdt.core.formatter.insert_space_after_opening_angle_bracket_in_type_arguments=do not insert
|
||||
org.eclipse.jdt.core.formatter.insert_space_after_opening_angle_bracket_in_type_parameters=do not insert
|
||||
|
@ -189,17 +169,13 @@ org.eclipse.jdt.core.formatter.insert_space_after_postfix_operator=do not insert
|
|||
org.eclipse.jdt.core.formatter.insert_space_after_prefix_operator=do not insert
|
||||
org.eclipse.jdt.core.formatter.insert_space_after_question_in_conditional=insert
|
||||
org.eclipse.jdt.core.formatter.insert_space_after_question_in_wildcard=do not insert
|
||||
org.eclipse.jdt.core.formatter.insert_space_after_relational_operator=insert
|
||||
org.eclipse.jdt.core.formatter.insert_space_after_semicolon_in_for=insert
|
||||
org.eclipse.jdt.core.formatter.insert_space_after_semicolon_in_try_resources=insert
|
||||
org.eclipse.jdt.core.formatter.insert_space_after_shift_operator=insert
|
||||
org.eclipse.jdt.core.formatter.insert_space_after_string_concatenation=insert
|
||||
org.eclipse.jdt.core.formatter.insert_space_after_unary_operator=do not insert
|
||||
org.eclipse.jdt.core.formatter.insert_space_before_additive_operator=insert
|
||||
org.eclipse.jdt.core.formatter.insert_space_before_and_in_type_parameter=insert
|
||||
org.eclipse.jdt.core.formatter.insert_space_before_assignment_operator=insert
|
||||
org.eclipse.jdt.core.formatter.insert_space_before_at_in_annotation_type_declaration=insert
|
||||
org.eclipse.jdt.core.formatter.insert_space_before_bitwise_operator=insert
|
||||
org.eclipse.jdt.core.formatter.insert_space_before_binary_operator=insert
|
||||
org.eclipse.jdt.core.formatter.insert_space_before_closing_angle_bracket_in_parameterized_type_reference=do not insert
|
||||
org.eclipse.jdt.core.formatter.insert_space_before_closing_angle_bracket_in_type_arguments=do not insert
|
||||
org.eclipse.jdt.core.formatter.insert_space_before_closing_angle_bracket_in_type_parameters=do not insert
|
||||
|
@ -246,8 +222,6 @@ org.eclipse.jdt.core.formatter.insert_space_before_comma_in_superinterfaces=do n
|
|||
org.eclipse.jdt.core.formatter.insert_space_before_comma_in_type_arguments=do not insert
|
||||
org.eclipse.jdt.core.formatter.insert_space_before_comma_in_type_parameters=do not insert
|
||||
org.eclipse.jdt.core.formatter.insert_space_before_ellipsis=do not insert
|
||||
org.eclipse.jdt.core.formatter.insert_space_before_logical_operator=insert
|
||||
org.eclipse.jdt.core.formatter.insert_space_before_multiplicative_operator=insert
|
||||
org.eclipse.jdt.core.formatter.insert_space_before_opening_angle_bracket_in_parameterized_type_reference=do not insert
|
||||
org.eclipse.jdt.core.formatter.insert_space_before_opening_angle_bracket_in_type_arguments=do not insert
|
||||
org.eclipse.jdt.core.formatter.insert_space_before_opening_angle_bracket_in_type_parameters=do not insert
|
||||
|
@ -284,12 +258,9 @@ org.eclipse.jdt.core.formatter.insert_space_before_postfix_operator=do not inser
|
|||
org.eclipse.jdt.core.formatter.insert_space_before_prefix_operator=do not insert
|
||||
org.eclipse.jdt.core.formatter.insert_space_before_question_in_conditional=insert
|
||||
org.eclipse.jdt.core.formatter.insert_space_before_question_in_wildcard=do not insert
|
||||
org.eclipse.jdt.core.formatter.insert_space_before_relational_operator=insert
|
||||
org.eclipse.jdt.core.formatter.insert_space_before_semicolon=do not insert
|
||||
org.eclipse.jdt.core.formatter.insert_space_before_semicolon_in_for=do not insert
|
||||
org.eclipse.jdt.core.formatter.insert_space_before_semicolon_in_try_resources=do not insert
|
||||
org.eclipse.jdt.core.formatter.insert_space_before_shift_operator=insert
|
||||
org.eclipse.jdt.core.formatter.insert_space_before_string_concatenation=insert
|
||||
org.eclipse.jdt.core.formatter.insert_space_before_unary_operator=do not insert
|
||||
org.eclipse.jdt.core.formatter.insert_space_between_brackets_in_array_type_reference=do not insert
|
||||
org.eclipse.jdt.core.formatter.insert_space_between_empty_braces_in_array_initializer=do not insert
|
||||
|
@ -315,10 +286,6 @@ org.eclipse.jdt.core.formatter.tabulation.char=tab
|
|||
org.eclipse.jdt.core.formatter.tabulation.size=4
|
||||
org.eclipse.jdt.core.formatter.use_on_off_tags=false
|
||||
org.eclipse.jdt.core.formatter.use_tabs_only_for_leading_indentations=false
|
||||
org.eclipse.jdt.core.formatter.wrap_before_additive_operator=true
|
||||
org.eclipse.jdt.core.formatter.wrap_before_bitwise_operator=true
|
||||
org.eclipse.jdt.core.formatter.wrap_before_logical_operator=true
|
||||
org.eclipse.jdt.core.formatter.wrap_before_multiplicative_operator=true
|
||||
org.eclipse.jdt.core.formatter.wrap_before_binary_operator=true
|
||||
org.eclipse.jdt.core.formatter.wrap_before_or_operator_multicatch=true
|
||||
org.eclipse.jdt.core.formatter.wrap_before_string_concatenation=true
|
||||
org.eclipse.jdt.core.formatter.wrap_outer_expressions_when_nested=true
|
||||
|
|
|
@ -49,16 +49,12 @@ cleanup.use_this_for_non_static_field_access=true
|
|||
cleanup.use_this_for_non_static_field_access_only_if_necessary=false
|
||||
cleanup.use_this_for_non_static_method_access=false
|
||||
cleanup.use_this_for_non_static_method_access_only_if_necessary=true
|
||||
cleanup_profile=_Devinsy-unix
|
||||
cleanup_profile=_Devinsy-utils
|
||||
cleanup_settings_version=2
|
||||
eclipse.preferences.version=1
|
||||
editor_save_participant_org.eclipse.jdt.ui.postsavelistener.cleanup=true
|
||||
formatter_profile=_Devinsy-unix
|
||||
formatter_profile=_Devinsy-utils
|
||||
formatter_settings_version=12
|
||||
org.eclipse.jdt.ui.exception.name=exception
|
||||
org.eclipse.jdt.ui.gettersetter.use.is=true
|
||||
org.eclipse.jdt.ui.keywordthis=false
|
||||
org.eclipse.jdt.ui.overrideannotation=true
|
||||
sp_cleanup.add_default_serial_version_id=true
|
||||
sp_cleanup.add_generated_serial_version_id=false
|
||||
sp_cleanup.add_missing_annotations=true
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<faceted-project>
|
||||
<runtime name="Apache Tomcat v7.0"/>
|
||||
<fixed facet="jst.java"/>
|
||||
<fixed facet="jst.utility"/>
|
||||
<installed facet="jst.java" version="6.0"/>
|
||||
<installed facet="jst.utility" version="1.0"/>
|
||||
<installed facet="jst.java" version="17"/>
|
||||
</faceted-project>
|
||||
|
|
12
README.md
12
README.md
|
@ -1,7 +1,9 @@
|
|||
|
||||
# DEVINSY Utils
|
||||
|
||||
DEVINSY Utils provides an easy, local and fast Java util classes.
|
||||
GeoNames is geographical database under a creative commons attribution license.
|
||||
|
||||
FlatDB4GeoNames provides an easy, local and fast Java access to GeoNames data.
|
||||
|
||||
## Author
|
||||
Christian Pierre MOMON <christian.momon@devinsy.fr>
|
||||
|
@ -11,12 +13,14 @@ This software is released under the GNU LGPL.
|
|||
|
||||
## Requirements
|
||||
|
||||
- Java 11
|
||||
- Eclipse 4.16 (202006).
|
||||
FlatDB4Geonames requires:
|
||||
- Java 1.6
|
||||
- Eclipse Kepler
|
||||
|
||||
## Context
|
||||
Several useful Java classes.
|
||||
|
||||
|
||||
## Conclusion
|
||||
Enjoy and use DEVINSY Utils. For questions, improvement, issues: christian.momon@devinsy.fr
|
||||
Enjoy and use FlatDB4GeoNames. For questions, improvement, issues: christian.momon@devinsy.fr
|
||||
|
|
@ -1,3 +1,3 @@
|
|||
#Build Number for ANT. Do not edit!
|
||||
#Thu Oct 14 18:12:14 CEST 2021
|
||||
build.number=1
|
||||
#Wed Apr 19 22:56:10 CEST 2017
|
||||
build.number=4
|
||||
|
|
|
@ -1,3 +1,3 @@
|
|||
product.name=devinsy-unix
|
||||
product.name=devinsy-utils
|
||||
product.revision.major=0
|
||||
product.revision.minor=17
|
||||
product.revision.minor=3
|
||||
|
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
18
lib/README
18
lib/README
|
@ -1,17 +1,9 @@
|
|||
== Description of used libraries.
|
||||
|
||||
Standards:
|
||||
- commons-io
|
||||
- commons-lang: useful tools (StringUtils…)
|
||||
- devinsy-strings
|
||||
- devinsy-cmdexec
|
||||
|
||||
Logging:
|
||||
Description of used libraries:
|
||||
- commons-lang: useful tools (StringUtils...)
|
||||
- hamcrest-core: required by junit
|
||||
- joda-time: useful DateTime library
|
||||
- junit: unit tests API
|
||||
- log4j: log API
|
||||
- slf4j-api: facade log API
|
||||
- slf4j-log4j12: adaptation layer between slf4j and log4j
|
||||
|
||||
Testing:
|
||||
- hamcrest-core: required by JUnit
|
||||
- junit: unit tests API
|
||||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
lib/commons-lang3-3.1-sources.jar
Normal file
BIN
lib/commons-lang3-3.1-sources.jar
Normal file
Binary file not shown.
BIN
lib/commons-lang3-3.1.jar
Normal file
BIN
lib/commons-lang3-3.1.jar
Normal file
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
lib/joda-time-2.3-sources.jar
Normal file
BIN
lib/joda-time-2.3-sources.jar
Normal file
Binary file not shown.
BIN
lib/joda-time-2.3.jar
Normal file
BIN
lib/joda-time-2.3.jar
Normal file
Binary file not shown.
BIN
lib/junit-4.11-sources.jar
Normal file
BIN
lib/junit-4.11-sources.jar
Normal file
Binary file not shown.
BIN
lib/junit-4.11.jar
Normal file
BIN
lib/junit-4.11.jar
Normal file
Binary file not shown.
BIN
lib/slf4j-api-1.7.5-sources.jar
Normal file
BIN
lib/slf4j-api-1.7.5-sources.jar
Normal file
Binary file not shown.
BIN
lib/slf4j-api-1.7.5.jar
Normal file
BIN
lib/slf4j-api-1.7.5.jar
Normal file
Binary file not shown.
BIN
lib/slf4j-log4j12-1.7.5-sources.jar
Normal file
BIN
lib/slf4j-log4j12-1.7.5-sources.jar
Normal file
Binary file not shown.
BIN
lib/slf4j-log4j12-1.7.5.jar
Normal file
BIN
lib/slf4j-log4j12-1.7.5.jar
Normal file
Binary file not shown.
3
src/META-INF/MANIFEST.MF
Normal file
3
src/META-INF/MANIFEST.MF
Normal file
|
@ -0,0 +1,3 @@
|
|||
Manifest-Version: 1.0
|
||||
Class-Path:
|
||||
|
|
@ -1,192 +0,0 @@
|
|||
/*
|
||||
* Copyright (C) 2006-2010,2013-2014,2017-2018 Christian Pierre MOMON
|
||||
*
|
||||
* This file is part of Devinsy-unix.
|
||||
*
|
||||
* Devinsy-unix is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Devinsy-unix is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Devinsy-unix. If not, see <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
package fr.devinsy.unix;
|
||||
|
||||
import java.util.Vector;
|
||||
|
||||
import fr.devinsy.unix.util.ChangeableFile;
|
||||
|
||||
/**
|
||||
* The Class EtcGroupFile.
|
||||
*
|
||||
* @author Christian Pierre MOMON (christian.momon@devinsy.fr)
|
||||
*/
|
||||
public class EtcGroupFile extends ChangeableFile
|
||||
{
|
||||
// static private final Logger logger =
|
||||
// LoggerFactory.getLogger(EtcGroupFile.class);
|
||||
|
||||
/** Lazy-loading with holder */
|
||||
private static class SingletonLoader
|
||||
{
|
||||
private static final EtcGroupFile instance = new EtcGroupFile();
|
||||
}
|
||||
|
||||
private Groups groups;
|
||||
|
||||
/**
|
||||
* Instantiates a new etc group file.
|
||||
*/
|
||||
private EtcGroupFile()
|
||||
{
|
||||
super("/etc/group");
|
||||
this.groups = new Groups();
|
||||
}
|
||||
|
||||
/**
|
||||
* This method checks if contains a target name.
|
||||
*
|
||||
* @param name
|
||||
* the name
|
||||
* @return true, if successful
|
||||
*/
|
||||
public boolean contains(final String name)
|
||||
{
|
||||
boolean result;
|
||||
|
||||
refresh();
|
||||
|
||||
result = this.groups.contains(name);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the.
|
||||
*
|
||||
* @param gid
|
||||
* the gid
|
||||
* @return the group
|
||||
*/
|
||||
public Group get(final int gid)
|
||||
{
|
||||
Group result;
|
||||
|
||||
refresh();
|
||||
|
||||
result = this.groups.getByGid(gid);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a group by name.
|
||||
*
|
||||
* @param name
|
||||
* the name
|
||||
* @return the group
|
||||
*/
|
||||
public Group get(final String name)
|
||||
{
|
||||
Group result;
|
||||
|
||||
refresh();
|
||||
|
||||
result = this.groups.getByName(name);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the login groups.
|
||||
*
|
||||
* @param login
|
||||
* the login
|
||||
* @return the login groups
|
||||
*/
|
||||
public Vector<String> getLoginGroups(final String login)
|
||||
{
|
||||
Vector<String> result;
|
||||
|
||||
refresh();
|
||||
|
||||
result = this.groups.getLoginGroups(login);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the login groups string.
|
||||
*
|
||||
* @param login
|
||||
* the login
|
||||
* @return the login groups string
|
||||
*/
|
||||
public String getLoginGroupsString(final String login)
|
||||
{
|
||||
String result;
|
||||
|
||||
refresh();
|
||||
|
||||
result = this.groups.getLoginGroupsString(login);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see fr.devinsy.unix.util.ChangeableFile#refresh()
|
||||
*/
|
||||
@Override
|
||||
public void refresh()
|
||||
{
|
||||
if (hasChanged())
|
||||
{
|
||||
this.groups.clear();
|
||||
this.groups.addAll(EtcGroupFileReader.load());
|
||||
|
||||
super.refresh();
|
||||
}
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see java.lang.Object#toString()
|
||||
*/
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
String result;
|
||||
|
||||
result = this.groups.toString();
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method returns the singleton instance.
|
||||
*
|
||||
* @return the EtcGroupFile instance
|
||||
*/
|
||||
public static EtcGroupFile instance()
|
||||
{
|
||||
EtcGroupFile result;
|
||||
|
||||
result = SingletonLoader.instance;
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
// ////////////////////////////////////////////////////////////////////////
|
|
@ -1,145 +0,0 @@
|
|||
/*
|
||||
* Copyright (C) 2006-2021 Christian Pierre MOMON
|
||||
*
|
||||
* This file is part of Devinsy-unix.
|
||||
*
|
||||
* Devinsy-unix is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Devinsy-unix is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Devinsy-unix. If not, see <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
package fr.devinsy.unix;
|
||||
|
||||
import fr.devinsy.unix.util.ChangeableFile;
|
||||
|
||||
/**
|
||||
* The Class EtcPasswdFile.
|
||||
*/
|
||||
public class EtcPasswdFile extends ChangeableFile
|
||||
{
|
||||
/** Lazy-loading with holder */
|
||||
private static class SingletonLoader
|
||||
{
|
||||
private static final EtcPasswdFile instance = new EtcPasswdFile();
|
||||
}
|
||||
|
||||
private Users users;
|
||||
|
||||
/**
|
||||
* Instantiates a new EtcPasswdFile class.
|
||||
*/
|
||||
private EtcPasswdFile()
|
||||
{
|
||||
super("/etc/passwd");
|
||||
this.users = new Users();
|
||||
}
|
||||
|
||||
/**
|
||||
* Contains.
|
||||
*
|
||||
* @param login
|
||||
* the login
|
||||
* @return true, if successful
|
||||
*/
|
||||
public boolean contains(final String login)
|
||||
{
|
||||
boolean result;
|
||||
|
||||
refresh();
|
||||
|
||||
result = this.users.contains(login);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the.
|
||||
*
|
||||
* @param uid
|
||||
* the uid
|
||||
* @return the user
|
||||
*/
|
||||
public User get(final int uid)
|
||||
{
|
||||
User result;
|
||||
|
||||
refresh();
|
||||
|
||||
result = this.users.getByUid(uid);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the.
|
||||
*
|
||||
* @param login
|
||||
* the login
|
||||
* @return the user
|
||||
*/
|
||||
public User get(final String login)
|
||||
{
|
||||
User result;
|
||||
|
||||
refresh();
|
||||
|
||||
result = this.users.getByLogin(login);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see fr.devinsy.unix.util.ChangeableFile#refresh()
|
||||
*/
|
||||
@Override
|
||||
public void refresh()
|
||||
{
|
||||
if (hasChanged())
|
||||
{
|
||||
this.users.clear();
|
||||
this.users.addAll(EtcPasswdFileReader.load());
|
||||
|
||||
super.refresh();
|
||||
}
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see java.lang.Object#toString()
|
||||
*/
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
String result;
|
||||
|
||||
result = this.users.toString();
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Instance.
|
||||
*
|
||||
* @return the EtcPasswdFile instance
|
||||
*/
|
||||
public static EtcPasswdFile instance()
|
||||
{
|
||||
EtcPasswdFile result;
|
||||
|
||||
result = SingletonLoader.instance;
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
}
|
|
@ -1,961 +0,0 @@
|
|||
/*
|
||||
* Copyright (C) 2006-2021 Christian Pierre MOMON
|
||||
*
|
||||
* This file is part of Devinsy-unix.
|
||||
*
|
||||
* Devinsy-unix is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Devinsy-unix is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Devinsy-unix. If not, see <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
package fr.devinsy.unix;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Vector;
|
||||
|
||||
import org.apache.commons.lang3.ArrayUtils;
|
||||
import org.apache.commons.lang3.SystemUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import fr.devinsy.cmdexec.CmdExecException;
|
||||
import fr.devinsy.cmdexec.CmdExecUtils;
|
||||
import fr.devinsy.strings.StringsUtils;
|
||||
import fr.devinsy.unix.acl.Acl;
|
||||
import fr.devinsy.unix.acl.AclManager;
|
||||
|
||||
/**
|
||||
* The Class Unix.
|
||||
*
|
||||
*/
|
||||
public class Unix
|
||||
{
|
||||
private static Logger logger = LoggerFactory.getLogger(Unix.class);
|
||||
|
||||
public static final String BASH = "/bin/bash";
|
||||
public static final String SUDO = "/usr/bin/sudo";
|
||||
|
||||
/**
|
||||
* Append to file.
|
||||
*
|
||||
* @param text
|
||||
* the text
|
||||
* @param path
|
||||
* the path
|
||||
* @throws UnixException
|
||||
* the unix exception
|
||||
*/
|
||||
public static void appendToFile(final String text, final String path) throws UnixException
|
||||
{
|
||||
if ((text == null) || (text.length() == 0) || (path == null) || (path.length() == 0))
|
||||
{
|
||||
throw new IllegalArgumentException("Parameter undefined: [" + text + "][" + path + "].");
|
||||
}
|
||||
else
|
||||
{
|
||||
try
|
||||
{
|
||||
bash(String.format("echo \"%s\" >> %s", text, path));
|
||||
}
|
||||
catch (CmdExecException exception)
|
||||
{
|
||||
throw new UnixException("Error detected appending text to file [" + path + "].", exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Bash.
|
||||
*
|
||||
* @param command
|
||||
* the command
|
||||
* @return the string
|
||||
* @throws CmdExecException
|
||||
* the cmd exec exception
|
||||
*/
|
||||
public static String bash(final String command) throws CmdExecException
|
||||
{
|
||||
String result;
|
||||
|
||||
if (SystemUtils.IS_OS_UNIX)
|
||||
{
|
||||
result = CmdExecUtils.run(BASH + " -c " + command);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new CmdExecException("bash call requires GNU/Linux operating system.");
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Bash.
|
||||
*
|
||||
* @param command
|
||||
* the command
|
||||
* @return the string
|
||||
* @throws CmdExecException
|
||||
* the cmd exec exception
|
||||
*/
|
||||
public static String bash(final String... command) throws CmdExecException
|
||||
{
|
||||
String result;
|
||||
|
||||
if (SystemUtils.IS_OS_UNIX)
|
||||
{
|
||||
result = CmdExecUtils.run(BASH + " -c " + command);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new CmdExecException("bash call requires GNU/Linux operating system.");
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Chmod.
|
||||
*
|
||||
* @param changes
|
||||
* the changes
|
||||
* @param path
|
||||
* the path
|
||||
* @throws UnixException
|
||||
* the unix exception
|
||||
*/
|
||||
public static void chmod(final String changes, final String path) throws UnixException
|
||||
{
|
||||
if ((changes == null) || (changes.length() == 0) || (path == null) || (path.length() == 0))
|
||||
{
|
||||
throw new IllegalArgumentException("Parameter undefined: [" + changes + "][" + path + "].");
|
||||
}
|
||||
else if (!new File(path).exists())
|
||||
{
|
||||
throw new IllegalArgumentException("Path not found: [" + path + "].");
|
||||
}
|
||||
else
|
||||
{
|
||||
try
|
||||
{
|
||||
// CmdExec.run(BASH, "chmod", changes, path);
|
||||
bash("chmod", changes, path);
|
||||
}
|
||||
catch (CmdExecException exception)
|
||||
{
|
||||
throw new UnixException("Error running chmod command for [" + changes + "][" + path + "].", exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear acl.
|
||||
*
|
||||
* @param id
|
||||
* the id
|
||||
* @param filePathName
|
||||
* the file path name
|
||||
* @throws UnixException
|
||||
* the unix exception
|
||||
*/
|
||||
public static void clearAcl(final String id, final String filePathName) throws UnixException
|
||||
{
|
||||
AclManager.clearId(id, filePathName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear acl group.
|
||||
*
|
||||
* @param group
|
||||
* the group
|
||||
* @param filePathName
|
||||
* the file path name
|
||||
* @throws UnixException
|
||||
* the unix exception
|
||||
*/
|
||||
public static void clearAclGroup(final String group, final String filePathName) throws UnixException
|
||||
{
|
||||
AclManager.clearGroup(group, filePathName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear acl user.
|
||||
*
|
||||
* @param login
|
||||
* the login
|
||||
* @param filePathName
|
||||
* the file path name
|
||||
* @throws UnixException
|
||||
* the unix exception
|
||||
*/
|
||||
public static void clearAclUser(final String login, final String filePathName) throws UnixException
|
||||
{
|
||||
AclManager.clearUser(login, filePathName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the user account.
|
||||
*
|
||||
* @param login
|
||||
* the login
|
||||
* @throws UnixException
|
||||
* the unix exception
|
||||
*/
|
||||
public static void createUserAccount(final String login) throws UnixException
|
||||
{
|
||||
if ((login == null) || (login.length() == 0))
|
||||
{
|
||||
throw new IllegalArgumentException("Login parameter undefined.");
|
||||
}
|
||||
else
|
||||
{
|
||||
createUserAccount(login, login);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the user account.
|
||||
*
|
||||
* @param login
|
||||
* the login
|
||||
* @param name
|
||||
* the name
|
||||
* @throws UnixException
|
||||
* the unix exception
|
||||
*/
|
||||
public static void createUserAccount(final String login, final String name) throws UnixException
|
||||
{
|
||||
if ((login == null) || (login.length() == 0))
|
||||
{
|
||||
throw new IllegalArgumentException("Login parameter undefined.");
|
||||
}
|
||||
else if ((name == null) || (name.length() == 0))
|
||||
{
|
||||
throw new IllegalArgumentException("Name parameter undefined.");
|
||||
}
|
||||
else
|
||||
{
|
||||
createUserAccount(login, name, "/home/" + login);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the user account.
|
||||
*
|
||||
* @param login
|
||||
* the login
|
||||
* @param name
|
||||
* the name
|
||||
* @param home
|
||||
* the home
|
||||
* @throws UnixException
|
||||
* the unix exception
|
||||
*/
|
||||
public static void createUserAccount(final String login, final String name, final String home) throws UnixException
|
||||
{
|
||||
if ((login == null) || (login.length() == 0))
|
||||
{
|
||||
throw new IllegalArgumentException("Login parameter undefined.");
|
||||
}
|
||||
else if ((name == null) || (name.length() == 0))
|
||||
{
|
||||
throw new IllegalArgumentException("Name parameter undefined.");
|
||||
}
|
||||
else if ((home == null) || (home.length() == 0))
|
||||
{
|
||||
throw new IllegalArgumentException("Home parameter undefined.");
|
||||
}
|
||||
else
|
||||
{
|
||||
try
|
||||
{
|
||||
logger.info("Creating user account for [{}].", login);
|
||||
// CmdExec.run(SUDO, "/usr/sbin/useradd", "-m", "-c", name,
|
||||
// "-d", home, login);
|
||||
sudo("/usr/sbin/useradd", "-m", "-c", name, "-d", home, login);
|
||||
logger.info("User account created for [{}].", login);
|
||||
}
|
||||
catch (CmdExecException exception)
|
||||
{
|
||||
throw new UnixException("Error detected creating user account [" + login + "].", exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the user account.
|
||||
*
|
||||
* @param login
|
||||
* the login
|
||||
* @param name
|
||||
* the name
|
||||
* @param home
|
||||
* the home
|
||||
* @param password
|
||||
* the password
|
||||
* @throws UnixException
|
||||
* the unix exception
|
||||
*/
|
||||
public static void createUserAccount(final String login, final String name, final String home, final String password) throws UnixException
|
||||
{
|
||||
if ((password == null) || (password.length() == 0))
|
||||
{
|
||||
throw new IllegalArgumentException("Password parameter undefined.");
|
||||
}
|
||||
else if (Unix.isLogin(login))
|
||||
{
|
||||
throw new IllegalArgumentException("Login [" + login + "] already in use");
|
||||
}
|
||||
else
|
||||
{
|
||||
createUserAccount(login, name, home);
|
||||
setPassword(login, password);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete group.
|
||||
*
|
||||
* @param group
|
||||
* the group
|
||||
* @throws UnixException
|
||||
* the unix exception
|
||||
*/
|
||||
public static void deleteGroup(final String group) throws UnixException
|
||||
{
|
||||
if ((group == null) || (group.length() == 0))
|
||||
{
|
||||
throw new IllegalArgumentException("Group parameter undefined.");
|
||||
}
|
||||
else
|
||||
{
|
||||
try
|
||||
{
|
||||
logger.info("Deleting group for [{}].", group);
|
||||
// CmdExec.run(SUDO + " groupdel " + group);
|
||||
sudo("groupdel " + group);
|
||||
logger.info("Group deleted for [{}].", group);
|
||||
}
|
||||
catch (CmdExecException exception)
|
||||
{
|
||||
throw new UnixException("Error running groupdel command for group [" + group + "].", exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete user account.
|
||||
*
|
||||
* @param login
|
||||
* the login
|
||||
* @throws UnixException
|
||||
* the unix exception
|
||||
*/
|
||||
public static void deleteUserAccount(final String login) throws UnixException
|
||||
{
|
||||
if ((login == null) || (login.length() == 0))
|
||||
{
|
||||
throw new IllegalArgumentException("Login parameter undefined.");
|
||||
}
|
||||
else
|
||||
{
|
||||
try
|
||||
{
|
||||
logger.info("Deleting user account for [{}].", login);
|
||||
// CmdExec.run(SUDO + " /usr/sbin/userdel " + login);
|
||||
sudo("/usr/sbin/userdel " + login);
|
||||
logger.info("User account delted for [{}].", login);
|
||||
}
|
||||
catch (CmdExecException exception)
|
||||
{
|
||||
throw new UnixException("Error running userdel command for login [" + login + "].", exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sudo.
|
||||
*
|
||||
* @param command
|
||||
* the command
|
||||
* @return the string
|
||||
* @throws CmdExecException
|
||||
* the cmd exec exception
|
||||
*/
|
||||
public static String exec(final String command) throws CmdExecException
|
||||
{
|
||||
String result;
|
||||
|
||||
if (SystemUtils.IS_OS_UNIX)
|
||||
{
|
||||
result = CmdExecUtils.run(command);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new CmdExecException("sudo call requires GNU/Linux operating system.");
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the acl data.
|
||||
*
|
||||
* @param filePathName
|
||||
* the file path name
|
||||
* @return the acl data
|
||||
* @throws UnixException
|
||||
* the unix exception
|
||||
*/
|
||||
public static String getAclData(final String filePathName) throws UnixException
|
||||
{
|
||||
String result;
|
||||
|
||||
result = AclManager.getAclData(filePathName);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the acl users.
|
||||
*
|
||||
* @param filePathName
|
||||
* the file path name
|
||||
* @return the acl users
|
||||
* @throws UnixException
|
||||
* the unix exception
|
||||
*/
|
||||
public static String[] getAclUsers(final String filePathName) throws UnixException
|
||||
{
|
||||
String[] result;
|
||||
|
||||
Acl acl = AclManager.getAcl(filePathName);
|
||||
|
||||
result = acl.currentAcl().getUserIds();
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if is group.
|
||||
*
|
||||
* @param groupName
|
||||
* the group name
|
||||
* @return true, if is group
|
||||
*/
|
||||
public static boolean isGroup(final String groupName)
|
||||
{
|
||||
boolean result;
|
||||
|
||||
result = EtcGroupFile.instance().contains(groupName);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if is login.
|
||||
*
|
||||
* @param login
|
||||
* the login
|
||||
* @return true, if is login
|
||||
*/
|
||||
public static boolean isLogin(final String login)
|
||||
{
|
||||
boolean result;
|
||||
|
||||
result = EtcPasswdFile.instance().contains(login);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Link.
|
||||
*
|
||||
* @param sourcePath
|
||||
* the source path
|
||||
* @param targetPath
|
||||
* the target path
|
||||
* @throws UnixException
|
||||
* the unix exception
|
||||
*/
|
||||
public static void link(final String sourcePath, final String targetPath) throws UnixException
|
||||
{
|
||||
logger.info("[{}][{}]", sourcePath, targetPath);
|
||||
|
||||
if ((sourcePath == null) || (sourcePath.length() == 0) || (targetPath == null) || (targetPath.length() == 0))
|
||||
{
|
||||
throw new IllegalArgumentException("Parameter undefined: [" + sourcePath + "][" + targetPath + "].");
|
||||
}
|
||||
else
|
||||
{
|
||||
File sourceFile = new File(sourcePath);
|
||||
File targetFile = new File(targetPath);
|
||||
|
||||
if (!sourceFile.exists())
|
||||
{
|
||||
throw new IllegalArgumentException("Source does not exist: [" + sourcePath + "].");
|
||||
}
|
||||
else if ((targetFile.exists()) && (!targetFile.isDirectory()))
|
||||
{
|
||||
throw new IllegalArgumentException("Target already exists: [" + targetPath + "].");
|
||||
}
|
||||
else
|
||||
{
|
||||
try
|
||||
{
|
||||
CmdExecUtils.run(SUDO, "ln", "-s", sourcePath, targetPath);
|
||||
}
|
||||
catch (CmdExecException exception)
|
||||
{
|
||||
throw new UnixException("Error detected linking [" + sourcePath + "][" + targetPath + "].", exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Modify login.
|
||||
*
|
||||
* @param sourceLogin
|
||||
* the source login
|
||||
* @param targetLogin
|
||||
* the target login
|
||||
* @param sourceHomeDirectory
|
||||
* the source home directory
|
||||
* @throws UnixException
|
||||
* the unix exception
|
||||
*/
|
||||
public static void modifyLogin(final String sourceLogin, final String targetLogin, final String sourceHomeDirectory) throws UnixException
|
||||
{
|
||||
logger.info("Starting login modifying: [{}] -> [{}]", sourceLogin, targetLogin);
|
||||
|
||||
if ((sourceLogin == null) || (sourceLogin.length() == 0))
|
||||
{
|
||||
throw new IllegalArgumentException("Original login parameters undefined");
|
||||
}
|
||||
else if ((targetLogin == null) || (targetLogin.length() == 0))
|
||||
{
|
||||
throw new IllegalArgumentException("New login parameters undefined");
|
||||
}
|
||||
else if (!Unix.isLogin(sourceLogin))
|
||||
{
|
||||
throw new IllegalArgumentException("Original login unknow: [" + sourceLogin + "].");
|
||||
}
|
||||
else if (Unix.isLogin(targetLogin))
|
||||
{
|
||||
throw new IllegalArgumentException("New login unknow: [" + targetLogin + "].");
|
||||
}
|
||||
else if (sourceHomeDirectory == null)
|
||||
{
|
||||
throw new IllegalArgumentException("sourceHomeDirectory parameter undefined, thus no home directory move.");
|
||||
}
|
||||
else if (!new File(sourceHomeDirectory).exists())
|
||||
{
|
||||
throw new IllegalArgumentException("Source home directory does not exist: [" + sourceHomeDirectory + "].");
|
||||
}
|
||||
else
|
||||
{
|
||||
File targetHomeDirectory = new File(new File(sourceHomeDirectory).getParent(), targetLogin);
|
||||
// String targetHomeDirectory = new
|
||||
// File(sourceHomeDirectory).getParent().toString() + "/" +
|
||||
// targetLogin;
|
||||
if (targetHomeDirectory.exists())
|
||||
{
|
||||
throw new IllegalArgumentException("Target home directory already exists: [" + targetHomeDirectory + "].");
|
||||
}
|
||||
else
|
||||
{
|
||||
try
|
||||
{
|
||||
logger.info("Login modifying: [" + sourceLogin + "] -> [" + targetLogin + "]");
|
||||
sudo("usermod -l " + targetLogin + " " + sourceLogin);
|
||||
logger.info("Login modified: [" + sourceLogin + "] -> [" + targetLogin + "]");
|
||||
}
|
||||
catch (CmdExecException exception)
|
||||
{
|
||||
throw new UnixException("Login modification failed for [" + sourceLogin + "].", exception);
|
||||
}
|
||||
|
||||
// TOOD problem;
|
||||
try
|
||||
{
|
||||
logger.info("Renaming home directory: [{}] -> [{}]", sourceHomeDirectory, targetLogin);
|
||||
sudo(" mv " + sourceHomeDirectory + " " + targetHomeDirectory);
|
||||
logger.info("Home directory renamed: [{}] -> [{}]", sourceHomeDirectory, targetLogin);
|
||||
}
|
||||
catch (CmdExecException exception)
|
||||
{
|
||||
throw new UnixException("Home directory rename failed for [" + sourceHomeDirectory + "].", exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
logger.info("Login modifying done: [{}] -> [{}]", sourceLogin, targetLogin);
|
||||
}
|
||||
|
||||
/**
|
||||
* Recursive chmod.
|
||||
*
|
||||
* @param changes
|
||||
* the changes
|
||||
* @param path
|
||||
* the path
|
||||
* @throws UnixException
|
||||
* the unix exception
|
||||
*/
|
||||
public static void recursiveChmod(final String changes, final String path) throws UnixException
|
||||
{
|
||||
if ((changes == null) || (changes.length() == 0) || (path == null) || (path.length() == 0))
|
||||
{
|
||||
throw new IllegalArgumentException("Parameter undefined: [" + changes + "][" + path + "].");
|
||||
}
|
||||
else if (!new File(path).exists())
|
||||
{
|
||||
throw new IllegalArgumentException("Path not found: [" + path + "].");
|
||||
}
|
||||
else
|
||||
{
|
||||
try
|
||||
{
|
||||
sudo("chmod", "-R", changes, path);
|
||||
}
|
||||
catch (CmdExecException exception)
|
||||
{
|
||||
throw new UnixException("Error running recursive chmod command for [" + changes + "][" + path + "].", exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Rename group.
|
||||
*
|
||||
* @param sourceGroup
|
||||
* the source group
|
||||
* @param targetGroup
|
||||
* the target group
|
||||
* @throws UnixException
|
||||
* the unix exception
|
||||
*/
|
||||
public static void renameGroup(final String sourceGroup, final String targetGroup) throws UnixException
|
||||
{
|
||||
logger.info("Starting group renaming: [" + sourceGroup + "] -> [" + targetGroup + "]");
|
||||
if ((sourceGroup == null) || (sourceGroup.length() == 0))
|
||||
{
|
||||
throw new IllegalArgumentException("Original group name parameters undefined");
|
||||
}
|
||||
else if ((targetGroup == null) || (targetGroup.length() == 0))
|
||||
{
|
||||
throw new IllegalArgumentException("New group name parameters undefined");
|
||||
}
|
||||
else if (!Unix.isGroup(sourceGroup))
|
||||
{
|
||||
throw new IllegalArgumentException("Original group unknow: [" + sourceGroup + "].");
|
||||
}
|
||||
else if (Unix.isGroup(targetGroup))
|
||||
{
|
||||
throw new IllegalArgumentException("New group unknow: [" + targetGroup + "].");
|
||||
}
|
||||
else
|
||||
{
|
||||
try
|
||||
{
|
||||
logger.info("Login modifying: [{}] -> [{}]", sourceGroup, targetGroup);
|
||||
sudo("groupmod -n " + targetGroup + " " + sourceGroup);
|
||||
logger.info("Login modified: [{}] -> [{}]", sourceGroup, targetGroup);
|
||||
}
|
||||
catch (CmdExecException exception)
|
||||
{
|
||||
throw new UnixException("Group renaming failed for [" + sourceGroup + "].", exception);
|
||||
}
|
||||
}
|
||||
logger.info("Group renaming done: [{}] -> [{}]", sourceGroup, targetGroup);
|
||||
}
|
||||
|
||||
/**
|
||||
* Search group.
|
||||
*
|
||||
* @param groupName
|
||||
* the group name
|
||||
* @return the group
|
||||
*/
|
||||
public static Group searchGroup(final String groupName)
|
||||
{
|
||||
Group result;
|
||||
|
||||
result = EtcGroupFile.instance().get(groupName);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Search login.
|
||||
*
|
||||
* @param login
|
||||
* the login
|
||||
* @return the user
|
||||
*/
|
||||
public static User searchLogin(final String login)
|
||||
{
|
||||
User result;
|
||||
|
||||
result = EtcPasswdFile.instance().get(login);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Search login groups.
|
||||
*
|
||||
* @param login
|
||||
* the login
|
||||
* @return the vector
|
||||
*/
|
||||
public static Vector<String> searchLoginGroups(final String login)
|
||||
{
|
||||
Vector<String> result;
|
||||
|
||||
result = EtcGroupFile.instance().getLoginGroups(login);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the facl.
|
||||
*
|
||||
* @param args
|
||||
* the new facl
|
||||
* @throws UnixException
|
||||
* the unix exception
|
||||
*/
|
||||
public static void setfacl(final String... args) throws UnixException
|
||||
{
|
||||
try
|
||||
{
|
||||
String[] command = ArrayUtils.insert(0, args, "setfacl");
|
||||
sudo(command);
|
||||
}
|
||||
catch (CmdExecException exception)
|
||||
{
|
||||
throw new UnixException("Error running setfacl command for " + StringsUtils.toStringWithBrackets(args) + ":" + exception.getMessage() + ".", exception);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* As 'passwd' command has not the option '--stdin' in all systems (eg.
|
||||
* Debian), this method uses the 'chpasswd' command.
|
||||
*
|
||||
* @param login
|
||||
* the login
|
||||
* @param newPassword
|
||||
* the new password
|
||||
* @throws UnixException
|
||||
* the exception
|
||||
*/
|
||||
public static void setPassword(final String login, final String newPassword) throws UnixException
|
||||
{
|
||||
if ((login == null) || (login.length() == 0))
|
||||
{
|
||||
throw new IllegalArgumentException("Login parameter undefined.");
|
||||
}
|
||||
else if (newPassword == null)
|
||||
{
|
||||
throw new IllegalArgumentException("New password parameter undefined.");
|
||||
}
|
||||
else
|
||||
{
|
||||
try
|
||||
{
|
||||
logger.info("Password setting for [{}].", login);
|
||||
sudoBash("echo \"" + login + ":" + newPassword + "\"| chpasswd ");
|
||||
logger.info("Password set for [{}].", login);
|
||||
}
|
||||
catch (CmdExecException exception)
|
||||
{
|
||||
throw new UnixException("Error detected on setting of the new password for [" + login + "].", exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* chfn [ -f full-name ] [ username ].
|
||||
*
|
||||
* @param login
|
||||
* the login
|
||||
* @param newRealName
|
||||
* the new real name
|
||||
* @throws UnixException
|
||||
* the unix exception
|
||||
*/
|
||||
public static void setRealName(final String login, final String newRealName) throws UnixException
|
||||
{
|
||||
if ((login == null) || (login.length() == 0))
|
||||
{
|
||||
throw new IllegalArgumentException("Login parameter undefined.");
|
||||
}
|
||||
else if (newRealName == null)
|
||||
{
|
||||
throw new IllegalArgumentException("New real name parameter undefined.");
|
||||
}
|
||||
else
|
||||
{
|
||||
try
|
||||
{
|
||||
logger.info("Real name changing for user [{}]", login);
|
||||
CmdExecUtils.run(SUDO, "chfn", "-f", newRealName, login);
|
||||
}
|
||||
catch (CmdExecException exception)
|
||||
{
|
||||
throw new UnixException("Error detected on setting of the new real name for [" + login + "].", exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sudo.
|
||||
*
|
||||
* @param command
|
||||
* the command
|
||||
* @return the string
|
||||
* @throws CmdExecException
|
||||
* the cmd exec exception
|
||||
*/
|
||||
public static String sudo(final String command) throws CmdExecException
|
||||
{
|
||||
String result;
|
||||
|
||||
if (SystemUtils.IS_OS_UNIX)
|
||||
{
|
||||
result = CmdExecUtils.run(SUDO + " " + command);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new CmdExecException("sudo call requires GNU/Linux operating system.");
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sudo.
|
||||
*
|
||||
* @param command
|
||||
* the command
|
||||
* @return the string
|
||||
* @throws CmdExecException
|
||||
* the cmd exec exception
|
||||
*/
|
||||
public static String sudo(final String... command) throws CmdExecException
|
||||
{
|
||||
String result;
|
||||
|
||||
if (SystemUtils.IS_OS_UNIX)
|
||||
{
|
||||
String[] newCommand = ArrayUtils.insert(0, command, SUDO);
|
||||
result = CmdExecUtils.run(newCommand);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new CmdExecException("sudo call requires GNU/Linux operating system.");
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sudo.
|
||||
*
|
||||
* @param command
|
||||
* the command
|
||||
* @return the string
|
||||
* @throws CmdExecException
|
||||
* the cmd exec exception
|
||||
*/
|
||||
public static String sudoBash(final String command) throws CmdExecException
|
||||
{
|
||||
String result;
|
||||
|
||||
if (SystemUtils.IS_OS_UNIX)
|
||||
{
|
||||
result = CmdExecUtils.run(BASH + " -c " + command);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new CmdExecException("sudo call requires GNU/Linux operating system.");
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sudo chmod.
|
||||
*
|
||||
* @param changes
|
||||
* the changes
|
||||
* @param path
|
||||
* the path
|
||||
* @throws UnixException
|
||||
* the unix exception
|
||||
*/
|
||||
public static void sudoChmod(final String changes, final String path) throws UnixException
|
||||
{
|
||||
if ((changes == null) || (changes.length() == 0) || (path == null) || (path.length() == 0))
|
||||
{
|
||||
throw new IllegalArgumentException("Parameter undefined: [" + changes + "][" + path + "].");
|
||||
}
|
||||
else if (!new File(path).exists())
|
||||
{
|
||||
throw new IllegalArgumentException("Path not found: [" + path + "].");
|
||||
}
|
||||
else
|
||||
{
|
||||
try
|
||||
{
|
||||
// CmdExec.run(SUDO, "chmod", changes, path);
|
||||
sudo("chmod", changes, path);
|
||||
}
|
||||
catch (CmdExecException exception)
|
||||
{
|
||||
throw new UnixException("Error running chmod command for [" + changes + "][" + path + "].", exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Unlink.
|
||||
*
|
||||
* @param filePathName
|
||||
* the file path name
|
||||
*/
|
||||
public static void unlink(final String filePathName)
|
||||
{
|
||||
logger.info("unlink [filePathName=s{}]", filePathName);
|
||||
if (filePathName == null)
|
||||
{
|
||||
throw new IllegalArgumentException("Parameter undefined: [" + filePathName + "].");
|
||||
}
|
||||
else
|
||||
{
|
||||
new File(filePathName).delete();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,72 +0,0 @@
|
|||
/*
|
||||
* Copyright (C) 2016-2018 Christian Pierre MOMON
|
||||
*
|
||||
* This file is part of Devinsy-unix.
|
||||
*
|
||||
* Devinsy-unix is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Devinsy-unix is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Devinsy-unix. If not, see <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
package fr.devinsy.unix;
|
||||
|
||||
/**
|
||||
* The Class UnixException.
|
||||
*
|
||||
* @author Christian Pierre MOMON (christian.momon@devinsy.fr)
|
||||
*/
|
||||
public class UnixException extends Exception
|
||||
{
|
||||
private static final long serialVersionUID = 5469332331366485779L;
|
||||
|
||||
/**
|
||||
* Instantiates a new UnixException.
|
||||
*/
|
||||
public UnixException()
|
||||
{
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* Instantiates a new UnixException.
|
||||
*
|
||||
* @param message
|
||||
* the message
|
||||
*/
|
||||
public UnixException(final String message)
|
||||
{
|
||||
super(message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Instantiates a new UnixException.
|
||||
*
|
||||
* @param message
|
||||
* the message
|
||||
* @param cause
|
||||
* the cause
|
||||
*/
|
||||
public UnixException(final String message, final Throwable cause)
|
||||
{
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
/**
|
||||
* Instantiates a new UnixException.
|
||||
*
|
||||
* @param cause
|
||||
* the cause
|
||||
*/
|
||||
public UnixException(final Throwable cause)
|
||||
{
|
||||
super(cause);
|
||||
}
|
||||
}
|
|
@ -1,56 +0,0 @@
|
|||
/*
|
||||
* Copyright (C) 2017-2018 Christian Pierre MOMON
|
||||
*
|
||||
* This file is part of Devinsy-unix.
|
||||
*
|
||||
* Devinsy-unix is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Devinsy-unix is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Devinsy-unix. If not, see <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
package fr.devinsy.unix.demo;
|
||||
|
||||
import fr.devinsy.strings.StringList;
|
||||
|
||||
/**
|
||||
* The Class UnixDemo.
|
||||
*/
|
||||
public class UnixDemo
|
||||
{
|
||||
/**
|
||||
* The main method.
|
||||
*
|
||||
* @param args
|
||||
* the arguments
|
||||
*/
|
||||
public static void main(final String[] args)
|
||||
{
|
||||
// #1
|
||||
{
|
||||
StringList strings = new StringList();
|
||||
|
||||
strings.appendln("========== DEMO #1;");
|
||||
strings.appendln();
|
||||
|
||||
System.out.println(strings.toString());
|
||||
}
|
||||
|
||||
// #2
|
||||
{
|
||||
StringList strings = new StringList();
|
||||
|
||||
strings.appendln("========== DEMO #2;");
|
||||
strings.appendln();
|
||||
|
||||
System.out.println(strings.toString());
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,340 +0,0 @@
|
|||
/*
|
||||
* Copyright (C) 2017-2021 Christian Pierre MOMON
|
||||
*
|
||||
* This file is part of Devinsy-unix.
|
||||
*
|
||||
* Devinsy-unix is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Devinsy-unix is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Devinsy-unix. If not, see <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
package fr.devinsy.unix.linux;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.commons.lang3.time.DurationFormatUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import fr.devinsy.unix.util.Property;
|
||||
import fr.devinsy.unix.util.PropertyList;
|
||||
|
||||
/**
|
||||
* The Class Linux.
|
||||
*
|
||||
*/
|
||||
public class Linux
|
||||
{
|
||||
private static Logger logger = LoggerFactory.getLogger(Linux.class);
|
||||
|
||||
private static final Pattern PROC_MEMINFO_PATTERN = Pattern.compile("^(\\S+):\\s+(\\d+)(\\s\\w{2})?$");
|
||||
|
||||
private static final String REDHAT_RELEASE_FILE = "/etc/redhat-release";
|
||||
private static final String DEBIAN_RELEASE_FILE = "/etc/debian_version";
|
||||
private static final String SUSE_RELEASE_FILE = "/etc/suse-foo";
|
||||
|
||||
/**
|
||||
* Instantiates a new linux.
|
||||
*/
|
||||
private Linux()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Read /proc/meminfo properties.
|
||||
*
|
||||
* @return the property list
|
||||
* @throws IOException
|
||||
* Signals that an I/O exception has occurred.
|
||||
*/
|
||||
public static PropertyList getProcMeminfoProperties() throws IOException
|
||||
{
|
||||
PropertyList result;
|
||||
|
||||
result = new PropertyList(50);
|
||||
|
||||
String buffer = readProcMeminfoFile();
|
||||
String[] lines = buffer.split("\n");
|
||||
for (String line : lines)
|
||||
{
|
||||
Matcher matcher = PROC_MEMINFO_PATTERN.matcher(line);
|
||||
|
||||
if ((matcher.matches()) && ((matcher.groupCount() == 2) || (matcher.groupCount() == 3)))
|
||||
{
|
||||
result.add(matcher.group(1), matcher.group(2));
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new IOException("Unknown line format in /proc/meminfo file.");
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a /proc/meminfo summary.
|
||||
*
|
||||
* @return the string
|
||||
* @throws IOException
|
||||
* Signals that an I/O exception has occurred.
|
||||
*/
|
||||
public static PropertyList getProcMeminfoSummaryProperties() throws IOException
|
||||
{
|
||||
PropertyList result;
|
||||
|
||||
PropertyList meminfo = getProcMeminfoProperties();
|
||||
|
||||
result = new PropertyList(10);
|
||||
|
||||
result.add(meminfo.get(0));
|
||||
result.add(meminfo.get(1));
|
||||
result.add(meminfo.get(2));
|
||||
result.add(meminfo.get(3));
|
||||
result.add(meminfo.get(4));
|
||||
result.add(meminfo.get(5));
|
||||
result.add(meminfo.get(6));
|
||||
result.add(meminfo.get(7));
|
||||
result.add(meminfo.get(13));
|
||||
result.add(meminfo.get(14));
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the /proc/uptime duration.
|
||||
*
|
||||
* @return the /proc/uptime duration
|
||||
* @throws IOException
|
||||
* Signals that an I/O exception has occurred.
|
||||
*/
|
||||
public static long getProcUptimeDuration() throws IOException
|
||||
{
|
||||
long result;
|
||||
|
||||
String procUptime = readProcUptimeFile();
|
||||
|
||||
result = (long) Double.parseDouble(procUptime.split("\\s")[0]);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the /proc/uptime duration properties.
|
||||
*
|
||||
* @return the /proc/uptime duration properties
|
||||
* @throws IOException
|
||||
* Signals that an I/O exception has occurred.
|
||||
*/
|
||||
public static Property getProcUptimeDurationProperties() throws IOException
|
||||
{
|
||||
Property result;
|
||||
|
||||
String procUptime = readProcUptimeFile();
|
||||
|
||||
result = new Property("uptime.duration", procUptime.split("\\s")[0]);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the /proc/uptime duration properties.
|
||||
*
|
||||
* @return the /proc/uptime duration properties
|
||||
* @throws IOException
|
||||
* Signals that an I/O exception has occurred.
|
||||
*/
|
||||
public static PropertyList getProcUptimeProperties() throws IOException
|
||||
{
|
||||
PropertyList result;
|
||||
|
||||
result = new PropertyList();
|
||||
|
||||
String procUptime = readProcUptimeFile();
|
||||
String[] data = procUptime.split("\\s");
|
||||
|
||||
result.add("uptime.duration", data[0]);
|
||||
|
||||
long duration = Long.parseLong(data[0]);
|
||||
String humanDuration = DurationFormatUtils.formatDuration(duration * 1000, "dd'd'HH':'mm':'ss");
|
||||
result.add("uptime.duration.human", humanDuration);
|
||||
|
||||
result.add("uptime.idle", data[1]);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the release distribution properties.
|
||||
*
|
||||
* @return the release distribution properties
|
||||
* @throws IOException
|
||||
* Signals that an I/O exception has occurred.
|
||||
*/
|
||||
public static Property getReleaseDistributionProperties() throws IOException
|
||||
{
|
||||
Property result;
|
||||
|
||||
String buffer = readReleaseDistributionFile();
|
||||
|
||||
result = new Property("distribution.release", buffer);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read /proc/cpu processor 0.
|
||||
*
|
||||
* @return the string
|
||||
* @throws IOException
|
||||
* Signals that an I/O exception has occurred.
|
||||
*/
|
||||
public static String readProcCpuinfo0() throws IOException
|
||||
{
|
||||
String result;
|
||||
|
||||
result = readProcCpuinfoFile();
|
||||
|
||||
int separator = StringUtils.indexOf(result, "\n\n");
|
||||
|
||||
if (separator != -1)
|
||||
{
|
||||
result = result.substring(0, separator);
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read /proc/cpuinfo file.
|
||||
*
|
||||
* @return the string
|
||||
* @throws IOException
|
||||
* Signals that an I/O exception has occurred.
|
||||
*/
|
||||
public static String readProcCpuinfoFile() throws IOException
|
||||
{
|
||||
String result;
|
||||
|
||||
result = FileUtils.readFileToString(new File("/proc/cpuinfo"), StandardCharsets.UTF_8);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read /proc/meminfo file.
|
||||
*
|
||||
* @return the string
|
||||
* @throws IOException
|
||||
* Signals that an I/O exception has occurred.
|
||||
*/
|
||||
public static String readProcMeminfoFile() throws IOException
|
||||
{
|
||||
String result;
|
||||
|
||||
result = FileUtils.readFileToString(new File("/proc/meminfo"), StandardCharsets.UTF_8);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read /proc/uptime file.
|
||||
*
|
||||
* @return the string
|
||||
* @throws IOException
|
||||
* Signals that an I/O exception has occurred.
|
||||
*/
|
||||
public static String readProcUptimeFile() throws IOException
|
||||
{
|
||||
String result;
|
||||
|
||||
result = FileUtils.readFileToString(new File("/proc/uptime"), StandardCharsets.UTF_8);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read /proc/version file.
|
||||
*
|
||||
* @return the string
|
||||
* @throws IOException
|
||||
* Signals that an I/O exception has occurred.
|
||||
*/
|
||||
public static String readProcVersionFile() throws IOException
|
||||
{
|
||||
String result;
|
||||
|
||||
result = FileUtils.readFileToString(new File("/proc/version"), StandardCharsets.UTF_8);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read release distribution file.
|
||||
*
|
||||
* @return the string
|
||||
*/
|
||||
public static String readReleaseDistributionFile()
|
||||
{
|
||||
String result;
|
||||
|
||||
try
|
||||
{
|
||||
File redhatFile = new File(REDHAT_RELEASE_FILE);
|
||||
File debianFile = new File(DEBIAN_RELEASE_FILE);
|
||||
File suseFile = new File(SUSE_RELEASE_FILE);
|
||||
|
||||
if (debianFile.exists())
|
||||
{
|
||||
result = "Debian " + FileUtils.readFileToString(debianFile, StandardCharsets.UTF_8);
|
||||
}
|
||||
else if (redhatFile.exists())
|
||||
{
|
||||
result = FileUtils.readFileToString(redhatFile, StandardCharsets.UTF_8);
|
||||
}
|
||||
else if (suseFile.exists())
|
||||
{
|
||||
// TODO
|
||||
result = "SUSE " + FileUtils.readFileToString(debianFile, StandardCharsets.UTF_8);
|
||||
}
|
||||
else
|
||||
{
|
||||
result = null;
|
||||
}
|
||||
|
||||
}
|
||||
catch (IOException exception)
|
||||
{
|
||||
logger.warn("Failed to read distribution infos.", exception);
|
||||
result = null;
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
}
|
|
@ -1,113 +0,0 @@
|
|||
/*
|
||||
* Copyright (C) 2006-2021 Christian Pierre MOMON
|
||||
*
|
||||
* This file is part of Devinsy-unix.
|
||||
*
|
||||
* Devinsy-unix is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Devinsy-unix is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Devinsy-unix. If not, see <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
package fr.devinsy.unix.util;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
/**
|
||||
* The Class ChangeableFile.
|
||||
*/
|
||||
public class ChangeableFile
|
||||
{
|
||||
private File sourceFile;
|
||||
private long sourceTime;
|
||||
|
||||
/**
|
||||
* Instantiates a new cached file.
|
||||
*
|
||||
* @param source
|
||||
* the file to cache
|
||||
*/
|
||||
public ChangeableFile(final File source)
|
||||
{
|
||||
this.sourceFile = source;
|
||||
this.sourceTime = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Instantiates a new cached file.
|
||||
*
|
||||
* @param fileName
|
||||
* the file name
|
||||
*/
|
||||
public ChangeableFile(final String fileName)
|
||||
{
|
||||
this(new File(fileName));
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the changeable file.
|
||||
*
|
||||
* @return the changeable file
|
||||
*/
|
||||
public File getFile()
|
||||
{
|
||||
File result;
|
||||
|
||||
result = this.sourceFile;
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the status.
|
||||
*
|
||||
* @return the status
|
||||
*/
|
||||
public boolean hasChanged()
|
||||
{
|
||||
boolean result;
|
||||
|
||||
if ((this.sourceFile == null) || (!this.sourceFile.exists()))
|
||||
{
|
||||
result = true;
|
||||
}
|
||||
else if (this.sourceTime == this.sourceFile.lastModified())
|
||||
{
|
||||
result = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
result = true;
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets updated.
|
||||
*/
|
||||
public void refresh()
|
||||
{
|
||||
if ((this.sourceFile != null) && (this.sourceFile.exists()))
|
||||
{
|
||||
this.sourceTime = this.sourceFile.lastModified();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets unload.
|
||||
*/
|
||||
public void unload()
|
||||
{
|
||||
this.sourceTime = 0;
|
||||
}
|
||||
}
|
|
@ -1,84 +0,0 @@
|
|||
/*
|
||||
* Copyright (C) 2017-2021 Christian Pierre MOMON
|
||||
*
|
||||
* This file is part of Devinsy-unix.
|
||||
*
|
||||
* Devinsy-unix is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Devinsy-unix is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Devinsy-unix. If not, see <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
package fr.devinsy.unix.util;
|
||||
|
||||
/**
|
||||
* The Class Property.
|
||||
*/
|
||||
public class Property
|
||||
{
|
||||
private String name;
|
||||
private String value;
|
||||
|
||||
/**
|
||||
* Utility class has not to have a constructor.
|
||||
*
|
||||
* @param name
|
||||
* the name
|
||||
* @param value
|
||||
* the value
|
||||
*/
|
||||
public Property(final String name, final String value)
|
||||
{
|
||||
this.name = name;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the name.
|
||||
*
|
||||
* @return the name
|
||||
*/
|
||||
public String getName()
|
||||
{
|
||||
return this.name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the value.
|
||||
*
|
||||
* @return the value
|
||||
*/
|
||||
public String getValue()
|
||||
{
|
||||
return this.value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the name.
|
||||
*
|
||||
* @param name
|
||||
* the new name
|
||||
*/
|
||||
public void setName(final String name)
|
||||
{
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the value.
|
||||
*
|
||||
* @param value
|
||||
* the new value
|
||||
*/
|
||||
public void setValue(final String value)
|
||||
{
|
||||
this.value = value;
|
||||
}
|
||||
}
|
|
@ -1,169 +0,0 @@
|
|||
/*
|
||||
* Copyright (C) 2017-2018 Christian Pierre MOMON
|
||||
*
|
||||
* This file is part of Devinsy-unix.
|
||||
*
|
||||
* Devinsy-unix is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Devinsy-unix is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Devinsy-unix. If not, see <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
package fr.devinsy.unix.util;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
|
||||
/**
|
||||
* The Class PropertyBeanList.
|
||||
*/
|
||||
public class PropertyList extends ArrayList<Property>
|
||||
{
|
||||
|
||||
private static final long serialVersionUID = 90604210351214011L;
|
||||
|
||||
/**
|
||||
* Instantiates a new work import beans.
|
||||
*/
|
||||
public PropertyList()
|
||||
{
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* Instantiates a new work import beans.
|
||||
*
|
||||
* @param initialCapacity
|
||||
* the initial capacity
|
||||
*/
|
||||
public PropertyList(final int initialCapacity)
|
||||
{
|
||||
super(initialCapacity);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the.
|
||||
*
|
||||
* @param key
|
||||
* the key
|
||||
* @param value
|
||||
* the value
|
||||
* @return true, if successful
|
||||
*/
|
||||
public boolean add(final String key, final String value)
|
||||
{
|
||||
boolean result;
|
||||
|
||||
result = add(new Property(key, value));
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Put.
|
||||
*
|
||||
* @param name
|
||||
* the name
|
||||
* @param value
|
||||
* the value
|
||||
* @return true, if successful
|
||||
*/
|
||||
public boolean put(final String name, final int value)
|
||||
{
|
||||
boolean result;
|
||||
|
||||
result = add(new Property(name, String.valueOf(value)));
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Put.
|
||||
*
|
||||
* @param name
|
||||
* the name
|
||||
* @param value
|
||||
* the value
|
||||
* @return true, if successful
|
||||
*/
|
||||
public boolean put(final String name, final String value)
|
||||
{
|
||||
boolean result;
|
||||
|
||||
result = add(new Property(name, value));
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse.
|
||||
*
|
||||
* @return the work import beans
|
||||
*/
|
||||
public PropertyList reverse()
|
||||
{
|
||||
PropertyList result;
|
||||
|
||||
Collections.reverse(this);
|
||||
|
||||
result = this;
|
||||
|
||||
//
|
||||
return result;
|
||||
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see java.util.AbstractCollection#toString()
|
||||
*/
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
String result;
|
||||
|
||||
StringBuffer buffer = new StringBuffer(100000);
|
||||
|
||||
for (Property property : this)
|
||||
{
|
||||
buffer.append("[").append(property.getName()).append(",").append(property.getValue()).append("]\n");
|
||||
}
|
||||
|
||||
result = buffer.toString();
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Webify value of the property list item.
|
||||
*
|
||||
* @return the property list
|
||||
*/
|
||||
public PropertyList webify()
|
||||
{
|
||||
PropertyList result;
|
||||
|
||||
for (Property property : this)
|
||||
{
|
||||
if (property.getValue() != null)
|
||||
{
|
||||
property.setValue(property.getValue().replaceAll("\n", "<br/>"));
|
||||
}
|
||||
}
|
||||
|
||||
result = this;
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
}
|
256
src/fr/devinsy/util/DataFile.java
Executable file
256
src/fr/devinsy/util/DataFile.java
Executable file
|
@ -0,0 +1,256 @@
|
|||
/**
|
||||
* Copyright (C) 2008-2010, 2013-2014 Christian Pierre MOMON
|
||||
*
|
||||
* This file is part of Devinsy-utils.
|
||||
*
|
||||
* Devinsy-utils is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Devinsy-utils is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Devinsy-utils. If not, see <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
package fr.devinsy.util;
|
||||
|
||||
/**
|
||||
* This class defines a content file.
|
||||
*
|
||||
* @author Christian Pierre MOMON (christian.momon@devinsy.fr)
|
||||
*/
|
||||
public class DataFile
|
||||
{
|
||||
public static int NOID = 0;
|
||||
public static int DEFAULT_SIZE = 0;
|
||||
|
||||
//
|
||||
protected int id;
|
||||
protected int contentId;
|
||||
protected String name;
|
||||
protected long size;
|
||||
protected byte[] data;
|
||||
protected String creationDate;
|
||||
protected String creationUser;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public DataFile()
|
||||
{
|
||||
this.id = NOID;
|
||||
this.contentId = NOID;
|
||||
this.name = null;
|
||||
this.size = this.DEFAULT_SIZE;
|
||||
this.data = null;
|
||||
this.creationDate = null;
|
||||
this.creationUser = null;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public DataFile(final int contentId, final String name, final long size, final byte[] data)
|
||||
{
|
||||
this.id = NOID;
|
||||
this.contentId = contentId;
|
||||
this.name = name;
|
||||
this.size = size;
|
||||
this.data = data;
|
||||
this.creationDate = null;
|
||||
this.creationUser = null;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public DataFile(final String name, final long size, final byte[] data)
|
||||
{
|
||||
this.id = NOID;
|
||||
this.contentId = NOID;
|
||||
this.name = name;
|
||||
this.size = size;
|
||||
this.data = data;
|
||||
this.creationDate = null;
|
||||
this.creationUser = null;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public int contentId()
|
||||
{
|
||||
int result;
|
||||
|
||||
result = this.contentId;
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public String creationDate()
|
||||
{
|
||||
String result;
|
||||
|
||||
result = this.creationDate;
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public String creationUser()
|
||||
{
|
||||
String result;
|
||||
|
||||
result = this.creationUser;
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public byte[] data()
|
||||
{
|
||||
byte[] result;
|
||||
|
||||
result = this.data;
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public int id()
|
||||
{
|
||||
int result;
|
||||
|
||||
result = this.id;
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public String name()
|
||||
{
|
||||
String result;
|
||||
|
||||
result = this.name;
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public void setContentId(final int contentId)
|
||||
{
|
||||
this.contentId = contentId;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public void setCreationDate(final String creationDate)
|
||||
{
|
||||
if (creationDate == null)
|
||||
{
|
||||
this.creationDate = "";
|
||||
}
|
||||
else
|
||||
{
|
||||
this.creationDate = creationDate;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public void setCreationUser(final String creationUser)
|
||||
{
|
||||
if (creationUser == null)
|
||||
{
|
||||
this.creationUser = "";
|
||||
}
|
||||
else
|
||||
{
|
||||
this.creationUser = creationUser;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public void setData(final byte[] data)
|
||||
{
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public void setId(final int id)
|
||||
{
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public void setName(final String name)
|
||||
{
|
||||
if (name == null)
|
||||
{
|
||||
this.name = "";
|
||||
}
|
||||
else
|
||||
{
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public void setSize(final long size)
|
||||
{
|
||||
if (size >= 0)
|
||||
{
|
||||
this.size = size;
|
||||
}
|
||||
else
|
||||
{
|
||||
this.size = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public long size()
|
||||
{
|
||||
long result;
|
||||
|
||||
result = this.size;
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
}
|
181
src/fr/devinsy/util/DataFiles.java
Executable file
181
src/fr/devinsy/util/DataFiles.java
Executable file
|
@ -0,0 +1,181 @@
|
|||
/**
|
||||
* Copyright (C) 2008-2010, 2013-2014 Christian Pierre MOMON
|
||||
*
|
||||
* This file is part of Devinsy-utils.
|
||||
*
|
||||
* Devinsy-utils is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Devinsy-utils is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Devinsy-utils. If not, see <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
package fr.devinsy.util;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* This class is a collection of DataFile objects whit some specific methods.
|
||||
*
|
||||
* @author Christian Pierre MOMON
|
||||
*/
|
||||
public class DataFiles extends ArrayList<DataFile>
|
||||
{
|
||||
private static final long serialVersionUID = -4584622422555785456L;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
*/
|
||||
public DataFiles()
|
||||
{
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param source
|
||||
*/
|
||||
public DataFiles(final DataFiles source)
|
||||
{
|
||||
super(source);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public DataFiles getByContentId(final int id)
|
||||
{
|
||||
DataFiles result = new DataFiles();
|
||||
|
||||
for (int nDataFile = 0; nDataFile < this.size(); nDataFile++)
|
||||
{
|
||||
DataFile contentFile = this.getByIndex(nDataFile);
|
||||
|
||||
if (contentFile.contentId() == id)
|
||||
{
|
||||
result.add(contentFile);
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public DataFile getById(final int id)
|
||||
{
|
||||
DataFile result = null;
|
||||
|
||||
boolean ended = false;
|
||||
int nDataFile = 0;
|
||||
while (!ended)
|
||||
{
|
||||
if (nDataFile >= this.size())
|
||||
{
|
||||
ended = true;
|
||||
result = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
DataFile contentFile = this.getByIndex(nDataFile);
|
||||
|
||||
if (id == contentFile.id())
|
||||
{
|
||||
ended = true;
|
||||
result = contentFile;
|
||||
}
|
||||
else
|
||||
{
|
||||
nDataFile += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public DataFile getByIndex(final int index)
|
||||
{
|
||||
DataFile result;
|
||||
|
||||
result = super.get(index);
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public DataFile getByName(final String name)
|
||||
{
|
||||
DataFile result = null;
|
||||
|
||||
if ((name == null) || (name.equals("")))
|
||||
{
|
||||
result = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
boolean ended = false;
|
||||
int dataFileIndex = 0;
|
||||
while (!ended)
|
||||
{
|
||||
if (dataFileIndex >= this.size())
|
||||
{
|
||||
ended = true;
|
||||
result = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
DataFile contentFile = this.getByIndex(dataFileIndex);
|
||||
|
||||
if (name.equals(contentFile.name()))
|
||||
{
|
||||
ended = true;
|
||||
result = contentFile;
|
||||
}
|
||||
else
|
||||
{
|
||||
dataFileIndex += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
StringBuffer result = new StringBuffer();
|
||||
|
||||
for (int nDataFile = 0; nDataFile < this.size(); nDataFile++)
|
||||
{
|
||||
DataFile contentFile = this.getByIndex(nDataFile);
|
||||
result.append("== " + contentFile.name() + "\n");
|
||||
result.append("contentFile " + nDataFile + " - " + contentFile.name() + "\n");
|
||||
}
|
||||
|
||||
//
|
||||
return (result.toString());
|
||||
}
|
||||
}
|
370
src/fr/devinsy/util/DateHelper.java
Executable file
370
src/fr/devinsy/util/DateHelper.java
Executable file
|
@ -0,0 +1,370 @@
|
|||
/**
|
||||
* Copyright (C) 2010, 2013-2014 Christian Pierre MOMON
|
||||
*
|
||||
* This file is part of Devinsy-utils.
|
||||
*
|
||||
* Devinsy-utils is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Devinsy-utils is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Devinsy-utils. If not, see <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
package fr.devinsy.util;
|
||||
|
||||
import java.util.Calendar;
|
||||
import java.util.GregorianCalendar;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* This class groups function to help in Calendar manipulation.
|
||||
*
|
||||
* SimpleDateFormat is not used cause does not thread safe?
|
||||
*/
|
||||
public class DateHelper
|
||||
{
|
||||
// static private final Logger logger =
|
||||
// LoggerFactory.getLogger(DateHelper.class);
|
||||
|
||||
private static final String EUROPEAN_DATE_FORMAT = "%02d/%02d/%04d";
|
||||
private static final String SHORT_EUROPEAN_DATE_FORMAT = "%02d/%02d";
|
||||
private static final String RAW_DATE_FORMAT = "%04d%02d%02d";
|
||||
private static final String ISO_DATE_FORMAT = "%04d-%02d-%02d";
|
||||
private static final String AMERICAN_DATE_FORMAT = "%02d/%02d/%04d";
|
||||
|
||||
private static final String EUROPEAN_DATE_PATTERN = "^([0123]{0,1}\\d)/([01]{0,1}\\d)/(\\d\\d\\d\\d)$";
|
||||
private static final String RAW_DATE_PATTERN = "^(\\d\\d\\d\\d)([01]\\d)([0123]\\d)$";
|
||||
private static final String ISO_DATE_PATTERN = "^(\\d\\d\\d\\d)-([01]\\d)-([0123]\\d)$";
|
||||
private static final String AMERICAN_DATE_PATTERN = "^([01]{0,1}\\d)/([0123]{0,1}\\d)/(\\d\\d\\d\\d)$";
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public static String americanFormat(final Calendar time)
|
||||
{
|
||||
String result;
|
||||
|
||||
if (time == null)
|
||||
{
|
||||
result = "";
|
||||
}
|
||||
else
|
||||
{
|
||||
result = String.format(AMERICAN_DATE_FORMAT, time.get(Calendar.MONTH) + 1, time.get(Calendar.DAY_OF_MONTH), time.get(Calendar.DAY_OF_MONTH));
|
||||
}
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public static String europeanFormat(final Calendar time)
|
||||
{
|
||||
String result;
|
||||
|
||||
if (time == null)
|
||||
{
|
||||
result = "";
|
||||
}
|
||||
else
|
||||
{
|
||||
result = String.format(EUROPEAN_DATE_FORMAT, time.get(Calendar.DAY_OF_MONTH), time.get(Calendar.MONTH) + 1, time.get(Calendar.YEAR));
|
||||
}
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public static boolean isAmericanFormat(final String date)
|
||||
{
|
||||
boolean result;
|
||||
|
||||
if (date == null)
|
||||
{
|
||||
result = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
result = date.matches(AMERICAN_DATE_PATTERN);
|
||||
}
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public static boolean isEuropeanFormat(final String date)
|
||||
{
|
||||
boolean result;
|
||||
|
||||
if (date == null)
|
||||
{
|
||||
result = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
result = date.matches(EUROPEAN_DATE_PATTERN);
|
||||
}
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public static boolean isISOFormat(final String date)
|
||||
{
|
||||
boolean result;
|
||||
|
||||
if (date == null)
|
||||
{
|
||||
result = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
result = date.matches(ISO_DATE_PATTERN);
|
||||
}
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public static String ISOFormat(final Calendar time)
|
||||
{
|
||||
String result;
|
||||
|
||||
if (time == null)
|
||||
{
|
||||
result = "";
|
||||
}
|
||||
else
|
||||
{
|
||||
result = String.format(ISO_DATE_FORMAT, time.get(Calendar.YEAR), time.get(Calendar.MONTH) + 1, time.get(Calendar.DAY_OF_MONTH));
|
||||
}
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public static boolean isRawFormat(final String date)
|
||||
{
|
||||
boolean result;
|
||||
|
||||
if (date == null)
|
||||
{
|
||||
result = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
result = date.matches(RAW_DATE_PATTERN);
|
||||
}
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public static boolean isValidDate(final String date)
|
||||
{
|
||||
boolean result;
|
||||
|
||||
if ((isEuropeanFormat(date)) || (isRawFormat(date)) || (isISOFormat(date)) || (isAmericanFormat(date)))
|
||||
{
|
||||
result = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
result = false;
|
||||
}
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public static Calendar parseAmericanDate(final String date)
|
||||
{
|
||||
Calendar result;
|
||||
|
||||
Pattern pattern = Pattern.compile(AMERICAN_DATE_PATTERN);
|
||||
Matcher matcher = pattern.matcher(date);
|
||||
|
||||
if ((matcher.find()) && (matcher.groupCount() == 3))
|
||||
{
|
||||
result = new GregorianCalendar(Integer.parseInt(matcher.group(3)), Integer.parseInt(matcher.group(1)) - 1, Integer.parseInt(matcher.group(2)));
|
||||
}
|
||||
else
|
||||
{
|
||||
result = null;
|
||||
}
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Note: European parsing test made before the American parsing one.
|
||||
*/
|
||||
public static Calendar parseDate(final String date)
|
||||
{
|
||||
Calendar result;
|
||||
|
||||
if (isEuropeanFormat(date))
|
||||
{
|
||||
result = parseEuropeanDate(date);
|
||||
}
|
||||
else if (isRawFormat(date))
|
||||
{
|
||||
result = parseRawDate(date);
|
||||
}
|
||||
else if (isISOFormat(date))
|
||||
{
|
||||
result = parseISODate(date);
|
||||
}
|
||||
else if (isAmericanFormat(date))
|
||||
{
|
||||
result = parseAmericanDate(date);
|
||||
}
|
||||
else
|
||||
{
|
||||
result = null;
|
||||
}
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public static Calendar parseEuropeanDate(final String date)
|
||||
{
|
||||
Calendar result;
|
||||
|
||||
Pattern pattern = Pattern.compile(EUROPEAN_DATE_PATTERN);
|
||||
Matcher matcher = pattern.matcher(date);
|
||||
|
||||
if ((matcher.find()) && (matcher.groupCount() == 3))
|
||||
{
|
||||
result = new GregorianCalendar(Integer.parseInt(matcher.group(3)), Integer.parseInt(matcher.group(2)) - 1, Integer.parseInt(matcher.group(1)));
|
||||
}
|
||||
else
|
||||
{
|
||||
result = null;
|
||||
}
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public static Calendar parseISODate(final String date)
|
||||
{
|
||||
Calendar result;
|
||||
|
||||
Pattern pattern = Pattern.compile(ISO_DATE_PATTERN);
|
||||
Matcher matcher = pattern.matcher(date);
|
||||
|
||||
if ((matcher.find()) && (matcher.groupCount() == 3))
|
||||
{
|
||||
result = new GregorianCalendar(Integer.parseInt(matcher.group(1)), Integer.parseInt(matcher.group(2)) - 1, Integer.parseInt(matcher.group(3)));
|
||||
}
|
||||
else
|
||||
{
|
||||
result = null;
|
||||
}
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public static Calendar parseRawDate(final String date)
|
||||
{
|
||||
Calendar result;
|
||||
|
||||
Pattern pattern = Pattern.compile(RAW_DATE_PATTERN);
|
||||
Matcher matcher = pattern.matcher(date);
|
||||
|
||||
if ((matcher.find()) && (matcher.groupCount() == 3))
|
||||
{
|
||||
result = new GregorianCalendar(Integer.parseInt(matcher.group(1)), Integer.parseInt(matcher.group(2)) - 1, Integer.parseInt(matcher.group(3)));
|
||||
}
|
||||
else
|
||||
{
|
||||
result = null;
|
||||
}
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public static String rawFormat(final Calendar time)
|
||||
{
|
||||
String result;
|
||||
|
||||
if (time == null)
|
||||
{
|
||||
result = "";
|
||||
}
|
||||
else
|
||||
{
|
||||
result = String.format(RAW_DATE_FORMAT, time.get(Calendar.YEAR), time.get(Calendar.MONTH), time.get(Calendar.DAY_OF_MONTH) + 1);
|
||||
}
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public static String shortEuropeanFormat(final Calendar time)
|
||||
{
|
||||
String result;
|
||||
|
||||
if (time == null)
|
||||
{
|
||||
result = "";
|
||||
}
|
||||
else
|
||||
{
|
||||
result = String.format(SHORT_EUROPEAN_DATE_FORMAT, time.get(Calendar.DAY_OF_MONTH), time.get(Calendar.MONTH) + 1);
|
||||
}
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
}
|
124
src/fr/devinsy/util/DateTimeHelper.java
Executable file
124
src/fr/devinsy/util/DateTimeHelper.java
Executable file
|
@ -0,0 +1,124 @@
|
|||
/**
|
||||
* Copyright (C) 2010, 2013-2014 Christian Pierre MOMON
|
||||
*
|
||||
* This file is part of Devinsy-utils.
|
||||
*
|
||||
* Devinsy-utils is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Devinsy-utils is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Devinsy-utils. If not, see <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
package fr.devinsy.util;
|
||||
|
||||
import java.util.Calendar;
|
||||
|
||||
/**
|
||||
* This class groups function to help in Calendar manipulation.
|
||||
*/
|
||||
public class DateTimeHelper
|
||||
{
|
||||
// static private final Logger logger =
|
||||
// LoggerFactory.getLogger(DateTimeHelper.class);
|
||||
|
||||
private static final String EUROPEAN_DATE_FORMAT = "%02d/%02d/%04d %02d:%02d:%02d";
|
||||
private static final String RAW_DATE_FORMAT = "%04d%02d%02d %02d:%02d:%02d";
|
||||
private static final String ISO_DATE_FORMAT = "%04d-%02d-%02d %02d:%02d:%02d";
|
||||
private static final String AMERICAN_DATE_FORMAT = "%02d/%02d/%04d %02d:%02d:%02d";
|
||||
|
||||
private static final String EUROPEAN_DATE_PATTERN = "^([0123]{0,1}\\d)/([01]{0,1}\\d)/(\\d\\d\\d\\d)$";
|
||||
private static final String RAW_DATE_PATTERN = "^(\\d\\d\\d\\d)([01]\\d)([0123]\\d)$";
|
||||
private static final String ISO_DATE_PATTERN = "^(\\d\\d\\d\\d)-([01]\\d)-([0123]\\d)$";
|
||||
private static final String AMERICAN_DATE_PATTERN = "^([01]{0,1}\\d)/([0123]{0,1}\\d)/(\\d\\d\\d\\d)$";
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public static String americanFormat(final Calendar time)
|
||||
{
|
||||
String result;
|
||||
|
||||
if (time == null)
|
||||
{
|
||||
result = "";
|
||||
}
|
||||
else
|
||||
{
|
||||
result = String.format(AMERICAN_DATE_FORMAT, time.get(Calendar.MONTH) + 1, time.get(Calendar.DAY_OF_MONTH), time.get(Calendar.YEAR), time.get(Calendar.HOUR_OF_DAY),
|
||||
time.get(Calendar.MINUTE), time.get(Calendar.SECOND));
|
||||
}
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public static String europeanFormat(final Calendar time)
|
||||
{
|
||||
String result;
|
||||
|
||||
if (time == null)
|
||||
{
|
||||
result = "";
|
||||
}
|
||||
else
|
||||
{
|
||||
result = String.format(EUROPEAN_DATE_FORMAT, time.get(Calendar.DAY_OF_MONTH), time.get(Calendar.MONTH) + 1, time.get(Calendar.YEAR), time.get(Calendar.HOUR_OF_DAY),
|
||||
time.get(Calendar.MINUTE), time.get(Calendar.SECOND));
|
||||
}
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public static String ISOFormat(final Calendar time)
|
||||
{
|
||||
String result;
|
||||
|
||||
if (time == null)
|
||||
{
|
||||
result = "";
|
||||
}
|
||||
else
|
||||
{
|
||||
result = String.format(ISO_DATE_FORMAT, time.get(Calendar.YEAR), time.get(Calendar.MONTH) + 1, time.get(Calendar.DAY_OF_MONTH), time.get(Calendar.HOUR_OF_DAY), time.get(Calendar.MINUTE),
|
||||
time.get(Calendar.SECOND));
|
||||
}
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public static String rawFormat(final Calendar time)
|
||||
{
|
||||
String result;
|
||||
|
||||
if (time == null)
|
||||
{
|
||||
result = "";
|
||||
}
|
||||
else
|
||||
{
|
||||
result = String.format(RAW_DATE_FORMAT, time.get(Calendar.YEAR), time.get(Calendar.MONTH) + 1, time.get(Calendar.DAY_OF_MONTH), time.get(Calendar.HOUR_OF_DAY), time.get(Calendar.MINUTE),
|
||||
time.get(Calendar.SECOND));
|
||||
}
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
}
|
128
src/fr/devinsy/util/Digester.java
Normal file
128
src/fr/devinsy/util/Digester.java
Normal file
|
@ -0,0 +1,128 @@
|
|||
/**
|
||||
* Copyright (C) 2006, 2010, 2013-2014 Christian Pierre MOMON
|
||||
*
|
||||
* This file is part of Devinsy-utils.
|
||||
*
|
||||
* Devinsy-utils is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Devinsy-utils is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Devinsy-utils. If not, see <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
package fr.devinsy.util;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.InputStream;
|
||||
import java.security.MessageDigest;
|
||||
|
||||
/**
|
||||
* This class is a helper to use MessageDigester class.
|
||||
*
|
||||
* @deprecated because of DigestUtils from apache-commons-codec is doing the
|
||||
* same but better.
|
||||
*/
|
||||
@Deprecated
|
||||
public class Digester
|
||||
{
|
||||
/**
|
||||
* "SHA-1", "MD5", "SHA-256", and "SHA-512"
|
||||
*/
|
||||
public static String computeHash(final String digestMethod, final File file) throws Exception
|
||||
{
|
||||
String result;
|
||||
|
||||
if ((file == null) || (!file.exists()))
|
||||
{
|
||||
result = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
// byte[] hash = null;
|
||||
|
||||
InputStream source = null;
|
||||
try
|
||||
{
|
||||
MessageDigest digester = MessageDigest.getInstance(digestMethod);
|
||||
source = new FileInputStream(file);
|
||||
boolean ended = false;
|
||||
int bytesNumber;
|
||||
byte[] buffer = new byte[100 * 1024];
|
||||
while (!ended)
|
||||
{
|
||||
bytesNumber = source.read(buffer);
|
||||
if (bytesNumber == -1)
|
||||
{
|
||||
ended = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
digester.update(buffer, 0, bytesNumber);
|
||||
}
|
||||
}
|
||||
|
||||
byte[] digest = digester.digest();
|
||||
|
||||
result = humanReadableDigest(digest);
|
||||
}
|
||||
catch (java.security.NoSuchAlgorithmException exception)
|
||||
{
|
||||
throw new Exception("Digest method unknown.", exception);
|
||||
}
|
||||
catch (java.io.FileNotFoundException exception)
|
||||
{
|
||||
throw new Exception("File not found (" + exception.getMessage() + ")", exception);
|
||||
}
|
||||
catch (java.io.IOException exception)
|
||||
{
|
||||
throw new Exception("Error reading file.", exception);
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (source != null)
|
||||
{
|
||||
source.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public static String humanReadableDigest(final byte[] digest)
|
||||
{
|
||||
String result;
|
||||
|
||||
StringBuffer hashString = new StringBuffer();
|
||||
|
||||
for (int letterIndex = 0; letterIndex < digest.length; ++letterIndex)
|
||||
{
|
||||
String hex = Integer.toHexString(digest[letterIndex]);
|
||||
if (hex.length() == 1)
|
||||
{
|
||||
hashString.append('0');
|
||||
hashString.append(hex.charAt(hex.length() - 1));
|
||||
}
|
||||
else
|
||||
{
|
||||
hashString.append(hex.substring(hex.length() - 2));
|
||||
}
|
||||
}
|
||||
|
||||
result = hashString.toString();
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
}
|
76
src/fr/devinsy/util/FileCopier.java
Normal file
76
src/fr/devinsy/util/FileCopier.java
Normal file
|
@ -0,0 +1,76 @@
|
|||
/**
|
||||
* Copyright (C) 2010, 2013-2014 Christian Pierre MOMON
|
||||
*
|
||||
* This file is part of Devinsy-utils.
|
||||
*
|
||||
* Devinsy-utils is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Devinsy-utils is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Devinsy-utils. If not, see <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
package fr.devinsy.util;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
|
||||
/**
|
||||
* Never used again. Prefer org.apache.commons.io.FileUtils class.
|
||||
*/
|
||||
public class FileCopier
|
||||
{
|
||||
public static final int BUFFER_SIZE = 4 * 1024;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public static void copy(final File source, final File target) throws Exception
|
||||
{
|
||||
if ((source == null) || (target == null))
|
||||
{
|
||||
throw new Exception("Null parameter.");
|
||||
}
|
||||
else
|
||||
{
|
||||
FileInputStream in = new FileInputStream(source);
|
||||
FileOutputStream out = new FileOutputStream(target);
|
||||
try
|
||||
{
|
||||
byte[] buffer = new byte[BUFFER_SIZE];
|
||||
boolean ended = false;
|
||||
while (!ended)
|
||||
{
|
||||
int size = in.read(buffer);
|
||||
if (size == -1)
|
||||
{
|
||||
ended = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
out.write(buffer, 0, size);
|
||||
}
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (in != null)
|
||||
{
|
||||
in.close();
|
||||
}
|
||||
|
||||
if (out != null)
|
||||
{
|
||||
out.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
452
src/fr/devinsy/util/FileIterator.java
Normal file
452
src/fr/devinsy/util/FileIterator.java
Normal file
|
@ -0,0 +1,452 @@
|
|||
/**
|
||||
* Copyright (C) 2010, 2013-2014 Christian Pierre MOMON
|
||||
*
|
||||
* This file is part of Devinsy-utils.
|
||||
*
|
||||
* Devinsy-utils is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Devinsy-utils is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Devinsy-utils. If not, see <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
package fr.devinsy.util;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Iterator;
|
||||
import java.util.Vector;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public class FileIterator extends Vector<FileIteratorState> implements Iterator<File>
|
||||
{
|
||||
private static final long serialVersionUID = 8790133455427427766L;
|
||||
|
||||
protected int currentDepth;
|
||||
protected Pattern pattern;
|
||||
protected File previous;
|
||||
protected boolean followLinks;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public FileIterator(final File root)
|
||||
{
|
||||
super();
|
||||
|
||||
String[] pathnames;
|
||||
if (root == null)
|
||||
{
|
||||
pathnames = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
pathnames = new String[1];
|
||||
pathnames[0] = root.getPath();
|
||||
}
|
||||
|
||||
init(pathnames, null, false);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public FileIterator(final File root, final String filter, final boolean followLinks)
|
||||
{
|
||||
super();
|
||||
|
||||
String[] pathnames;
|
||||
if (root == null)
|
||||
{
|
||||
pathnames = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
pathnames = new String[1];
|
||||
pathnames[0] = root.getPath();
|
||||
}
|
||||
|
||||
init(pathnames, filter, followLinks);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public FileIterator(final String pathname, final String filter, final boolean followLinks)
|
||||
{
|
||||
super();
|
||||
|
||||
String[] pathnames;
|
||||
if (pathname == null)
|
||||
{
|
||||
pathnames = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
pathnames = new String[1];
|
||||
pathnames[0] = pathname;
|
||||
}
|
||||
|
||||
init(pathnames, filter, followLinks);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public FileIterator(final String[] pathnames, final String filter, final boolean followLinks)
|
||||
{
|
||||
super();
|
||||
|
||||
init(pathnames, filter, followLinks);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public File currentFile()
|
||||
{
|
||||
File result;
|
||||
|
||||
result = this.currentState().currentFile();
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
protected FileIteratorState currentState()
|
||||
{
|
||||
FileIteratorState result;
|
||||
|
||||
result = this.get(this.currentDepth);
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public int directoryFinalCountdown()
|
||||
{
|
||||
int result;
|
||||
|
||||
result = 0;
|
||||
while (this.hasNext())
|
||||
{
|
||||
if (this.next().isDirectory())
|
||||
{
|
||||
result += 1;
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public int fileFinalCountdown()
|
||||
{
|
||||
int result;
|
||||
|
||||
result = 0;
|
||||
while (this.hasNext())
|
||||
{
|
||||
if (!this.next().isDirectory())
|
||||
{
|
||||
result += 1;
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
protected String filter()
|
||||
{
|
||||
String result;
|
||||
|
||||
if (this.pattern == null)
|
||||
{
|
||||
result = ".*";
|
||||
}
|
||||
else
|
||||
{
|
||||
result = this.pattern.toString();
|
||||
}
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public int finalCountdown()
|
||||
{
|
||||
int result;
|
||||
|
||||
result = 0;
|
||||
while (this.next() != null)
|
||||
{
|
||||
result += 1;
|
||||
}
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public boolean follow(final File file)
|
||||
{
|
||||
boolean result;
|
||||
|
||||
result = false;
|
||||
try
|
||||
{
|
||||
if ((this.followLinks) || (!isLink(file)))
|
||||
{
|
||||
result = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
result = false;
|
||||
}
|
||||
|
||||
// System.out.println("FOLLOWWWWW=[" + file.getPath() + "][" +
|
||||
// this.followLinks + "][" + isLink(file) + "][" + result + "]");
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
System.err.println("ERROR with file [" + this.next() + "]: " + exception.getMessage());
|
||||
result = false;
|
||||
}
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@Override
|
||||
public boolean hasNext()
|
||||
{
|
||||
boolean result;
|
||||
|
||||
result = this.currentState().hasNext();
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
protected void init(final String[] pathnames, final String filter, final boolean followLinks)
|
||||
{
|
||||
setFilter(filter);
|
||||
this.followLinks = followLinks;
|
||||
this.previous = null;
|
||||
|
||||
this.currentDepth = 0;
|
||||
this.add(new FileIteratorState(pathnames));
|
||||
|
||||
shift();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@Override
|
||||
public File next()
|
||||
{
|
||||
File result;
|
||||
|
||||
result = this.currentState().next();
|
||||
this.previous = result;
|
||||
if (result != null)
|
||||
{
|
||||
if (result.isDirectory())
|
||||
{
|
||||
this.push(result);
|
||||
}
|
||||
}
|
||||
|
||||
shift();
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public Pattern pattern()
|
||||
{
|
||||
Pattern result;
|
||||
|
||||
result = this.pattern;
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public void pop()
|
||||
{
|
||||
this.removeElementAt(this.currentDepth);
|
||||
this.currentDepth -= 1;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public void push(final File file)
|
||||
{
|
||||
if ((file != null) && (file.isDirectory()))
|
||||
{
|
||||
this.add(new FileIteratorState(file.listFiles()));
|
||||
this.currentDepth += 1;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@Override
|
||||
public void remove()
|
||||
{
|
||||
if (this.previous != null)
|
||||
{
|
||||
this.previous.delete();
|
||||
this.previous = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public void reset()
|
||||
{
|
||||
this.currentDepth = 0;
|
||||
this.previous = null;
|
||||
if (this.size() > 0)
|
||||
{
|
||||
this.get(0).reset();
|
||||
FileIteratorState firstState = this.get(0);
|
||||
this.removeAllElements();
|
||||
this.add(firstState);
|
||||
}
|
||||
|
||||
shift();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
protected void setFilter(final String filter)
|
||||
{
|
||||
if (filter == null)
|
||||
{
|
||||
this.pattern = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
this.pattern = Pattern.compile(filter);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set indexes to the good next item.
|
||||
*/
|
||||
public void shift()
|
||||
{
|
||||
boolean ended = false;
|
||||
while (!ended)
|
||||
{
|
||||
File next = this.currentFile();
|
||||
|
||||
if (next == null)
|
||||
{
|
||||
if (this.currentDepth == 0)
|
||||
{
|
||||
ended = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
this.pop();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (((this.pattern == null) || (this.pattern.matcher(next.getPath()).matches())) && (follow(next)))
|
||||
{
|
||||
ended = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
this.currentState().next();
|
||||
|
||||
if (next.isDirectory() && (follow(next)))
|
||||
{
|
||||
this.push(next);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
String result;
|
||||
|
||||
result = "[depth=" + this.currentDepth + "][index=" + this.get(this.currentDepth).currentIndex() + "/" + this.get(this.currentDepth).files.length + "]";
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public static boolean isLink(final File file) throws Exception
|
||||
{
|
||||
boolean result;
|
||||
|
||||
if ((file.exists()) && (file.getCanonicalPath().equals(file.getAbsolutePath())))
|
||||
{
|
||||
result = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
result = true;
|
||||
}
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
}
|
162
src/fr/devinsy/util/FileIteratorState.java
Normal file
162
src/fr/devinsy/util/FileIteratorState.java
Normal file
|
@ -0,0 +1,162 @@
|
|||
/**
|
||||
* Copyright (C) 2010, 2013-2014 Christian Pierre MOMON
|
||||
*
|
||||
* This file is part of Devinsy-utils.
|
||||
*
|
||||
* Devinsy-utils is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Devinsy-utils is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Devinsy-utils. If not, see <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
package fr.devinsy.util;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Iterator;
|
||||
|
||||
/**
|
||||
* Used by FileIterator class.
|
||||
*/
|
||||
public class FileIteratorState implements Iterator<File>
|
||||
{
|
||||
protected File[] files;
|
||||
protected int currentIndex;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public FileIteratorState(final File[] files)
|
||||
{
|
||||
// Initialize the state.
|
||||
this.currentIndex = 0;
|
||||
|
||||
if (files == null)
|
||||
{
|
||||
this.files = new File[0];
|
||||
}
|
||||
else
|
||||
{
|
||||
this.files = files;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Useful for the depth zero, otherwise parent path is lost.
|
||||
*/
|
||||
public FileIteratorState(final String[] pathnames)
|
||||
{
|
||||
// Initialize the state.
|
||||
this.currentIndex = 0;
|
||||
|
||||
this.files = new File[pathnames.length];
|
||||
for (int pathnameIndex = 0; pathnameIndex < pathnames.length; pathnameIndex++)
|
||||
{
|
||||
this.files[pathnameIndex] = new File(pathnames[pathnameIndex]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
protected File currentFile()
|
||||
{
|
||||
File result;
|
||||
|
||||
if (this.currentIndex >= this.files.length)
|
||||
{
|
||||
result = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
result = this.files[this.currentIndex];
|
||||
}
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
protected int currentIndex()
|
||||
{
|
||||
int result;
|
||||
|
||||
result = this.currentIndex;
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
protected File[] files()
|
||||
{
|
||||
File[] result;
|
||||
|
||||
result = this.files;
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@Override
|
||||
public boolean hasNext()
|
||||
{
|
||||
boolean result;
|
||||
|
||||
if (this.currentFile() == null)
|
||||
{
|
||||
result = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
result = true;
|
||||
}
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@Override
|
||||
public File next()
|
||||
{
|
||||
File result;
|
||||
|
||||
result = this.currentFile();
|
||||
this.currentIndex += 1;
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@Override
|
||||
public void remove()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public void reset()
|
||||
{
|
||||
this.currentIndex = 0;
|
||||
}
|
||||
}
|
635
src/fr/devinsy/util/FileTools.java
Normal file
635
src/fr/devinsy/util/FileTools.java
Normal file
|
@ -0,0 +1,635 @@
|
|||
/**
|
||||
* Copyright (C) 2008-2015 Christian Pierre MOMON
|
||||
*
|
||||
* This file is part of Devinsy-utils.
|
||||
*
|
||||
* Devinsy-utils is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Devinsy-utils is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Devinsy-utils. If not, see <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
package fr.devinsy.util;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.PrintWriter;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URL;
|
||||
|
||||
import fr.devinsy.util.strings.StringList;
|
||||
import fr.devinsy.util.strings.StringListUtils;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Christian Pierre MOMON (christian.momon@devinsy.fr)
|
||||
*/
|
||||
public class FileTools
|
||||
{
|
||||
public static final String DEFAULT_CHARSET_NAME = "UTF-8";
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @param fileName
|
||||
* Source.
|
||||
*
|
||||
* @return Extension value or null.
|
||||
*/
|
||||
public static String addBeforeExtension(final String fileName, final String addition)
|
||||
{
|
||||
String result;
|
||||
|
||||
if (fileName == null)
|
||||
{
|
||||
result = null;
|
||||
}
|
||||
else if (addition == null)
|
||||
{
|
||||
result = fileName;
|
||||
}
|
||||
else
|
||||
{
|
||||
//
|
||||
int separatorIndex = fileName.lastIndexOf('.');
|
||||
|
||||
//
|
||||
if (separatorIndex > 0)
|
||||
{
|
||||
result = fileName.substring(0, separatorIndex) + addition + fileName.substring(separatorIndex);
|
||||
}
|
||||
else
|
||||
{
|
||||
result = fileName + addition;
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @param file
|
||||
* Source.
|
||||
*
|
||||
* @return Extension value or null.
|
||||
*/
|
||||
public static File addToName(final File file, final String addition)
|
||||
{
|
||||
File result;
|
||||
|
||||
if (file == null)
|
||||
{
|
||||
result = null;
|
||||
}
|
||||
else if (addition == null)
|
||||
{
|
||||
result = file;
|
||||
}
|
||||
else
|
||||
{
|
||||
//
|
||||
String sourceFileName = file.getAbsolutePath();
|
||||
String targetFileName = addBeforeExtension(sourceFileName, addition);
|
||||
result = new File(targetFileName);
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the extension of a file.
|
||||
*
|
||||
* @param file
|
||||
* Source.
|
||||
*
|
||||
* @return Extension value or null.
|
||||
*/
|
||||
public static String getExtension(final File file)
|
||||
{
|
||||
String result;
|
||||
|
||||
if (file == null)
|
||||
{
|
||||
result = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
result = getExtension(file.getName());
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the extension of a file.
|
||||
*
|
||||
* <ul>
|
||||
* <li>getExtension(null) = null</li>
|
||||
* <li>getExtension("") = null</li>
|
||||
* <li>getExtension("abc") = null</li>
|
||||
* <li>getExtension("abc.efg") = "efg"</li>
|
||||
* </ul>
|
||||
*
|
||||
* @param file
|
||||
* Source.
|
||||
*
|
||||
* @return Extension value or null.
|
||||
* @deprecated See
|
||||
* <code>org.apache.commons.io.FilenameUtils.getExtension</code>
|
||||
*/
|
||||
@Deprecated
|
||||
public static String getExtension(final String fileName)
|
||||
{
|
||||
String result;
|
||||
|
||||
if (fileName == null)
|
||||
{
|
||||
result = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
int separatorIndex = fileName.lastIndexOf('.');
|
||||
if (separatorIndex > 0)
|
||||
{
|
||||
result = fileName.substring(separatorIndex + 1).toLowerCase();
|
||||
}
|
||||
else
|
||||
{
|
||||
result = null;
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param file
|
||||
* @return
|
||||
* @throws IOException
|
||||
*/
|
||||
public static String load(final File source) throws IOException
|
||||
{
|
||||
String result;
|
||||
|
||||
result = load(source, DEFAULT_CHARSET_NAME);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param file
|
||||
* @throws IOException
|
||||
*/
|
||||
public static String load(final File source, final String charsetName) throws IOException
|
||||
{
|
||||
String result;
|
||||
|
||||
result = loadToStringBuffer(source, charsetName).toString();
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param file
|
||||
* @return
|
||||
* @throws IOException
|
||||
*/
|
||||
public static String load(final URL source) throws IOException
|
||||
{
|
||||
String result;
|
||||
|
||||
result = load(source, DEFAULT_CHARSET_NAME);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param file
|
||||
* @throws IOException
|
||||
*/
|
||||
public static String load(final URL source, final String charsetName) throws IOException
|
||||
{
|
||||
String result;
|
||||
|
||||
//
|
||||
StringBuffer buffer = new StringBuffer(source.openConnection().getContentLength() + 1);
|
||||
read(buffer, source.openStream(), charsetName);
|
||||
|
||||
//
|
||||
result = buffer.toString();
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param file
|
||||
* @throws IOException
|
||||
*/
|
||||
public static StringList loadStringList(final File source) throws IOException
|
||||
{
|
||||
StringList result;
|
||||
|
||||
result = loadStringList(source, DEFAULT_CHARSET_NAME);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param file
|
||||
* @throws IOException
|
||||
*/
|
||||
public static StringList loadStringList(final File file, final String charsetName) throws IOException
|
||||
{
|
||||
StringList result;
|
||||
|
||||
result = StringListUtils.load(file, charsetName);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param file
|
||||
* @throws IOException
|
||||
*/
|
||||
public static StringBuffer loadToStringBuffer(final File source) throws IOException
|
||||
{
|
||||
StringBuffer result;
|
||||
|
||||
result = loadToStringBuffer(source, DEFAULT_CHARSET_NAME);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param file
|
||||
* @throws IOException
|
||||
*/
|
||||
public static StringBuffer loadToStringBuffer(final File file, final String charsetName) throws IOException
|
||||
{
|
||||
StringBuffer result;
|
||||
|
||||
BufferedReader in = null;
|
||||
try
|
||||
{
|
||||
in = new BufferedReader(new InputStreamReader(new FileInputStream(file), charsetName));
|
||||
|
||||
boolean ended = false;
|
||||
final String LINE_SEPARATOR = System.getProperty("line.separator");
|
||||
result = new StringBuffer((int) file.length() + 1);
|
||||
while (!ended)
|
||||
{
|
||||
String line = in.readLine();
|
||||
|
||||
if (line == null)
|
||||
{
|
||||
ended = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
result.append(line).append(LINE_SEPARATOR);
|
||||
}
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
try
|
||||
{
|
||||
if (in != null)
|
||||
{
|
||||
in.close();
|
||||
}
|
||||
}
|
||||
catch (IOException exception)
|
||||
{
|
||||
exception.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param file
|
||||
* @throws IOException
|
||||
*/
|
||||
public static StringList loadToStringList(final File source) throws IOException
|
||||
{
|
||||
StringList result;
|
||||
|
||||
result = loadToStringList(source, DEFAULT_CHARSET_NAME);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param file
|
||||
* @throws IOException
|
||||
*/
|
||||
public static StringList loadToStringList(final File file, final String charsetName) throws IOException
|
||||
{
|
||||
StringList result;
|
||||
|
||||
BufferedReader in = null;
|
||||
try
|
||||
{
|
||||
in = new BufferedReader(new InputStreamReader(new FileInputStream(file), charsetName));
|
||||
|
||||
boolean ended = false;
|
||||
final String LINE_SEPARATOR = System.getProperty("line.separator");
|
||||
result = new StringList();
|
||||
while (!ended)
|
||||
{
|
||||
String line = in.readLine();
|
||||
|
||||
if (line == null)
|
||||
{
|
||||
ended = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
result.append(line).append(LINE_SEPARATOR);
|
||||
}
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
try
|
||||
{
|
||||
if (in != null)
|
||||
{
|
||||
in.close();
|
||||
}
|
||||
}
|
||||
catch (IOException exception)
|
||||
{
|
||||
exception.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param file
|
||||
* @return
|
||||
* @throws IOException
|
||||
*/
|
||||
public static StringList loadToStringList(final URL source) throws IOException
|
||||
{
|
||||
StringList result;
|
||||
|
||||
result = loadToStringList(source, DEFAULT_CHARSET_NAME);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param file
|
||||
* @throws IOException
|
||||
*/
|
||||
public static StringList loadToStringList(final URL source, final String charsetName) throws IOException
|
||||
{
|
||||
StringList result;
|
||||
|
||||
//
|
||||
result = StringListUtils.load(source, charsetName);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param file
|
||||
* @throws IOException
|
||||
*/
|
||||
public static void read(final StringBuffer out, final InputStream is, final String charsetName) throws IOException
|
||||
{
|
||||
BufferedReader in = null;
|
||||
try
|
||||
{
|
||||
in = new BufferedReader(new InputStreamReader(is, charsetName));
|
||||
|
||||
boolean ended = false;
|
||||
final String LINE_SEPARATOR = System.getProperty("line.separator");
|
||||
|
||||
while (!ended)
|
||||
{
|
||||
String line = in.readLine();
|
||||
|
||||
if (line == null)
|
||||
{
|
||||
ended = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
out.append(line).append(LINE_SEPARATOR);
|
||||
}
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
try
|
||||
{
|
||||
if (in != null)
|
||||
{
|
||||
in.close();
|
||||
}
|
||||
}
|
||||
catch (IOException exception)
|
||||
{
|
||||
exception.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param source
|
||||
* @param extension
|
||||
* @return
|
||||
*
|
||||
* @deprecated See
|
||||
* <code>org.apache.commons.io.FilenameUtils.removeExtension</code>
|
||||
*/
|
||||
@Deprecated
|
||||
public static String removeExtension(final String source)
|
||||
{
|
||||
String result;
|
||||
|
||||
if (source == null)
|
||||
{
|
||||
result = source;
|
||||
}
|
||||
else
|
||||
{
|
||||
int separatorIndex = source.lastIndexOf('.');
|
||||
|
||||
//
|
||||
if (separatorIndex > 0)
|
||||
{
|
||||
result = source.substring(0, separatorIndex);
|
||||
}
|
||||
else
|
||||
{
|
||||
result = source;
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param file
|
||||
* @throws FileNotFoundException
|
||||
* @throws UnsupportedEncodingException
|
||||
*/
|
||||
public static void save(final File file, final String source) throws UnsupportedEncodingException, FileNotFoundException
|
||||
{
|
||||
PrintWriter out = null;
|
||||
try
|
||||
{
|
||||
out = new PrintWriter(new OutputStreamWriter(new FileOutputStream(file), DEFAULT_CHARSET_NAME));
|
||||
|
||||
out.println(source);
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (out != null)
|
||||
{
|
||||
out.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param file
|
||||
* @throws FileNotFoundException
|
||||
* @throws UnsupportedEncodingException
|
||||
*/
|
||||
public static void save(final File file, final StringBuffer source) throws UnsupportedEncodingException, FileNotFoundException
|
||||
{
|
||||
save(file, source.toString());
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param file
|
||||
* @throws FileNotFoundException
|
||||
* @throws UnsupportedEncodingException
|
||||
*/
|
||||
public static void save(final File file, final StringList source) throws UnsupportedEncodingException, FileNotFoundException
|
||||
{
|
||||
StringListUtils.save(file, source);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param source
|
||||
* @param extension
|
||||
* @return
|
||||
*/
|
||||
public static File setExtension(final File source, final String extension)
|
||||
{
|
||||
File result;
|
||||
|
||||
if ((source == null) || (extension == null))
|
||||
{
|
||||
result = source;
|
||||
}
|
||||
else
|
||||
{
|
||||
result = new File(setExtension(source.getAbsolutePath(), extension));
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param source
|
||||
* @param extension
|
||||
* @return
|
||||
*/
|
||||
public static String setExtension(final String source, final String extension)
|
||||
{
|
||||
String result;
|
||||
|
||||
if ((source == null) || (extension == null))
|
||||
{
|
||||
result = source;
|
||||
}
|
||||
else
|
||||
{
|
||||
int separatorIndex = source.lastIndexOf('.');
|
||||
|
||||
//
|
||||
if (separatorIndex > 0)
|
||||
{
|
||||
String prefix = source.substring(0, separatorIndex);
|
||||
if (prefix.endsWith(extension))
|
||||
{
|
||||
result = prefix;
|
||||
}
|
||||
else
|
||||
{
|
||||
result = prefix + extension;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
result = source + extension;
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
}
|
188
src/fr/devinsy/util/Fraction.java
Normal file
188
src/fr/devinsy/util/Fraction.java
Normal file
|
@ -0,0 +1,188 @@
|
|||
/**
|
||||
* Copyright (C) 2009-2010, 2013-2014 Christian Pierre MOMON
|
||||
*
|
||||
* This file is part of Devinsy-utils.
|
||||
*
|
||||
* Devinsy-utils is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Devinsy-utils is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Devinsy-utils. If not, see <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
package fr.devinsy.util;
|
||||
|
||||
/**
|
||||
* Useful for display beautiful percentage value as string.
|
||||
*
|
||||
* @author cpm
|
||||
*/
|
||||
public class Fraction
|
||||
{
|
||||
protected long numerator;
|
||||
protected long denominator;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public Fraction(final long numerator, final long denominator)
|
||||
{
|
||||
this.numerator = numerator;
|
||||
this.denominator = denominator;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public long denominator()
|
||||
{
|
||||
long result;
|
||||
|
||||
result = this.denominator;
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public long numerator()
|
||||
{
|
||||
long result;
|
||||
|
||||
result = this.numerator;
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public long percentage() throws Exception
|
||||
{
|
||||
long result;
|
||||
|
||||
result = percentage(this.numerator, this.denominator);
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public String percentageFullString()
|
||||
{
|
||||
String result;
|
||||
|
||||
result = percentageFullString(this.numerator, this.denominator);
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public String percentageString()
|
||||
{
|
||||
String result;
|
||||
|
||||
result = percentageString(this.numerator, this.denominator);
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
String result;
|
||||
|
||||
result = this.numerator + "/" + this.denominator;
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public static long percentage(final long numerator, final long denominator) throws Exception
|
||||
{
|
||||
long result;
|
||||
|
||||
if (denominator == 0)
|
||||
{
|
||||
throw new Exception("denominator is zero");
|
||||
}
|
||||
else
|
||||
{
|
||||
result = Math.round(numerator * 100 / denominator);
|
||||
}
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public static String percentageFullString(final long numerator, final long denominator)
|
||||
{
|
||||
String result;
|
||||
|
||||
result = percentageString(numerator, denominator);
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public static String percentageString(final long numerator, final long denominator)
|
||||
{
|
||||
String result;
|
||||
|
||||
try
|
||||
{
|
||||
long value = percentage(numerator, denominator);
|
||||
|
||||
if (numerator == 0)
|
||||
{
|
||||
result = "0%";
|
||||
}
|
||||
else if (value == 0)
|
||||
{
|
||||
result = "~0%";
|
||||
}
|
||||
else if (value < 10)
|
||||
{
|
||||
result = "0" + value + "%";
|
||||
}
|
||||
else
|
||||
{
|
||||
result = value + "%";
|
||||
}
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
result = "--%";
|
||||
}
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
}
|
241
src/fr/devinsy/util/InternetProxyConfiguration.java
Executable file
241
src/fr/devinsy/util/InternetProxyConfiguration.java
Executable file
|
@ -0,0 +1,241 @@
|
|||
/**
|
||||
* Copyright (C) 2009-2010, 2013-2014 Christian Pierre MOMON
|
||||
*
|
||||
* This file is part of Devinsy-utils.
|
||||
*
|
||||
* Devinsy-utils is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Devinsy-utils is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Devinsy-utils. If not, see <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
package fr.devinsy.util;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public class InternetProxyConfiguration
|
||||
{
|
||||
//
|
||||
protected String host;
|
||||
protected int port;
|
||||
protected String login;
|
||||
protected String password;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public InternetProxyConfiguration()
|
||||
{
|
||||
this.host = "";
|
||||
this.port = 0;
|
||||
this.login = "";
|
||||
this.password = "";
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public InternetProxyConfiguration(final String host, final int port, final String login, final String password)
|
||||
{
|
||||
//
|
||||
if (host == null)
|
||||
{
|
||||
this.host = "";
|
||||
}
|
||||
else
|
||||
{
|
||||
this.host = host;
|
||||
}
|
||||
|
||||
//
|
||||
this.port = port;
|
||||
|
||||
//
|
||||
if (login == null)
|
||||
{
|
||||
this.login = "";
|
||||
}
|
||||
else
|
||||
{
|
||||
this.login = login;
|
||||
}
|
||||
|
||||
//
|
||||
if (password == null)
|
||||
{
|
||||
this.password = "";
|
||||
}
|
||||
else
|
||||
{
|
||||
this.password = password;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public InternetProxyConfiguration(final String host, final String port, final String login, final String password) throws Exception
|
||||
{
|
||||
//
|
||||
if (host == null)
|
||||
{
|
||||
this.host = "";
|
||||
}
|
||||
else
|
||||
{
|
||||
this.host = host;
|
||||
}
|
||||
|
||||
//
|
||||
if ((port == null) || (port.trim().length() == 0))
|
||||
{
|
||||
this.port = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
try
|
||||
{
|
||||
this.port = Integer.parseInt(port);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
String errorMessage = "Incorrect PROXY port value.";
|
||||
throw new Exception(errorMessage, exception);
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
if (login == null)
|
||||
{
|
||||
this.login = "";
|
||||
}
|
||||
else
|
||||
{
|
||||
this.login = login;
|
||||
}
|
||||
|
||||
//
|
||||
if (password == null)
|
||||
{
|
||||
this.password = "";
|
||||
}
|
||||
else
|
||||
{
|
||||
this.password = password;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public String host()
|
||||
{
|
||||
String result;
|
||||
|
||||
result = this.host;
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public boolean isInitialized()
|
||||
{
|
||||
boolean result;
|
||||
|
||||
if ((this.host.length() > 0) && (this.port > 0))
|
||||
{
|
||||
result = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
result = false;
|
||||
}
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public String login()
|
||||
{
|
||||
String result;
|
||||
|
||||
result = this.login;
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public String password()
|
||||
{
|
||||
String result;
|
||||
|
||||
result = this.password;
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public int port()
|
||||
{
|
||||
int result;
|
||||
|
||||
result = this.port;
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
String result;
|
||||
|
||||
String login;
|
||||
if (this.login.length() == 0)
|
||||
{
|
||||
login = "";
|
||||
}
|
||||
else
|
||||
{
|
||||
login = "********";
|
||||
}
|
||||
|
||||
String password;
|
||||
if (this.password.length() == 0)
|
||||
{
|
||||
password = "";
|
||||
}
|
||||
else
|
||||
{
|
||||
password = "********";
|
||||
}
|
||||
|
||||
result = "(" + this.host + "," + this.port + "," + login + "," + password + ")";
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
}
|
133
src/fr/devinsy/util/SimpleAveragemeter.java
Executable file
133
src/fr/devinsy/util/SimpleAveragemeter.java
Executable file
|
@ -0,0 +1,133 @@
|
|||
/**
|
||||
* Copyright (C) 2009-2010, 2013-2014 Christian Pierre MOMON
|
||||
*
|
||||
* This file is part of Devinsy-utils.
|
||||
*
|
||||
* Devinsy-utils is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Devinsy-utils is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Devinsy-utils. If not, see <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
package fr.devinsy.util;
|
||||
|
||||
/**
|
||||
* This class defines a simple average manager. For example, it is useful for
|
||||
* millisecond. The maximum value available in input is one day in millisecond.
|
||||
*/
|
||||
public class SimpleAveragemeter
|
||||
{
|
||||
//
|
||||
protected long sum;
|
||||
protected long cardinal;
|
||||
protected long MAX_ADD = 1 * 24 * 60 * 60 * 1000; // One day in millisecond.
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public SimpleAveragemeter()
|
||||
{
|
||||
this.reset();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
synchronized public void add(final long value)
|
||||
{
|
||||
// Manage the sum limit.
|
||||
if ((this.sum > Long.MAX_VALUE / 2) && (this.cardinal % 2 == 0))
|
||||
{
|
||||
this.sum = this.sum / 2;
|
||||
this.cardinal = this.cardinal / 2;
|
||||
}
|
||||
|
||||
// Add the new value.
|
||||
if (this.sum > this.MAX_ADD)
|
||||
{
|
||||
this.sum += this.MAX_ADD;
|
||||
this.cardinal += 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
this.sum += value;
|
||||
this.cardinal += 1;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
synchronized public long average()
|
||||
{
|
||||
long result;
|
||||
|
||||
if (this.cardinal == 0)
|
||||
{
|
||||
result = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
result = this.sum / this.cardinal;
|
||||
}
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public long cardinal()
|
||||
{
|
||||
long result;
|
||||
|
||||
result = this.cardinal;
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
synchronized public void reset()
|
||||
{
|
||||
this.sum = 0;
|
||||
this.cardinal = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
String result;
|
||||
|
||||
result = Long.toString(this.average());
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public long value()
|
||||
{
|
||||
long result;
|
||||
|
||||
result = this.average();
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
}
|
164
src/fr/devinsy/util/SimpleChronometer.java
Executable file
164
src/fr/devinsy/util/SimpleChronometer.java
Executable file
|
@ -0,0 +1,164 @@
|
|||
/**
|
||||
* Copyright (C) 2008-2010, 2013-2014 Christian Pierre MOMON
|
||||
*
|
||||
* This file is part of Devinsy-utils.
|
||||
*
|
||||
* Devinsy-utils is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Devinsy-utils is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Devinsy-utils. If not, see <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
package fr.devinsy.util;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public class SimpleChronometer
|
||||
{
|
||||
//
|
||||
protected long firstTime;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public SimpleChronometer()
|
||||
{
|
||||
this.reset();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public long interval()
|
||||
{
|
||||
long result;
|
||||
|
||||
result = new Date().getTime() - this.firstTime;
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public void reset()
|
||||
{
|
||||
this.firstTime = new Date().getTime();
|
||||
}
|
||||
|
||||
/**
|
||||
* TO BE COMPLETED.
|
||||
*/
|
||||
public static String humanString(final long interval)
|
||||
{
|
||||
String result;
|
||||
|
||||
if (interval < 1000)
|
||||
{
|
||||
result = interval + "ms";
|
||||
}
|
||||
else if (interval < 60 * 1000)
|
||||
{
|
||||
result = interval / 1000 + "," + interval % 1000 + "s";
|
||||
}
|
||||
else if (interval < 60 * 60 * 1000)
|
||||
{
|
||||
result = interval / 1000 + "," + interval % 1000 + "s";
|
||||
}
|
||||
else if (interval < 24 * 60 * 60 * 1000)
|
||||
{
|
||||
result = interval / 1000 + "," + interval % 1000 + "s";
|
||||
}
|
||||
else if (interval < 7 * 24 * 60 * 60 * 1000)
|
||||
{
|
||||
result = interval / 1000 + "," + interval % 1000 + "s";
|
||||
}
|
||||
else
|
||||
// if (interval < 7*24*60*60*1000)
|
||||
{
|
||||
result = interval / 1000 + "," + interval % 1000 + "s";
|
||||
}
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public static String shortHumanString(final long interval)
|
||||
{
|
||||
String result;
|
||||
|
||||
if (interval < 1000)
|
||||
{
|
||||
result = interval + " ms";
|
||||
}
|
||||
else if (interval < 2 * 1000)
|
||||
{
|
||||
result = interval / 1000 + " seconde";
|
||||
}
|
||||
else if (interval < 60 * 1000)
|
||||
{
|
||||
result = interval / 1000 + " secondes";
|
||||
}
|
||||
else if (interval < 2 * 60 * 1000L)
|
||||
{
|
||||
result = interval / (60 * 1000L) + " minute";
|
||||
}
|
||||
else if (interval < 60 * 60 * 1000L)
|
||||
{
|
||||
result = interval / (60 * 1000L) + " minutes";
|
||||
}
|
||||
else if (interval < 2 * 60 * 60 * 1000L)
|
||||
{
|
||||
result = interval / (60 * 60 * 1000L) + " heure";
|
||||
}
|
||||
else if (interval < 24 * 60 * 60 * 1000L)
|
||||
{
|
||||
result = interval / (60 * 60 * 1000L) + " heures";
|
||||
}
|
||||
else if (interval < 2 * 24 * 60 * 60 * 1000L)
|
||||
{
|
||||
result = interval / (24 * 60 * 60 * 1000L) + " jour";
|
||||
}
|
||||
else if (interval < 7 * 24 * 60 * 60 * 1000L)
|
||||
{
|
||||
result = interval / (24 * 60 * 60 * 1000L) + " jours";
|
||||
}
|
||||
else if (interval < 2 * 7 * 24 * 60 * 60 * 1000L)
|
||||
{
|
||||
result = interval / (7 * 24 * 60 * 60 * 1000L) + " semaine";
|
||||
}
|
||||
else if (interval < 30 * 24 * 60 * 60 * 1000L)
|
||||
{
|
||||
result = interval / (7 * 24 * 60 * 60 * 1000L) + " semaines";
|
||||
}
|
||||
else if (interval < 52 * 7 * 24 * 60 * 60 * 1000L)
|
||||
{
|
||||
result = interval / (30 * 24 * 60 * 60 * 1000L) + " mois";
|
||||
}
|
||||
else if (interval < 2 * 52 * 7 * 24 * 60 * 60 * 1000L)
|
||||
{
|
||||
result = interval / (52 * 7 * 24 * 60 * 60 * 1000L) + " année";
|
||||
}
|
||||
else
|
||||
{
|
||||
result = interval / (52 * 7 * 24 * 60 * 60 * 1000L) + " années";
|
||||
}
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
}
|
47
src/fr/devinsy/util/StacktraceWriter.java
Normal file
47
src/fr/devinsy/util/StacktraceWriter.java
Normal file
|
@ -0,0 +1,47 @@
|
|||
/**
|
||||
* Copyright (C) 2010, 2013-2016 Christian Pierre MOMON
|
||||
*
|
||||
* This file is part of Devinsy-utils.
|
||||
*
|
||||
* Devinsy-utils is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Devinsy-utils is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Devinsy-utils. If not, see <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
package fr.devinsy.util;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.PrintStream;
|
||||
|
||||
/**
|
||||
* @deprecated use <code>SLF4J.Logger.error("blabla", exception)</code> method
|
||||
* or the ExceptionUtils.getStackTrace(throwable).
|
||||
*/
|
||||
@Deprecated
|
||||
public class StacktraceWriter
|
||||
{
|
||||
/**
|
||||
* @deprecated use <code>SLF4J.Logger.error("blabla", exception)</code>
|
||||
* method or the ExceptionUtils.getStackTrace(throwable).
|
||||
*/
|
||||
@Deprecated
|
||||
public static String toString(final Exception exception)
|
||||
{
|
||||
String result;
|
||||
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream(50000);
|
||||
exception.printStackTrace(new PrintStream(out));
|
||||
result = out.toString();
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
}
|
319
src/fr/devinsy/util/ToolBox.java
Normal file
319
src/fr/devinsy/util/ToolBox.java
Normal file
|
@ -0,0 +1,319 @@
|
|||
/**
|
||||
* Copyright (C) 2008-2010, 2013-2014 Christian Pierre MOMON
|
||||
*
|
||||
* This file is part of Devinsy-utils.
|
||||
*
|
||||
* Devinsy-utils is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Devinsy-utils is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Devinsy-utils. If not, see <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
package fr.devinsy.util;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import fr.devinsy.util.strings.StringList;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author christian.momon@devinsy.fr
|
||||
*/
|
||||
public class ToolBox
|
||||
{
|
||||
|
||||
public static String clean(final String source)
|
||||
{
|
||||
String result;
|
||||
|
||||
result = source.replaceAll("[^\\w ]", " ");
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns information about the calling class of a calledClass.
|
||||
*
|
||||
* @param calledClassName
|
||||
* the class name which is the subject of the search.
|
||||
*
|
||||
* @return information about the calling class.
|
||||
*/
|
||||
public static StackTraceElement getCaller(final String calledClassName)
|
||||
{
|
||||
StackTraceElement result;
|
||||
|
||||
//
|
||||
StackTraceElement[] stack = Thread.currentThread().getStackTrace();
|
||||
// System.out.println("////////////////////////////");
|
||||
// for (int i = 0; (i < stack.length) && (i < 4); i++) {
|
||||
// System.out.println(i + " " + stack[i].getClassName());
|
||||
// }
|
||||
|
||||
// Search for first entry of class called.
|
||||
boolean ended = false;
|
||||
Integer indexOfCalled = null;
|
||||
int depth = 1;
|
||||
while (!ended)
|
||||
{
|
||||
if (depth < stack.length)
|
||||
{
|
||||
String currentClassName = stack[depth].getClassName();
|
||||
if (currentClassName.equals(calledClassName))
|
||||
{
|
||||
ended = true;
|
||||
indexOfCalled = Integer.valueOf(depth);
|
||||
}
|
||||
else
|
||||
{
|
||||
depth += 1;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ended = true;
|
||||
result = null;
|
||||
}
|
||||
}
|
||||
|
||||
// Search for caller of class called.
|
||||
if (indexOfCalled == null)
|
||||
{
|
||||
result = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
result = null;
|
||||
ended = false;
|
||||
depth = indexOfCalled;
|
||||
while (!ended)
|
||||
{
|
||||
if (depth < stack.length)
|
||||
{
|
||||
String currentClassName = stack[depth].getClassName();
|
||||
if (currentClassName.equals(calledClassName))
|
||||
{
|
||||
depth += 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
ended = true;
|
||||
result = stack[depth];
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ended = true;
|
||||
result = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param pattern
|
||||
* @param source
|
||||
* @return
|
||||
*/
|
||||
public static int indexOf(final String pattern, final List<String> source)
|
||||
{
|
||||
int result;
|
||||
|
||||
if (source == null)
|
||||
{
|
||||
result = -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
boolean ended = false;
|
||||
result = -1;
|
||||
int currentIndex = 0;
|
||||
while (!ended)
|
||||
{
|
||||
if (currentIndex < source.size())
|
||||
{
|
||||
String sourceString = source.get(currentIndex);
|
||||
if (StringUtils.equals(sourceString, pattern))
|
||||
{
|
||||
ended = true;
|
||||
result = currentIndex;
|
||||
}
|
||||
else
|
||||
{
|
||||
currentIndex += 1;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ended = true;
|
||||
currentIndex = -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param string
|
||||
* @param targets
|
||||
* @return
|
||||
*/
|
||||
public static boolean matchesAny(final String string, final String... targets)
|
||||
{
|
||||
boolean result;
|
||||
|
||||
if ((string == null) || (targets == null))
|
||||
{
|
||||
result = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
//
|
||||
boolean ended = false;
|
||||
int index = 0;
|
||||
result = false;
|
||||
while (!ended)
|
||||
{
|
||||
if (index < targets.length)
|
||||
{
|
||||
if (StringUtils.equals(string, targets[index]))
|
||||
{
|
||||
ended = true;
|
||||
result = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
index += 1;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ended = true;
|
||||
result = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static Double[] sort(final Set<Double> source)
|
||||
{
|
||||
Double[] result;
|
||||
|
||||
if (source == null)
|
||||
{
|
||||
result = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
result = new Double[source.size()];
|
||||
|
||||
source.toArray(result);
|
||||
Arrays.sort(result);
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Concatenates int values from an array, adding decoration strings.
|
||||
*
|
||||
* @param values
|
||||
* Source of int values.
|
||||
* @param prefix
|
||||
* Decoration to put on start.
|
||||
* @param separator
|
||||
* Decoration to put between values.
|
||||
* @param postfix
|
||||
* Decoration to put on end.
|
||||
*
|
||||
* @return A decorated string representing the int values.
|
||||
*/
|
||||
public static String toString(final int[] values, final String prefix, final String separator, final String postfix)
|
||||
{
|
||||
String result;
|
||||
|
||||
StringList buffer = new StringList();
|
||||
|
||||
//
|
||||
if (prefix != null)
|
||||
{
|
||||
buffer.append(prefix);
|
||||
}
|
||||
|
||||
//
|
||||
boolean firstPassed = false;
|
||||
for (int value : values)
|
||||
{
|
||||
if (firstPassed)
|
||||
{
|
||||
buffer.append(separator);
|
||||
}
|
||||
else
|
||||
{
|
||||
firstPassed = true;
|
||||
}
|
||||
buffer.append(value);
|
||||
}
|
||||
|
||||
//
|
||||
if (postfix != null)
|
||||
{
|
||||
buffer.append(postfix);
|
||||
}
|
||||
|
||||
//
|
||||
result = buffer.toString();
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param source
|
||||
* @return
|
||||
*/
|
||||
public static String toString(final String source)
|
||||
{
|
||||
String result;
|
||||
|
||||
if (source == null)
|
||||
{
|
||||
result = "";
|
||||
}
|
||||
else
|
||||
{
|
||||
result = source;
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
}
|
500
src/fr/devinsy/util/cmdexec/CmdExec.java
Normal file
500
src/fr/devinsy/util/cmdexec/CmdExec.java
Normal file
|
@ -0,0 +1,500 @@
|
|||
/**
|
||||
* Copyright (C) 2005-2010, 2013, 2015-2016 Christian Pierre MOMON
|
||||
*
|
||||
* This file is part of Devinsy-utils.
|
||||
*
|
||||
* Devinsy-utils is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Devinsy-utils is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Devinsy-utils. If not, see <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
package fr.devinsy.util.cmdexec;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import fr.devinsy.util.cmdexec.StreamGobbler.StreamWay;
|
||||
import fr.devinsy.util.strings.StringListUtils;
|
||||
|
||||
/**
|
||||
* We must use the isOver method on Gobblers because with short tasks the
|
||||
* waitFor ends before the Gobbler read.
|
||||
*
|
||||
* @author Christian Pierre MOMON (christian.momon@devinsy.fr)
|
||||
*/
|
||||
public class CmdExec
|
||||
{
|
||||
private static Logger logger = LoggerFactory.getLogger(CmdExec.class);
|
||||
|
||||
private int exitValue;
|
||||
private String out;
|
||||
private String err;
|
||||
|
||||
// ////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// ////////////////////////////////////////////////////////////////////
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public CmdExec(final String command)
|
||||
{
|
||||
run(command, StreamGobbler.StreamWay.BUFFER, StreamGobbler.StreamWay.BUFFER);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public CmdExec(final String... command)
|
||||
{
|
||||
run(command, StreamGobbler.StreamWay.BUFFER, StreamGobbler.StreamWay.BUFFER);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public CmdExec(final String command, final StreamGobbler outputGobbler, final StreamGobbler errorGobbler)
|
||||
{
|
||||
run(command, outputGobbler, errorGobbler);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public CmdExec(final String command, final StreamGobbler.StreamWay stdout, final StreamGobbler.StreamWay stderr)
|
||||
{
|
||||
run(command, stdout, stderr);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public CmdExec(final String[] command, final StreamGobbler outputGobbler, final StreamGobbler errorGobbler)
|
||||
{
|
||||
run(command, outputGobbler, errorGobbler);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public CmdExec(final String[] command, final StreamGobbler.StreamWay stdout, final StreamGobbler.StreamWay stderr)
|
||||
{
|
||||
run(command, stdout, stderr);
|
||||
}
|
||||
|
||||
// ////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// ////////////////////////////////////////////////////////////////////
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String getErrStream()
|
||||
{
|
||||
String result;
|
||||
|
||||
result = this.err;
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public int getExitValue()
|
||||
{
|
||||
int result;
|
||||
|
||||
result = this.exitValue;
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String getOutStream()
|
||||
{
|
||||
String result;
|
||||
|
||||
result = this.out;
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param command
|
||||
* : not a shell command, it must be a executable program.
|
||||
* @param outputGobbler
|
||||
* @param errorGobbler
|
||||
* @return
|
||||
*/
|
||||
public int run(final String command, final StreamGobbler outputGobbler, final StreamGobbler errorGobbler)
|
||||
{
|
||||
int result;
|
||||
|
||||
logger.info("CmdExec(command) = [" + command + "]");
|
||||
|
||||
String[] commands = command.split("[ \t\n\r\f]");
|
||||
|
||||
result = run(commands, outputGobbler, errorGobbler);
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public int run(final String command, final StreamWay stdout, final StreamWay stderr)
|
||||
{
|
||||
int result;
|
||||
|
||||
result = run(command, new StreamGobbler("OUTPUT", stdout), new StreamGobbler("ERROR", stderr));
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Note: this code is inspired by an article of Michael C. Daconta published
|
||||
* in JavaWorld Dec 29, 2000 (http://www.javaworld.com/article/2071275
|
||||
* /core-java/when-runtime-exec---won-t.html?page=2).
|
||||
*
|
||||
* @param command
|
||||
* not a shell command, it must be a executable program.
|
||||
* @param outputGobbler
|
||||
* @param errorGobbler
|
||||
* @return
|
||||
*/
|
||||
public int run(final String[] command, final StreamGobbler outputGobbler, final StreamGobbler errorGobbler)
|
||||
{
|
||||
int result;
|
||||
|
||||
logger.info("CmdExec(command[]) = [" + StringListUtils.toStringSeparatedBy(command, " ") + "]");
|
||||
logger.info("CmdExec(command[]) = [" + StringListUtils.toStringWithBrackets(command) + "]");
|
||||
|
||||
try
|
||||
{
|
||||
Runtime rt = Runtime.getRuntime();
|
||||
|
||||
Process process = rt.exec(command);
|
||||
|
||||
// Set a collector for error message.
|
||||
errorGobbler.setInputStream(process.getErrorStream());
|
||||
|
||||
// Set a collector for output message.
|
||||
outputGobbler.setInputStream(process.getInputStream());
|
||||
|
||||
// Collect messages.
|
||||
errorGobbler.start();
|
||||
outputGobbler.start();
|
||||
|
||||
// Wait and manage the exit value.
|
||||
this.exitValue = process.waitFor();
|
||||
logger.info("ExitValue: {}", this.exitValue);
|
||||
|
||||
// Sometimes, process ends before Gobblers read its outpout, so we
|
||||
// must wait them.
|
||||
while ((!outputGobbler.isOver()) || (!errorGobbler.isOver()))
|
||||
{
|
||||
Thread.sleep(1);
|
||||
}
|
||||
|
||||
// Store messages.
|
||||
this.out = outputGobbler.getStream();
|
||||
this.err = errorGobbler.getStream();
|
||||
|
||||
result = this.exitValue;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
this.err = exception.getMessage();
|
||||
this.exitValue = -77;
|
||||
result = this.exitValue;
|
||||
logger.error(exception.getMessage(), exception);
|
||||
}
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param command
|
||||
* @param stdout
|
||||
* @param stderr
|
||||
* @return
|
||||
*/
|
||||
public int run(final String[] command, final StreamGobbler.StreamWay stdout, final StreamGobbler.StreamWay stderr)
|
||||
{
|
||||
int result;
|
||||
|
||||
result = run(command, new StreamGobbler("OUTPUT", stdout), new StreamGobbler("ERROR", stderr));
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param commands
|
||||
* @return
|
||||
* @throws CmdExecException
|
||||
*/
|
||||
public static String multirun(final String... commands) throws CmdExecException
|
||||
{
|
||||
String result;
|
||||
|
||||
ArrayList<String> stdouts = new ArrayList<String>(commands.length);
|
||||
|
||||
boolean ended = false;
|
||||
int commandCounter = 0;
|
||||
while (!ended)
|
||||
{
|
||||
if (commandCounter < commands.length)
|
||||
{
|
||||
String command = commands[commandCounter];
|
||||
|
||||
if ((command == null) || (command.length() == 0))
|
||||
{
|
||||
result = null;
|
||||
commandCounter += 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
stdouts.add(CmdExec.run(command));
|
||||
commandCounter += 1;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ended = true;
|
||||
result = null;
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
int resultLength = 0;
|
||||
for (String stdout : stdouts)
|
||||
{
|
||||
resultLength += stdout.length();
|
||||
}
|
||||
StringBuffer buffer = new StringBuffer(resultLength);
|
||||
for (String stdout : stdouts)
|
||||
{
|
||||
buffer.append(stdout);
|
||||
}
|
||||
result = buffer.toString();
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
// ////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// ////////////////////////////////////////////////////////////////////
|
||||
/**
|
||||
* @throws CmdExecException
|
||||
*/
|
||||
public static String run(final String command) throws CmdExecException
|
||||
{
|
||||
String result;
|
||||
|
||||
result = CmdExec.run(command.split("[ \t\n\r\f]"));
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws CmdExecException
|
||||
* @throws Exception
|
||||
*/
|
||||
public static String run(final String... command) throws CmdExecException
|
||||
{
|
||||
String result;
|
||||
|
||||
if ((command == null) || (command.length == 0))
|
||||
{
|
||||
throw new IllegalArgumentException("Empty command");
|
||||
}
|
||||
else
|
||||
{
|
||||
CmdExec cmd = new CmdExec(command, StreamGobbler.StreamWay.BUFFER, StreamGobbler.StreamWay.BUFFER);
|
||||
|
||||
if (cmd.getExitValue() == 0)
|
||||
{
|
||||
result = cmd.getOutStream();
|
||||
}
|
||||
else
|
||||
{
|
||||
logger.error("Command=\"" + StringListUtils.toStringWithBrackets(command));
|
||||
logger.error("Command=\"[" + StringListUtils.toString(command) + "]\n out => [" + cmd.getOutStream() + "]\n " + "err => (" + cmd.getErrStream().length() + ")[" + cmd.getErrStream()
|
||||
+ "]");
|
||||
throw new CmdExecException(cmd.getErrStream());
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Examples:
|
||||
*
|
||||
* run("sudo", "setfacl", "-m", "g:cpm:rwX", "/tmp/toto");
|
||||
*
|
||||
* run("sudo", "setfacl", "-R", "-m", "g:cpm:rwX", "/tmp/toto");
|
||||
*
|
||||
* @throws CmdExecException
|
||||
*/
|
||||
public static String run(final String program1, final String program2, final String[] args, final int min, final int max) throws CmdExecException
|
||||
{
|
||||
String result;
|
||||
|
||||
//
|
||||
boolean nullArg = false;
|
||||
boolean ended = false;
|
||||
int argumentIndex = 0;
|
||||
while (!ended)
|
||||
{
|
||||
if (argumentIndex >= args.length)
|
||||
{
|
||||
ended = true;
|
||||
nullArg = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (args[argumentIndex] == null)
|
||||
{
|
||||
ended = true;
|
||||
nullArg = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
argumentIndex += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
if (program1 == null)
|
||||
{
|
||||
throw new IllegalArgumentException("Null program parameter 1 detected: [" + program1 + "].");
|
||||
}
|
||||
else if (program2 == null)
|
||||
{
|
||||
throw new IllegalArgumentException("Null program parameter 2 detected: [" + program2 + "].");
|
||||
}
|
||||
else if (nullArg)
|
||||
{
|
||||
throw new IllegalArgumentException("Null parameter detected in position " + argumentIndex + " for " + StringListUtils.toStringWithBrackets(args) + ".");
|
||||
}
|
||||
else if ((args.length < min) || (args.length > max))
|
||||
{
|
||||
throw new IllegalArgumentException("Bad number of parameters: " + args.length + " for " + StringListUtils.toStringWithBrackets(args) + ".");
|
||||
}
|
||||
else
|
||||
{
|
||||
//
|
||||
String[] command = new String[args.length + 2];
|
||||
command[0] = program1;
|
||||
command[1] = program2;
|
||||
for (argumentIndex = 0; argumentIndex < args.length; argumentIndex++)
|
||||
{
|
||||
command[argumentIndex + 2] = args[argumentIndex];
|
||||
}
|
||||
|
||||
result = CmdExec.run(command);
|
||||
}
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Examples:
|
||||
*
|
||||
* run("setfacl", "-m", "g:cpm:rwX", "/tmp/toto");
|
||||
*
|
||||
* run("setfacl", "-R", "-m", "g:cpm:rwX", "/tmp/toto");
|
||||
*
|
||||
* @throws CmdExecException
|
||||
*/
|
||||
public static String run(final String program, final String[] args, final int min, final int max) throws CmdExecException
|
||||
{
|
||||
String result;
|
||||
|
||||
//
|
||||
boolean nullArg = false;
|
||||
boolean ended = false;
|
||||
int argumentCounter = 0;
|
||||
while (!ended)
|
||||
{
|
||||
if (argumentCounter >= args.length)
|
||||
{
|
||||
ended = true;
|
||||
nullArg = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (args[argumentCounter] == null)
|
||||
{
|
||||
ended = true;
|
||||
nullArg = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
argumentCounter += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
if (program == null)
|
||||
{
|
||||
throw new IllegalArgumentException("Null program parameter detected: [" + program + "].");
|
||||
}
|
||||
else if (nullArg)
|
||||
{
|
||||
throw new IllegalArgumentException("Null parameter detected in position " + argumentCounter + " for " + StringListUtils.toStringWithBrackets(args) + ".");
|
||||
}
|
||||
else if ((args.length < min) || (args.length > max))
|
||||
{
|
||||
throw new IllegalArgumentException("Bad number of parameters: " + args.length + " for " + StringListUtils.toStringWithBrackets(args) + ".");
|
||||
}
|
||||
else
|
||||
{
|
||||
//
|
||||
String[] command = new String[args.length + 1];
|
||||
command[0] = program;
|
||||
for (argumentCounter = 0; argumentCounter < args.length; argumentCounter++)
|
||||
{
|
||||
command[argumentCounter + 1] = args[argumentCounter];
|
||||
}
|
||||
|
||||
result = CmdExec.run(command);
|
||||
}
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
}
|
65
src/fr/devinsy/util/cmdexec/CmdExecException.java
Normal file
65
src/fr/devinsy/util/cmdexec/CmdExecException.java
Normal file
|
@ -0,0 +1,65 @@
|
|||
/**
|
||||
* Copyright (C) 2016 Christian Pierre MOMON
|
||||
*
|
||||
* This file is part of Devinsy-utils.
|
||||
*
|
||||
* Devinsy-utils is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Devinsy-utils is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Devinsy-utils. If not, see <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
package fr.devinsy.util.cmdexec;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Christian Pierre MOMON (christian.momon@devinsy.fr)
|
||||
*
|
||||
*/
|
||||
public class CmdExecException extends Exception
|
||||
{
|
||||
private static final long serialVersionUID = 3264886426311529668L;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public CmdExecException()
|
||||
{
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param message
|
||||
*/
|
||||
public CmdExecException(final String message)
|
||||
{
|
||||
super(message);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param message
|
||||
* @param cause
|
||||
*/
|
||||
public CmdExecException(final String message, final Throwable cause)
|
||||
{
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param cause
|
||||
*/
|
||||
public CmdExecException(final Throwable cause)
|
||||
{
|
||||
super(cause);
|
||||
}
|
||||
}
|
196
src/fr/devinsy/util/cmdexec/StreamGobbler.java
Normal file
196
src/fr/devinsy/util/cmdexec/StreamGobbler.java
Normal file
|
@ -0,0 +1,196 @@
|
|||
/**
|
||||
* Copyright (C) 2005-2008, 2010, 2013, 2016 Christian Pierre MOMON
|
||||
*
|
||||
* This file is part of Devinsy-utils.
|
||||
*
|
||||
* Devinsy-utils is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Devinsy-utils is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Devinsy-utils. If not, see <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
package fr.devinsy.util.cmdexec;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Christian Pierre MOMON (christian.momon@devinsy.fr)
|
||||
*/
|
||||
public class StreamGobbler extends Thread
|
||||
{
|
||||
public enum StreamWay
|
||||
{
|
||||
NONE,
|
||||
PRINT,
|
||||
BUFFER
|
||||
}
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(CmdExec.class);
|
||||
|
||||
private InputStream is;
|
||||
private String name;
|
||||
private StreamWay streamWay;
|
||||
private StringBuffer stream;
|
||||
|
||||
// Important if the caller wants have complete stream in case of very short
|
||||
// command.
|
||||
private boolean isOverStatus;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
StreamGobbler()
|
||||
{
|
||||
this.is = null;
|
||||
this.name = "";
|
||||
this.streamWay = StreamWay.NONE;
|
||||
this.stream = new StringBuffer();
|
||||
this.isOverStatus = false;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param is
|
||||
* @param name
|
||||
*/
|
||||
StreamGobbler(final InputStream is, final String name)
|
||||
{
|
||||
this.is = is;
|
||||
this.name = name;
|
||||
this.streamWay = StreamWay.NONE;
|
||||
this.stream = new StringBuffer();
|
||||
this.isOverStatus = false;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param is
|
||||
* @param name
|
||||
* @param streamWay
|
||||
*/
|
||||
StreamGobbler(final InputStream is, final String name, final StreamWay streamWay)
|
||||
{
|
||||
this.is = is;
|
||||
this.name = name;
|
||||
this.streamWay = streamWay;
|
||||
this.stream = new StringBuffer();
|
||||
this.isOverStatus = false;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param type
|
||||
* @param streamWay
|
||||
*/
|
||||
StreamGobbler(final String type, final StreamWay streamWay)
|
||||
{
|
||||
this.name = type;
|
||||
this.streamWay = streamWay;
|
||||
this.stream = new StringBuffer();
|
||||
this.isOverStatus = false;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String getStream()
|
||||
{
|
||||
String result;
|
||||
|
||||
if (this.stream != null)
|
||||
{
|
||||
result = this.stream.toString();
|
||||
}
|
||||
else
|
||||
{
|
||||
result = null;
|
||||
}
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public boolean isOver()
|
||||
{
|
||||
boolean result;
|
||||
|
||||
result = this.isOverStatus;
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
try
|
||||
{
|
||||
InputStreamReader isr = new InputStreamReader(this.is);
|
||||
BufferedReader buffer = new BufferedReader(isr);
|
||||
String line = null;
|
||||
switch (this.streamWay)
|
||||
{
|
||||
case NONE:
|
||||
while ((line = buffer.readLine()) != null)
|
||||
{
|
||||
;
|
||||
}
|
||||
break;
|
||||
|
||||
case PRINT:
|
||||
while ((line = buffer.readLine()) != null)
|
||||
{
|
||||
System.out.println(this.name + ">" + line);
|
||||
}
|
||||
break;
|
||||
|
||||
case BUFFER:
|
||||
while ((line = buffer.readLine()) != null)
|
||||
{
|
||||
this.stream.append(line + "\n");
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
logger.warn("unknow way for stream");
|
||||
}
|
||||
}
|
||||
catch (IOException exception)
|
||||
{
|
||||
logger.error(exception.getMessage(), exception);
|
||||
}
|
||||
|
||||
this.isOverStatus = true;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param is
|
||||
*/
|
||||
public void setInputStream(final InputStream is)
|
||||
{
|
||||
this.is = is;
|
||||
}
|
||||
}
|
214
src/fr/devinsy/util/rss/RSSCache.java
Normal file
214
src/fr/devinsy/util/rss/RSSCache.java
Normal file
|
@ -0,0 +1,214 @@
|
|||
/**
|
||||
* Copyright (C) 2013-2014 Christian Pierre MOMON
|
||||
*
|
||||
* This file is part of Devinsy-utils.
|
||||
*
|
||||
* Devinsy-utils is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Devinsy-utils is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Devinsy-utils. If not, see <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
package fr.devinsy.util.rss;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Locale;
|
||||
|
||||
import fr.devinsy.util.strings.StringList;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Christian Pierre MOMON (christian.momon@devinsy.fr)
|
||||
*/
|
||||
public class RSSCache
|
||||
{
|
||||
private static RSSCache instance = new RSSCache();
|
||||
private HashMap<String, String> cache;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private RSSCache()
|
||||
{
|
||||
this.cache = new HashMap<String, String>();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param key
|
||||
* @param locale
|
||||
* @return
|
||||
*/
|
||||
public String get(final String name)
|
||||
{
|
||||
return get(name, Locale.ROOT);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param name
|
||||
* @param locale
|
||||
* @return
|
||||
*/
|
||||
public String get(final String name, final Locale locale)
|
||||
{
|
||||
String result;
|
||||
|
||||
if (name == null)
|
||||
{
|
||||
throw new NullPointerException("name is null.");
|
||||
}
|
||||
else if (locale == null)
|
||||
{
|
||||
result = get(name, Locale.ROOT);
|
||||
}
|
||||
else
|
||||
{
|
||||
result = this.cache.get(key(name, locale));
|
||||
|
||||
if (result == null)
|
||||
{
|
||||
result = this.cache.get(name);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param name
|
||||
* @param locale
|
||||
* @return
|
||||
*/
|
||||
private String key(final String name, final Locale locale)
|
||||
{
|
||||
String result;
|
||||
|
||||
if (locale.getLanguage().length() == 0)
|
||||
{
|
||||
result = name;
|
||||
}
|
||||
else
|
||||
{
|
||||
result = name + "_" + locale.getLanguage();
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param key
|
||||
* @param locale
|
||||
* @param rss
|
||||
*/
|
||||
public void put(final String name, final Locale locale, final String rss)
|
||||
{
|
||||
if (name == null)
|
||||
{
|
||||
throw new NullPointerException("name is null.");
|
||||
}
|
||||
else if (locale == null)
|
||||
{
|
||||
put(name, Locale.ROOT, rss);
|
||||
}
|
||||
else
|
||||
{
|
||||
this.cache.put(key(name, locale), rss);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param name
|
||||
* @param locale
|
||||
* @param rss
|
||||
*/
|
||||
public void put(final String name, final String rss)
|
||||
{
|
||||
put(name, Locale.ROOT, rss);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param name
|
||||
*/
|
||||
public void remove(final String name)
|
||||
{
|
||||
remove(name, Locale.ROOT);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param name
|
||||
*/
|
||||
public void remove(final String name, final Locale locale)
|
||||
{
|
||||
if (name == null)
|
||||
{
|
||||
throw new NullPointerException("key is null.");
|
||||
}
|
||||
else
|
||||
{
|
||||
this.cache.remove(name + "-" + locale.getLanguage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param key
|
||||
*/
|
||||
public void setOudated(final String name)
|
||||
{
|
||||
for (String subkey : subkeys(name))
|
||||
{
|
||||
if (subkey.startsWith(name))
|
||||
{
|
||||
this.cache.remove(subkey);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param key
|
||||
*/
|
||||
public StringList subkeys(final String name)
|
||||
{
|
||||
StringList result;
|
||||
|
||||
//
|
||||
result = new StringList();
|
||||
|
||||
//
|
||||
for (String key : this.cache.keySet())
|
||||
{
|
||||
if (key.startsWith(name))
|
||||
{
|
||||
result.add(key);
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static RSSCache instance()
|
||||
{
|
||||
return instance;
|
||||
}
|
||||
|
||||
}
|
154
src/fr/devinsy/util/rss/RSSElement.java
Normal file
154
src/fr/devinsy/util/rss/RSSElement.java
Normal file
|
@ -0,0 +1,154 @@
|
|||
/**
|
||||
* Copyright (C) 2013-2014 Christian Pierre MOMON
|
||||
*
|
||||
* This file is part of Devinsy-utils.
|
||||
*
|
||||
* Devinsy-utils is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Devinsy-utils is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Devinsy-utils. If not, see <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
package fr.devinsy.util.rss;
|
||||
|
||||
import org.joda.time.DateTime;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Christian Pierre MOMON (christian.momon@devinsy.fr)
|
||||
*/
|
||||
public class RSSElement
|
||||
{
|
||||
public static final String DATE_PATTERN = "dd MMM YYYY hh:mm:ss Z";
|
||||
private String name;
|
||||
private String value;
|
||||
private String[] attributes;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param name
|
||||
* @param value
|
||||
*/
|
||||
public RSSElement(final String name, final DateTime value)
|
||||
{
|
||||
setName(name);
|
||||
if (value == null)
|
||||
{
|
||||
this.value = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
this.value = value.toString(DATE_PATTERN);
|
||||
}
|
||||
this.attributes = null;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param name
|
||||
* @param value
|
||||
*/
|
||||
public RSSElement(final String name, final long value)
|
||||
{
|
||||
setName(name);
|
||||
this.value = String.valueOf(value);
|
||||
this.attributes = null;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param name
|
||||
* @param value
|
||||
*/
|
||||
public RSSElement(final String name, final long value, final String... attributes)
|
||||
{
|
||||
setName(name);
|
||||
this.value = String.valueOf(value);
|
||||
this.attributes = attributes;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param name
|
||||
* @param value
|
||||
*/
|
||||
public RSSElement(final String name, final String value)
|
||||
{
|
||||
setName(name);
|
||||
this.value = value;
|
||||
this.attributes = null;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param name
|
||||
* @param value
|
||||
*/
|
||||
public RSSElement(final String name, final String value, final String... attributes)
|
||||
{
|
||||
setName(name);
|
||||
this.value = value;
|
||||
this.attributes = attributes;
|
||||
}
|
||||
|
||||
public String[] getAttributes()
|
||||
{
|
||||
return this.attributes;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String getName()
|
||||
{
|
||||
return this.name;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String getValue()
|
||||
{
|
||||
return this.value;
|
||||
}
|
||||
|
||||
public void setAttributes(final String[] attributes)
|
||||
{
|
||||
this.attributes = attributes;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param name
|
||||
*/
|
||||
public void setName(final String name)
|
||||
{
|
||||
if (name == null)
|
||||
{
|
||||
throw new NullPointerException("name is null");
|
||||
}
|
||||
else
|
||||
{
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param value
|
||||
*/
|
||||
public void setValue(final String value)
|
||||
{
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
}
|
172
src/fr/devinsy/util/rss/RSSWriter.java
Normal file
172
src/fr/devinsy/util/rss/RSSWriter.java
Normal file
|
@ -0,0 +1,172 @@
|
|||
/**
|
||||
* Copyright (C) 2013-2014 Christian Pierre MOMON
|
||||
*
|
||||
* This file is part of Devinsy-utils.
|
||||
*
|
||||
* Devinsy-utils is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Devinsy-utils is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Devinsy-utils. If not, see <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
package fr.devinsy.util.rss;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.io.Writer;
|
||||
|
||||
import fr.devinsy.util.xml.XMLWriter;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Christian Pierre MOMON (christian.momon@devinsy.fr)
|
||||
*/
|
||||
public class RSSWriter
|
||||
{
|
||||
private XMLWriter out;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param file
|
||||
* @throws FileNotFoundException
|
||||
* @throws UnsupportedEncodingException
|
||||
*/
|
||||
public RSSWriter(final File file) throws UnsupportedEncodingException, FileNotFoundException
|
||||
{
|
||||
this.out = new XMLWriter(file);
|
||||
writeRSSHeader();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param target
|
||||
* @throws UnsupportedEncodingException
|
||||
*/
|
||||
public RSSWriter(final OutputStream target) throws UnsupportedEncodingException
|
||||
{
|
||||
this.out = new XMLWriter(target);
|
||||
writeRSSHeader();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param target
|
||||
* @throws UnsupportedEncodingException
|
||||
*/
|
||||
public RSSWriter(final Writer target) throws UnsupportedEncodingException
|
||||
{
|
||||
this.out = new XMLWriter(target);
|
||||
writeRSSHeader();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public void close() throws IOException
|
||||
{
|
||||
if (this.out != null)
|
||||
{
|
||||
this.out.writeEndTag("channel");
|
||||
this.out.writeEndTag("rss");
|
||||
this.out.close();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public void flush() throws IOException
|
||||
{
|
||||
if (this.out != null)
|
||||
{
|
||||
this.out.flush();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public void writeChannel(final String title, final String link, final String description, final RSSElement... elements)
|
||||
{
|
||||
//
|
||||
this.out.writeStartTag("channel");
|
||||
|
||||
//
|
||||
this.out.writeTag("title", title);
|
||||
this.out.writeTag("link", link);
|
||||
this.out.writeTag("description", description);
|
||||
|
||||
//
|
||||
if ((elements != null) && (elements.length > 0))
|
||||
{
|
||||
for (RSSElement element : elements)
|
||||
{
|
||||
this.out.writeTag(element.getName(), element.getValue(), element.getAttributes());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param comment
|
||||
*/
|
||||
public void writeComment(final String comment)
|
||||
{
|
||||
this.out.writeComment(comment);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public void writeItem(final String title, final String link, final String description, final RSSElement... elements)
|
||||
{
|
||||
//
|
||||
this.out.writeStartTag("item");
|
||||
|
||||
//
|
||||
this.out.writeTag("title", title);
|
||||
if (link != null)
|
||||
{
|
||||
this.out.writeTag("link", link);
|
||||
}
|
||||
if (description == null)
|
||||
{
|
||||
this.out.writeTag("description", "n/a");
|
||||
}
|
||||
else
|
||||
{
|
||||
this.out.writeTag("description", description);
|
||||
}
|
||||
|
||||
//
|
||||
if ((elements != null) && (elements.length > 0))
|
||||
{
|
||||
for (RSSElement element : elements)
|
||||
{
|
||||
this.out.writeTag(element.getName(), element.getValue(), element.getAttributes());
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
this.out.writeEndTag("item");
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private void writeRSSHeader()
|
||||
{
|
||||
this.out.writeXMLHeader();
|
||||
this.out.writeStartTag("rss", "version", "2.0");
|
||||
}
|
||||
}
|
46
src/fr/devinsy/util/strings/BufferedStringListReader.java
Normal file
46
src/fr/devinsy/util/strings/BufferedStringListReader.java
Normal file
|
@ -0,0 +1,46 @@
|
|||
package fr.devinsy.util.strings;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Christian Pierre MOMON (christian.momon@devinsy.fr)
|
||||
*
|
||||
*/
|
||||
public class BufferedStringListReader implements StringListReader
|
||||
{
|
||||
private BufferedReader in;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param in
|
||||
*/
|
||||
public BufferedStringListReader(final BufferedReader in)
|
||||
{
|
||||
this.in = in;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@Override
|
||||
public void close()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@Override
|
||||
public String readLine() throws IOException
|
||||
{
|
||||
String result;
|
||||
|
||||
result = this.in.readLine();
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
}
|
128
src/fr/devinsy/util/strings/StringLengthComparator.java
Normal file
128
src/fr/devinsy/util/strings/StringLengthComparator.java
Normal file
|
@ -0,0 +1,128 @@
|
|||
package fr.devinsy.util.strings;
|
||||
|
||||
import java.util.Comparator;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public class StringLengthComparator implements Comparator<String>
|
||||
{
|
||||
private static StringLengthComparator instance;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@Override
|
||||
public int compare(final String alpha, final String bravo)
|
||||
{
|
||||
int result;
|
||||
|
||||
//
|
||||
Integer alphaValue;
|
||||
if (alpha == null)
|
||||
{
|
||||
//
|
||||
alphaValue = null;
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
//
|
||||
alphaValue = alpha.length();
|
||||
}
|
||||
|
||||
//
|
||||
Integer bravoValue;
|
||||
if (bravo == null)
|
||||
{
|
||||
//
|
||||
bravoValue = null;
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
//
|
||||
bravoValue = bravo.length();
|
||||
}
|
||||
|
||||
//
|
||||
result = compare(alphaValue, bravoValue);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method compares two nullable string values.
|
||||
*
|
||||
* The comparison manages the local language alphabet order.
|
||||
*
|
||||
* <pre>
|
||||
* compare(null, null) = 0
|
||||
* compare(null, bravo) < 0
|
||||
* compare(alpha, null) > 0
|
||||
* compare(alpha, bravo) = alpha.compareTo(bravo)
|
||||
* </pre>
|
||||
*
|
||||
* @param alpha
|
||||
* one of the value.
|
||||
*
|
||||
* @param bravo
|
||||
* the other value.
|
||||
*
|
||||
* @return zero or a positive value or a negative value.
|
||||
*
|
||||
*/
|
||||
public static int compare(final Integer alpha, final Integer bravo)
|
||||
{
|
||||
int result;
|
||||
|
||||
//
|
||||
if ((alpha == null) && (bravo == null))
|
||||
{
|
||||
//
|
||||
result = 0;
|
||||
|
||||
}
|
||||
else if (alpha == null)
|
||||
{
|
||||
//
|
||||
result = -1;
|
||||
|
||||
}
|
||||
else if (bravo == null)
|
||||
{
|
||||
//
|
||||
result = +1;
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
//
|
||||
result = alpha.compareTo(bravo);
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static StringLengthComparator instance()
|
||||
{
|
||||
StringLengthComparator result;
|
||||
|
||||
if (instance == null)
|
||||
{
|
||||
instance = new StringLengthComparator();
|
||||
}
|
||||
|
||||
result = instance;
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
1539
src/fr/devinsy/util/strings/StringList.java
Executable file
1539
src/fr/devinsy/util/strings/StringList.java
Executable file
File diff suppressed because it is too large
Load diff
133
src/fr/devinsy/util/strings/StringListCharIterator.java
Normal file
133
src/fr/devinsy/util/strings/StringListCharIterator.java
Normal file
|
@ -0,0 +1,133 @@
|
|||
/**
|
||||
* Copyright (C) 2014-2015 Christian Pierre MOMON
|
||||
*
|
||||
* This file is part of Devinsy-utils.
|
||||
*
|
||||
* Devinsy-utils is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Devinsy-utils is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Devinsy-utils. If not, see <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
package fr.devinsy.util.strings;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public class StringListCharIterator implements Iterator<Character>
|
||||
{
|
||||
private StringList source;
|
||||
private StringListCharPosition currentPosition;
|
||||
private StringListCharPosition nextPosition;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public StringListCharIterator(final StringList source)
|
||||
{
|
||||
super();
|
||||
|
||||
this.source = source;
|
||||
this.nextPosition = new StringListCharPosition();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@Override
|
||||
public boolean hasNext()
|
||||
{
|
||||
boolean result;
|
||||
|
||||
if (this.source == null)
|
||||
{
|
||||
result = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (this.source.isOutOfBounds(this.nextPosition))
|
||||
{
|
||||
result = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
result = true;
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@Override
|
||||
public Character next()
|
||||
{
|
||||
Character result;
|
||||
|
||||
if (this.source == null)
|
||||
{
|
||||
result = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (hasNext())
|
||||
{
|
||||
result = this.source.charAt(this.nextPosition);
|
||||
|
||||
this.nextPosition.next();
|
||||
|
||||
if (this.source.isOutOfLine(this.nextPosition))
|
||||
{
|
||||
this.nextPosition.nextEndOfLine();
|
||||
|
||||
while ((!this.source.isOutOfList(this.nextPosition)) && (this.source.get(this.nextPosition.getStringIndex()) == null))
|
||||
{
|
||||
this.nextPosition.nextEndOfLine();
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
result = null;
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public StringListCharPosition nextPosition()
|
||||
{
|
||||
StringListCharPosition result;
|
||||
|
||||
result = new StringListCharPosition(this.nextPosition);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Do nothing.
|
||||
*/
|
||||
@Override
|
||||
public void remove()
|
||||
{
|
||||
// TODO or not TODO?
|
||||
}
|
||||
}
|
111
src/fr/devinsy/util/strings/StringListCharPosition.java
Normal file
111
src/fr/devinsy/util/strings/StringListCharPosition.java
Normal file
|
@ -0,0 +1,111 @@
|
|||
/**
|
||||
* Copyright (C) 2013-2015 Christian Pierre MOMON
|
||||
*
|
||||
* This file is part of Devinsy-utils.
|
||||
*
|
||||
* Devinsy-utils is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Devinsy-utils is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Devinsy-utils. If not, see <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
package fr.devinsy.util.strings;
|
||||
|
||||
/**
|
||||
* This class manages a char position in a StringList object.
|
||||
*
|
||||
* @author Christian P. Momon
|
||||
*/
|
||||
public class StringListCharPosition
|
||||
{
|
||||
private int charIndex;
|
||||
private int stringIndex;
|
||||
private int localCharIndex;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param index
|
||||
* @param stringIndex
|
||||
* @param localIndex
|
||||
*/
|
||||
public StringListCharPosition()
|
||||
{
|
||||
this.charIndex = 0;
|
||||
this.stringIndex = 0;
|
||||
this.localCharIndex = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param index
|
||||
* @param stringIndex
|
||||
* @param localIndex
|
||||
*/
|
||||
public StringListCharPosition(final int index, final int stringIndex, final int localIndex)
|
||||
{
|
||||
this.charIndex = index;
|
||||
this.stringIndex = stringIndex;
|
||||
this.localCharIndex = localIndex;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param source
|
||||
*/
|
||||
public StringListCharPosition(final StringListCharPosition source)
|
||||
{
|
||||
this.charIndex = source.getCharIndex();
|
||||
this.stringIndex = source.getStringIndex();
|
||||
this.localCharIndex = source.getLocalCharIndex();
|
||||
}
|
||||
|
||||
public int getCharIndex()
|
||||
{
|
||||
return this.charIndex;
|
||||
}
|
||||
|
||||
public int getLocalCharIndex()
|
||||
{
|
||||
return this.localCharIndex;
|
||||
}
|
||||
|
||||
public int getStringIndex()
|
||||
{
|
||||
return this.stringIndex;
|
||||
}
|
||||
|
||||
public void next()
|
||||
{
|
||||
this.charIndex += 1;
|
||||
this.localCharIndex += 1;
|
||||
}
|
||||
|
||||
public void nextEndOfLine()
|
||||
{
|
||||
this.localCharIndex = 0;
|
||||
this.stringIndex += 1;
|
||||
}
|
||||
|
||||
public void setCharIndex(final int charIndex)
|
||||
{
|
||||
this.charIndex = charIndex;
|
||||
}
|
||||
|
||||
public void setLocalCharIndex(final int localCharIndex)
|
||||
{
|
||||
this.localCharIndex = localCharIndex;
|
||||
}
|
||||
|
||||
public void setStringIndex(final int stringIndex)
|
||||
{
|
||||
this.stringIndex = stringIndex;
|
||||
}
|
||||
|
||||
}
|
130
src/fr/devinsy/util/strings/StringListInputStream.java
Normal file
130
src/fr/devinsy/util/strings/StringListInputStream.java
Normal file
|
@ -0,0 +1,130 @@
|
|||
/**
|
||||
* Copyright (C) 2013-2015 Christian Pierre MOMON
|
||||
*
|
||||
* This file is part of Devinsy-utils.
|
||||
*
|
||||
* Devinsy-utils is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Devinsy-utils is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Devinsy-utils. If not, see <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
package fr.devinsy.util.strings;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
* TODO CREATION STEP.
|
||||
*/
|
||||
public class StringListInputStream extends InputStream
|
||||
{
|
||||
private StringList in;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public StringListInputStream()
|
||||
{
|
||||
this.in = new StringList();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public StringListInputStream(final int size)
|
||||
{
|
||||
this.in = new StringList(size);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public StringListInputStream(final StringList source)
|
||||
{
|
||||
if (source == null)
|
||||
{
|
||||
throw new NullPointerException("source is null.");
|
||||
}
|
||||
else
|
||||
{
|
||||
this.in = source;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public int available() throws IOException
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public void close() throws IOException
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public synchronized void mark(final int readlimit)
|
||||
{
|
||||
// TODO
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public boolean markSupported()
|
||||
{
|
||||
boolean result;
|
||||
|
||||
result = true;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public int read() throws IOException
|
||||
{
|
||||
// TODO Auto-generated method stub
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public synchronized void reset() throws IOException
|
||||
{
|
||||
// TODO
|
||||
throw new IOException("mark/reset not supported");
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public long skip(final long n) throws IOException
|
||||
{
|
||||
// TODO
|
||||
return 0;
|
||||
}
|
||||
}
|
23
src/fr/devinsy/util/strings/StringListReader.java
Normal file
23
src/fr/devinsy/util/strings/StringListReader.java
Normal file
|
@ -0,0 +1,23 @@
|
|||
package fr.devinsy.util.strings;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Christian Pierre MOMON (christian.momon@devinsy.fr)
|
||||
*
|
||||
*/
|
||||
public interface StringListReader extends Closeable
|
||||
{
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@Override
|
||||
void close();
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
String readLine() throws IOException;
|
||||
}
|
1038
src/fr/devinsy/util/strings/StringListUtils.java
Normal file
1038
src/fr/devinsy/util/strings/StringListUtils.java
Normal file
File diff suppressed because it is too large
Load diff
148
src/fr/devinsy/util/strings/StringListWriter.java
Executable file
148
src/fr/devinsy/util/strings/StringListWriter.java
Executable file
|
@ -0,0 +1,148 @@
|
|||
/**
|
||||
* Copyright (C) 2013-2015 Christian Pierre MOMON
|
||||
*
|
||||
* This file is part of Devinsy-utils.
|
||||
*
|
||||
* Devinsy-utils is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Devinsy-utils is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Devinsy-utils. If not, see <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
package fr.devinsy.util.strings;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Writer;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public class StringListWriter extends Writer
|
||||
{
|
||||
private StringList out;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public StringListWriter()
|
||||
{
|
||||
this.out = new StringList();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public StringListWriter(final int size)
|
||||
{
|
||||
this.out = new StringList(size);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public StringListWriter(final StringList target)
|
||||
{
|
||||
if (target == null)
|
||||
{
|
||||
throw new NullPointerException("target is null.");
|
||||
}
|
||||
else
|
||||
{
|
||||
this.out = target;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
@Override
|
||||
public void close() throws IOException
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
@Override
|
||||
public void flush() throws IOException
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
String result;
|
||||
|
||||
result = this.out.toString();
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public StringList toStringList()
|
||||
{
|
||||
StringList result;
|
||||
|
||||
result = this.out;
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
public void write(final char c) throws IOException
|
||||
{
|
||||
this.out.append(c);
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
@Override
|
||||
public void write(final char[] cbuf, final int off, final int len) throws IOException
|
||||
{
|
||||
char[] target;
|
||||
if ((off == 0) && (cbuf.length == len))
|
||||
{
|
||||
target = cbuf;
|
||||
}
|
||||
else
|
||||
{
|
||||
target = new char[len];
|
||||
for (int index = off; index < len; index++)
|
||||
{
|
||||
target[index] = cbuf[index];
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
this.out.append(new String(target));
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
@Override
|
||||
public void write(final String string) throws IOException
|
||||
{
|
||||
this.out.append(string);
|
||||
}
|
||||
}
|
411
src/fr/devinsy/util/strings/StringSet.java
Normal file
411
src/fr/devinsy/util/strings/StringSet.java
Normal file
|
@ -0,0 +1,411 @@
|
|||
/**
|
||||
* Copyright (C) 2014-2016 Christian Pierre MOMON
|
||||
*
|
||||
* This file is part of Devinsy-utils.
|
||||
*
|
||||
* Devinsy-utils is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Devinsy-utils is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Devinsy-utils. If not, see <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
package fr.devinsy.util.strings;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
|
||||
/**
|
||||
* This class is a set of String objects with specific methods. It makes
|
||||
* possible to build a string without any copy. The goal is to make easier the
|
||||
* use of set of strings.
|
||||
*/
|
||||
public class StringSet extends HashSet<String>
|
||||
{
|
||||
private static final long serialVersionUID = 6674838743930005326L;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public StringSet()
|
||||
{
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a list of string of the specified collection, in the order
|
||||
* they are returned by the collection's iterator.
|
||||
*
|
||||
* @param source
|
||||
*/
|
||||
public StringSet(final Collection<String> source)
|
||||
{
|
||||
super(source.size());
|
||||
|
||||
if (source != null)
|
||||
{
|
||||
//
|
||||
for (String string : source)
|
||||
{
|
||||
this.add(string);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public StringSet(final int initialCapacity)
|
||||
{
|
||||
super(initialCapacity);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public StringSet(final String[] source)
|
||||
{
|
||||
super();
|
||||
|
||||
if (source != null)
|
||||
{
|
||||
//
|
||||
for (String string : source)
|
||||
{
|
||||
this.add(string);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public StringSet(final StringSet source)
|
||||
{
|
||||
super();
|
||||
|
||||
if (source != null)
|
||||
{
|
||||
//
|
||||
for (String string : source)
|
||||
{
|
||||
this.add(string);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check null parameter before add.
|
||||
*/
|
||||
@Override
|
||||
public boolean add(final String string)
|
||||
{
|
||||
boolean result;
|
||||
|
||||
if (string != null)
|
||||
{
|
||||
//
|
||||
result = super.add(string);
|
||||
}
|
||||
else
|
||||
{
|
||||
//
|
||||
result = false;
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public StringSet add(final StringSet strings)
|
||||
{
|
||||
StringSet result;
|
||||
|
||||
if (strings != null)
|
||||
{
|
||||
for (String string : strings)
|
||||
{
|
||||
this.add(string);
|
||||
}
|
||||
}
|
||||
|
||||
result = this;
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deep copy and shallow copy have no sense about a list of immutable
|
||||
* objects.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public StringSet clone()
|
||||
{
|
||||
StringSet result;
|
||||
|
||||
result = new StringSet(size());
|
||||
|
||||
for (String string : this)
|
||||
{
|
||||
result.add(string);
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public int length()
|
||||
{
|
||||
int result = 0;
|
||||
|
||||
for (String string : this)
|
||||
{
|
||||
result += string.length();
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public StringSet merge()
|
||||
{
|
||||
StringSet result;
|
||||
|
||||
StringBuffer buffer = new StringBuffer(length());
|
||||
|
||||
for (String string : this)
|
||||
{
|
||||
buffer.append(string);
|
||||
}
|
||||
|
||||
String merge = buffer.toString();
|
||||
|
||||
clear();
|
||||
add(merge);
|
||||
|
||||
//
|
||||
result = this;
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public StringSet put(final char character)
|
||||
{
|
||||
StringSet result;
|
||||
|
||||
this.add(String.valueOf(character));
|
||||
|
||||
result = this;
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public StringSet put(final double value)
|
||||
{
|
||||
StringSet result;
|
||||
|
||||
result = this.put(String.valueOf(value));
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public StringSet put(final int value)
|
||||
{
|
||||
StringSet result;
|
||||
|
||||
result = this.put(String.valueOf(value));
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public StringSet put(final long value)
|
||||
{
|
||||
StringSet result;
|
||||
|
||||
result = this.put(String.valueOf(value));
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public StringSet put(final Object value)
|
||||
{
|
||||
StringSet result;
|
||||
|
||||
if (value != null)
|
||||
{
|
||||
this.put(value.toString());
|
||||
}
|
||||
|
||||
result = this;
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check null parameter before add.
|
||||
*/
|
||||
public StringSet put(final String string)
|
||||
{
|
||||
StringSet result;
|
||||
|
||||
if (string != null)
|
||||
{
|
||||
this.add(string);
|
||||
}
|
||||
|
||||
result = this;
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public StringSet put(final String... strings)
|
||||
{
|
||||
StringSet result;
|
||||
|
||||
if (strings != null)
|
||||
{
|
||||
for (String string : strings)
|
||||
{
|
||||
this.put(string);
|
||||
}
|
||||
}
|
||||
|
||||
result = this;
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public StringSet put(final StringList strings)
|
||||
{
|
||||
StringSet result;
|
||||
|
||||
if (strings != null)
|
||||
{
|
||||
for (String string : strings)
|
||||
{
|
||||
this.put(string);
|
||||
}
|
||||
}
|
||||
|
||||
result = this;
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public StringSet put(final StringSet strings)
|
||||
{
|
||||
StringSet result;
|
||||
|
||||
if (strings != null)
|
||||
{
|
||||
for (String string : strings)
|
||||
{
|
||||
this.put(string);
|
||||
}
|
||||
}
|
||||
|
||||
result = this;
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public StringSet putNullable(final Object value)
|
||||
{
|
||||
StringSet result;
|
||||
|
||||
if (value == null)
|
||||
{
|
||||
super.add((String) null);
|
||||
}
|
||||
else
|
||||
{
|
||||
this.put(value.toString());
|
||||
}
|
||||
|
||||
result = this;
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
String result;
|
||||
|
||||
result = toStringList().toStringWithCommas();
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public StringList toStringList()
|
||||
{
|
||||
StringList result;
|
||||
|
||||
result = new StringList(this);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
}
|
134
src/fr/devinsy/util/unix/CachedFile.java
Normal file
134
src/fr/devinsy/util/unix/CachedFile.java
Normal file
|
@ -0,0 +1,134 @@
|
|||
/**
|
||||
* Copyright (C) 2006-2010, 2013-2014 Christian Pierre MOMON
|
||||
*
|
||||
* This file is part of Devinsy-utils.
|
||||
*
|
||||
* Devinsy-utils is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Devinsy-utils is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Devinsy-utils. If not, see <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
package fr.devinsy.util.unix;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Christian Pierre MOMON (christian.momon@devinsy.fr)
|
||||
*/
|
||||
public class CachedFile
|
||||
{
|
||||
public enum Status
|
||||
{
|
||||
NOT_LOAD, EXPIRED, UPDATED
|
||||
}
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(CachedFile.class);
|
||||
|
||||
protected String sourceName;
|
||||
protected long sourceTime;
|
||||
protected Status status;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public CachedFile(final String fileName)
|
||||
{
|
||||
this.sourceName = fileName;
|
||||
this.sourceTime = 0;
|
||||
this.status = Status.NOT_LOAD;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
protected File getSourceFile()
|
||||
{
|
||||
File result;
|
||||
|
||||
if (this.sourceName == null)
|
||||
{
|
||||
result = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
File source = new File(this.sourceName);
|
||||
|
||||
if (!source.exists())
|
||||
{
|
||||
logger.error("source file defined but not found");
|
||||
result = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
result = source;
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
protected Status getStatus()
|
||||
{
|
||||
Status result;
|
||||
|
||||
File source = getSourceFile();
|
||||
|
||||
if (source == null)
|
||||
{
|
||||
this.status = Status.NOT_LOAD;
|
||||
}
|
||||
else if (this.sourceTime != source.lastModified())
|
||||
{
|
||||
this.status = Status.EXPIRED;
|
||||
}
|
||||
|
||||
result = this.status;
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public void setNotLoad()
|
||||
{
|
||||
this.status = Status.NOT_LOAD;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public void setUpdated()
|
||||
{
|
||||
File source = getSourceFile();
|
||||
|
||||
if (source == null)
|
||||
{
|
||||
this.status = Status.NOT_LOAD;
|
||||
}
|
||||
else
|
||||
{
|
||||
this.sourceTime = source.lastModified();
|
||||
this.status = Status.UPDATED;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ////////////////////////////////////////////////////////////////////////
|
200
src/fr/devinsy/util/unix/EtcGroupFile.java
Normal file
200
src/fr/devinsy/util/unix/EtcGroupFile.java
Normal file
|
@ -0,0 +1,200 @@
|
|||
/**
|
||||
* Copyright (C) 2006-2010, 2013-2014 Christian Pierre MOMON
|
||||
*
|
||||
* This file is part of Devinsy-utils.
|
||||
*
|
||||
* Devinsy-utils is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Devinsy-utils is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Devinsy-utils. If not, see <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
package fr.devinsy.util.unix;
|
||||
|
||||
import java.util.Vector;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Christian Pierre MOMON (christian.momon@devinsy.fr)
|
||||
*/
|
||||
public class EtcGroupFile extends CachedFile
|
||||
{
|
||||
// static private final Logger logger =
|
||||
// LoggerFactory.getLogger(EtcGroupFile.class);
|
||||
|
||||
private static EtcGroupFile instance = null;
|
||||
protected Groups groups;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
protected EtcGroupFile()
|
||||
{
|
||||
super("/etc/group");
|
||||
this.groups = null;
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
public boolean contains(final String name)
|
||||
{
|
||||
boolean result;
|
||||
|
||||
Groups groups = updatedGroups();
|
||||
|
||||
if (groups == null)
|
||||
{
|
||||
result = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
result = groups.contains(name);
|
||||
}
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public Group get(final int gid)
|
||||
{
|
||||
Group result;
|
||||
|
||||
Groups groups = updatedGroups();
|
||||
|
||||
if (groups == null)
|
||||
{
|
||||
result = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
result = groups.getByGid(gid);
|
||||
}
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public Group get(final String name)
|
||||
{
|
||||
Group result;
|
||||
|
||||
Groups groups = updatedGroups();
|
||||
|
||||
if (groups == null)
|
||||
{
|
||||
result = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
result = groups.getByName(name);
|
||||
}
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public Vector<String> getLoginGroups(final String login)
|
||||
{
|
||||
Vector<String> result;
|
||||
|
||||
Groups groups = updatedGroups();
|
||||
|
||||
result = groups.getLoginGroups(login);
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public String getLoginGroupsString(final String login)
|
||||
{
|
||||
String result;
|
||||
|
||||
this.groups = updatedGroups();
|
||||
|
||||
result = this.groups.getLoginGroupsString(login);
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
String result;
|
||||
|
||||
result = this.groups.toString();
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
protected Groups updatedGroups()
|
||||
{
|
||||
Groups result;
|
||||
|
||||
if (getStatus() != Status.UPDATED)
|
||||
{
|
||||
this.groups = EtcGroupFileReader.load();
|
||||
|
||||
if (this.groups == null)
|
||||
{
|
||||
setNotLoad();
|
||||
}
|
||||
else
|
||||
{
|
||||
setUpdated();
|
||||
}
|
||||
}
|
||||
|
||||
result = this.groups;
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
static public EtcGroupFile instance()
|
||||
{
|
||||
EtcGroupFile result;
|
||||
|
||||
if (EtcGroupFile.instance == null)
|
||||
{
|
||||
EtcGroupFile.instance = new EtcGroupFile();
|
||||
}
|
||||
|
||||
result = EtcGroupFile.instance;
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
}
|
||||
|
||||
// ////////////////////////////////////////////////////////////////////////
|
|
@ -1,32 +1,31 @@
|
|||
/*
|
||||
* Copyright (C) 2006-2021 Christian Pierre MOMON
|
||||
/**
|
||||
* Copyright (C) 2006-2010, 2013-2014 Christian Pierre MOMON
|
||||
*
|
||||
* This file is part of Devinsy-unix.
|
||||
* This file is part of Devinsy-utils.
|
||||
*
|
||||
* Devinsy-unix is free software: you can redistribute it and/or modify
|
||||
* Devinsy-utils is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Devinsy-unix is distributed in the hope that it will be useful,
|
||||
* Devinsy-utils is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Devinsy-unix. If not, see <http://www.gnu.org/licenses/>
|
||||
* along with Devinsy-utils. If not, see <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
package fr.devinsy.unix;
|
||||
package fr.devinsy.util.unix;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* The Class EtcGroupFileReader.
|
||||
*
|
||||
* @author Christian Pierre MOMON (christian.momon@devinsy.fr)
|
||||
*/
|
||||
|
@ -35,37 +34,43 @@ public class EtcGroupFileReader
|
|||
private static final Logger logger = LoggerFactory.getLogger(EtcGroupFileReader.class);
|
||||
|
||||
/**
|
||||
* Load.
|
||||
*
|
||||
* @return the groups
|
||||
*
|
||||
*/
|
||||
public static Groups load()
|
||||
{
|
||||
Groups result;
|
||||
|
||||
BufferedReader file = null;
|
||||
BufferedReader file;
|
||||
|
||||
try
|
||||
{
|
||||
file = new BufferedReader(new FileReader("/etc/group"));
|
||||
}
|
||||
catch (FileNotFoundException exception)
|
||||
{
|
||||
logger.error("File not found");
|
||||
file = null;
|
||||
}
|
||||
|
||||
if (file == null)
|
||||
{
|
||||
result = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
result = new Groups();
|
||||
boolean ended = false;
|
||||
while (!ended)
|
||||
{
|
||||
String line = file.readLine();
|
||||
|
||||
if (line == null)
|
||||
{
|
||||
ended = true;
|
||||
}
|
||||
else
|
||||
try
|
||||
{
|
||||
String line;
|
||||
while ((line = file.readLine()) != null)
|
||||
{
|
||||
String[] tokens = line.split(":");
|
||||
|
||||
Group group = new Group();
|
||||
group.setName(tokens[0]);
|
||||
group.setPassword(tokens[1]);
|
||||
group.setGid(Integer.valueOf(tokens[2]).intValue());
|
||||
group.setGid((new Integer(tokens[2])).intValue());
|
||||
|
||||
// Manage the case of empty shell.
|
||||
if (tokens.length == 4)
|
||||
|
@ -79,29 +84,19 @@ public class EtcGroupFileReader
|
|||
}
|
||||
result.add(group);
|
||||
}
|
||||
|
||||
file.close();
|
||||
}
|
||||
}
|
||||
catch (java.io.IOException exception)
|
||||
{
|
||||
logger.error("IO error catched", exception);
|
||||
result = null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (file != null)
|
||||
catch (java.io.IOException exception)
|
||||
{
|
||||
try
|
||||
{
|
||||
file.close();
|
||||
}
|
||||
catch (IOException exception)
|
||||
{
|
||||
logger.error("Error closing.", exception);
|
||||
}
|
||||
logger.error("Exception here.");
|
||||
result = null;
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
return (result);
|
||||
}
|
||||
}
|
||||
|
||||
// ////////////////////////////////////////////////////////////////////////
|
186
src/fr/devinsy/util/unix/EtcPasswdFile.java
Normal file
186
src/fr/devinsy/util/unix/EtcPasswdFile.java
Normal file
|
@ -0,0 +1,186 @@
|
|||
/**
|
||||
* Copyright (C) 2006-2010, 2013-2014 Christian Pierre MOMON
|
||||
*
|
||||
* This file is part of Devinsy-utils.
|
||||
*
|
||||
* Devinsy-utils is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Devinsy-utils is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Devinsy-utils. If not, see <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
package fr.devinsy.util.unix;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Christian Pierre MOMON (christian.momon@devinsy.fr)
|
||||
*/
|
||||
public class EtcPasswdFile extends CachedFile
|
||||
{
|
||||
private static final Logger logger = LoggerFactory.getLogger(EtcPasswdFile.class);
|
||||
|
||||
private static EtcPasswdFile instance = null;
|
||||
protected Users users;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
protected EtcPasswdFile()
|
||||
{
|
||||
super("/etc/passwd");
|
||||
this.users = null;
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
public boolean contains(final String login)
|
||||
{
|
||||
boolean result;
|
||||
|
||||
Users users = updatedUsers();
|
||||
|
||||
if (users == null)
|
||||
{
|
||||
result = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
result = users.contains(login);
|
||||
}
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public User get(final int uid)
|
||||
{
|
||||
User result;
|
||||
|
||||
Users users = updatedUsers();
|
||||
|
||||
if (users == null)
|
||||
{
|
||||
result = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
result = users.getByUid(uid);
|
||||
}
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public User get(final String login)
|
||||
{
|
||||
User result;
|
||||
|
||||
Users users = updatedUsers();
|
||||
|
||||
if (users == null)
|
||||
{
|
||||
result = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
result = users.getByLogin(login);
|
||||
}
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
String result;
|
||||
|
||||
result = this.users.toString();
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
protected Users update()
|
||||
{
|
||||
Users result;
|
||||
|
||||
logger.debug("updating");
|
||||
this.users = EtcPasswdFileReader.load();
|
||||
|
||||
if (this.users == null)
|
||||
{
|
||||
setNotLoad();
|
||||
}
|
||||
else
|
||||
{
|
||||
setUpdated();
|
||||
}
|
||||
|
||||
result = this.users;
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
protected Users updatedUsers()
|
||||
{
|
||||
Users result;
|
||||
|
||||
if (getStatus() != Status.UPDATED)
|
||||
{
|
||||
update();
|
||||
}
|
||||
|
||||
result = this.users;
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public static EtcPasswdFile instance()
|
||||
{
|
||||
EtcPasswdFile result;
|
||||
|
||||
if (EtcPasswdFile.instance == null)
|
||||
{
|
||||
EtcPasswdFile.instance = new EtcPasswdFile();
|
||||
}
|
||||
|
||||
result = EtcPasswdFile.instance;
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
}
|
||||
|
||||
// ////////////////////////////////////////////////////////////////////////
|
|
@ -1,72 +1,77 @@
|
|||
/*
|
||||
* Copyright (C) 2006-2021 Christian Pierre MOMON
|
||||
/**
|
||||
* Copyright (C) 2006-2010, 2013-2014 Christian Pierre MOMON
|
||||
*
|
||||
* This file is part of Devinsy-unix.
|
||||
* This file is part of Devinsy-utils.
|
||||
*
|
||||
* Devinsy-unix is free software: you can redistribute it and/or modify
|
||||
* Devinsy-utils is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Devinsy-unix is distributed in the hope that it will be useful,
|
||||
* Devinsy-utils is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Devinsy-unix. If not, see <http://www.gnu.org/licenses/>
|
||||
* along with Devinsy-utils. If not, see <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
package fr.devinsy.unix;
|
||||
package fr.devinsy.util.unix;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* The Class EtcPasswdFileReader.
|
||||
*
|
||||
* @author Christian Pierre MOMON (christian.momon@devinsy.fr)
|
||||
*/
|
||||
public class EtcPasswdFileReader
|
||||
{
|
||||
private static final Logger logger = LoggerFactory.getLogger(EtcPasswdFileReader.class);
|
||||
|
||||
/**
|
||||
* This method loads users from the /etc/password file.
|
||||
*
|
||||
* @return the users
|
||||
*
|
||||
*/
|
||||
public static Users load()
|
||||
{
|
||||
Users result;
|
||||
|
||||
BufferedReader file = null;
|
||||
BufferedReader file;
|
||||
|
||||
try
|
||||
{
|
||||
file = new BufferedReader(new FileReader("/etc/passwd"));
|
||||
}
|
||||
catch (FileNotFoundException exception)
|
||||
{
|
||||
logger.error("File not found");
|
||||
file = null;
|
||||
}
|
||||
|
||||
if (file == null)
|
||||
{
|
||||
result = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
result = new Users();
|
||||
|
||||
boolean ended = false;
|
||||
while (!ended)
|
||||
try
|
||||
{
|
||||
String line = file.readLine();
|
||||
|
||||
if (line == null)
|
||||
{
|
||||
ended = true;
|
||||
}
|
||||
else
|
||||
String line;
|
||||
while ((line = file.readLine()) != null)
|
||||
{
|
||||
String[] tokens = line.split(":");
|
||||
|
||||
User user = new User();
|
||||
user.setLogin(tokens[0]);
|
||||
user.setPassword(tokens[1]);
|
||||
user.setUid(Integer.valueOf(tokens[2]).intValue());
|
||||
user.setGid(Integer.valueOf(tokens[3]).intValue());
|
||||
user.setUid((new Integer(tokens[2])).intValue());
|
||||
user.setGid((new Integer(tokens[3])).intValue());
|
||||
user.setRealName(tokens[4]);
|
||||
user.setHomeDirectory(tokens[5]);
|
||||
|
||||
|
@ -82,29 +87,19 @@ public class EtcPasswdFileReader
|
|||
|
||||
result.add(user);
|
||||
}
|
||||
|
||||
file.close();
|
||||
}
|
||||
}
|
||||
catch (java.io.IOException exception)
|
||||
{
|
||||
logger.error("IO error catched", exception);
|
||||
result = null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (file != null)
|
||||
catch (java.io.IOException exception)
|
||||
{
|
||||
try
|
||||
{
|
||||
file.close();
|
||||
}
|
||||
catch (IOException exception)
|
||||
{
|
||||
logger.error("Error closing.", exception);
|
||||
}
|
||||
logger.error("Exception here.");
|
||||
result = null;
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
return (result);
|
||||
}
|
||||
}
|
||||
|
||||
// ////////////////////////////////////////////////////////////////////////
|
|
@ -1,28 +1,28 @@
|
|||
/*
|
||||
* Copyright (C) 2006-2021 Christian Pierre MOMON
|
||||
/**
|
||||
* Copyright (C) 2006-2010, 2013-2014 Christian Pierre MOMON
|
||||
*
|
||||
* This file is part of Devinsy-unix.
|
||||
* This file is part of Devinsy-utils.
|
||||
*
|
||||
* Devinsy-unix is free software: you can redistribute it and/or modify
|
||||
* Devinsy-utils is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Devinsy-unix is distributed in the hope that it will be useful,
|
||||
* Devinsy-utils is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Devinsy-unix. If not, see <http://www.gnu.org/licenses/>
|
||||
* along with Devinsy-utils. If not, see <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
package fr.devinsy.unix;
|
||||
package fr.devinsy.util.unix;
|
||||
|
||||
import java.util.Vector;
|
||||
|
||||
/**
|
||||
* The Class Group.
|
||||
*
|
||||
* @author Christian Pierre MOMON (christian.momon@devinsy.fr)
|
||||
*/
|
||||
public class Group
|
||||
{
|
||||
|
@ -42,13 +42,13 @@ public class Group
|
|||
* };
|
||||
*/
|
||||
|
||||
private String name;
|
||||
private String password;
|
||||
private int gid;
|
||||
private Vector<String> members;
|
||||
protected String name;
|
||||
protected String password;
|
||||
protected int gid;
|
||||
protected Vector<String> members;
|
||||
|
||||
/**
|
||||
* Instantiates a new group.
|
||||
*
|
||||
*/
|
||||
public Group()
|
||||
{
|
||||
|
@ -59,10 +59,7 @@ public class Group
|
|||
}
|
||||
|
||||
/**
|
||||
* Adds the member.
|
||||
*
|
||||
* @param login
|
||||
* the login
|
||||
*
|
||||
*/
|
||||
public void addMember(final String login)
|
||||
{
|
||||
|
@ -73,9 +70,7 @@ public class Group
|
|||
}
|
||||
|
||||
/**
|
||||
* Gets the gid.
|
||||
*
|
||||
* @return the gid
|
||||
*
|
||||
*/
|
||||
public int getGid()
|
||||
{
|
||||
|
@ -84,13 +79,11 @@ public class Group
|
|||
result = this.gid;
|
||||
|
||||
//
|
||||
return result;
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the members.
|
||||
*
|
||||
* @return the members
|
||||
*/
|
||||
public Vector<String> getMembers()
|
||||
{
|
||||
|
@ -99,13 +92,11 @@ public class Group
|
|||
result = this.members;
|
||||
|
||||
//
|
||||
return result;
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the name.
|
||||
*
|
||||
* @return the name
|
||||
*/
|
||||
public String getName()
|
||||
{
|
||||
|
@ -114,13 +105,11 @@ public class Group
|
|||
result = this.name;
|
||||
|
||||
//
|
||||
return result;
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the password.
|
||||
*
|
||||
* @return the password
|
||||
*/
|
||||
public String getPassword()
|
||||
{
|
||||
|
@ -129,13 +118,11 @@ public class Group
|
|||
result = this.password;
|
||||
|
||||
//
|
||||
return result;
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gid.
|
||||
*
|
||||
* @return the int
|
||||
*/
|
||||
public int gid()
|
||||
{
|
||||
|
@ -144,13 +131,11 @@ public class Group
|
|||
result = this.gid;
|
||||
|
||||
//
|
||||
return result;
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Members.
|
||||
*
|
||||
* @return the vector
|
||||
*/
|
||||
public Vector<String> members()
|
||||
{
|
||||
|
@ -159,13 +144,11 @@ public class Group
|
|||
result = this.members;
|
||||
|
||||
//
|
||||
return result;
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Name.
|
||||
*
|
||||
* @return the string
|
||||
*/
|
||||
public String name()
|
||||
{
|
||||
|
@ -174,13 +157,11 @@ public class Group
|
|||
result = this.name;
|
||||
|
||||
//
|
||||
return result;
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Passwd.
|
||||
*
|
||||
* @return the string
|
||||
*/
|
||||
public String passwd()
|
||||
{
|
||||
|
@ -189,14 +170,11 @@ public class Group
|
|||
result = this.password;
|
||||
|
||||
//
|
||||
return result;
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the gid.
|
||||
*
|
||||
* @param gid
|
||||
* the new gid
|
||||
*/
|
||||
public void setGid(final int gid)
|
||||
{
|
||||
|
@ -204,10 +182,7 @@ public class Group
|
|||
}
|
||||
|
||||
/**
|
||||
* Sets the name.
|
||||
*
|
||||
* @param name
|
||||
* the new name
|
||||
*/
|
||||
public void setName(final String name)
|
||||
{
|
||||
|
@ -215,10 +190,7 @@ public class Group
|
|||
}
|
||||
|
||||
/**
|
||||
* Sets the passwd.
|
||||
*
|
||||
* @param password
|
||||
* the new passwd
|
||||
*/
|
||||
public void setPasswd(final String password)
|
||||
{
|
||||
|
@ -226,18 +198,15 @@ public class Group
|
|||
}
|
||||
|
||||
/**
|
||||
* Sets the password.
|
||||
*
|
||||
* @param password
|
||||
* the new password
|
||||
*/
|
||||
public void setPassword(final String password)
|
||||
{
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see java.lang.Object#toString()
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@Override
|
||||
public String toString()
|
||||
|
@ -247,6 +216,8 @@ public class Group
|
|||
result = "|" + this.name + "|" + this.password + "|" + this.gid + "|" + this.members + "|";
|
||||
|
||||
//
|
||||
return result;
|
||||
return (result);
|
||||
}
|
||||
}
|
||||
|
||||
// ////////////////////////////////////////////////////////////////////////
|
|
@ -1,51 +1,48 @@
|
|||
/*
|
||||
* Copyright (C) 2006-2021 Christian Pierre MOMON
|
||||
/**
|
||||
* Copyright (C) 2006-2010, 2013-2014 Christian Pierre MOMON
|
||||
*
|
||||
* This file is part of Devinsy-unix.
|
||||
* This file is part of Devinsy-utils.
|
||||
*
|
||||
* Devinsy-unix is free software: you can redistribute it and/or modify
|
||||
* Devinsy-utils is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Devinsy-unix is distributed in the hope that it will be useful,
|
||||
* Devinsy-utils is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Devinsy-unix. If not, see <http://www.gnu.org/licenses/>
|
||||
* along with Devinsy-utils. If not, see <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
package fr.devinsy.unix;
|
||||
package fr.devinsy.util.unix;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.Vector;
|
||||
|
||||
/**
|
||||
* The Class Groups.
|
||||
*
|
||||
* @author Christian Pierre MOMON (christian.momon@devinsy.fr)
|
||||
*/
|
||||
public class Groups extends Vector<Group>
|
||||
{
|
||||
|
||||
private static final long serialVersionUID = 5802487312198869603L;
|
||||
|
||||
// static private final Logger logger =
|
||||
// LoggerFactory.getLogger(Groups.class);
|
||||
|
||||
/**
|
||||
* Instantiates a new groups.
|
||||
*
|
||||
*/
|
||||
public Groups()
|
||||
{
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* Contains.
|
||||
*
|
||||
* @param gid
|
||||
* the gid
|
||||
* @return true, if successful
|
||||
/*
|
||||
*
|
||||
*/
|
||||
public boolean contains(final int gid)
|
||||
{
|
||||
|
@ -61,15 +58,11 @@ public class Groups extends Vector<Group>
|
|||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Contains.
|
||||
*
|
||||
* @param name
|
||||
* the name
|
||||
* @return true, if successful
|
||||
/*
|
||||
*
|
||||
*/
|
||||
public boolean contains(final String name)
|
||||
{
|
||||
|
@ -85,15 +78,11 @@ public class Groups extends Vector<Group>
|
|||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the by gid.
|
||||
*
|
||||
* @param gid
|
||||
* the gid
|
||||
* @return the by gid
|
||||
*/
|
||||
public Group getByGid(final int gid)
|
||||
{
|
||||
|
@ -121,15 +110,11 @@ public class Groups extends Vector<Group>
|
|||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the by name.
|
||||
*
|
||||
* @param name
|
||||
* the name
|
||||
* @return the by name
|
||||
*
|
||||
*/
|
||||
public Group getByName(final String name)
|
||||
{
|
||||
|
@ -164,15 +149,11 @@ public class Groups extends Vector<Group>
|
|||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the login groups.
|
||||
*
|
||||
* @param login
|
||||
* the login
|
||||
* @return the login groups
|
||||
*
|
||||
*/
|
||||
public Vector<String> getLoginGroups(final String login)
|
||||
{
|
||||
|
@ -192,15 +173,11 @@ public class Groups extends Vector<Group>
|
|||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the login groups string.
|
||||
*
|
||||
* @param login
|
||||
* the login
|
||||
* @return the login groups string
|
||||
*
|
||||
*/
|
||||
public String getLoginGroupsString(final String login)
|
||||
{
|
||||
|
@ -226,11 +203,11 @@ public class Groups extends Vector<Group>
|
|||
result = string.toString();
|
||||
|
||||
//
|
||||
return result;
|
||||
return (result);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see java.util.Vector#toString()
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@Override
|
||||
public String toString()
|
||||
|
@ -249,6 +226,8 @@ public class Groups extends Vector<Group>
|
|||
result = out.toString();
|
||||
|
||||
//
|
||||
return result;
|
||||
return (result);
|
||||
}
|
||||
}
|
||||
|
||||
// ////////////////////////////////////////////////////////////////////////
|
597
src/fr/devinsy/util/unix/Unix.java
Normal file
597
src/fr/devinsy/util/unix/Unix.java
Normal file
|
@ -0,0 +1,597 @@
|
|||
/**
|
||||
* Copyright (C) 2006-2010, 2013-2015 Christian Pierre MOMON
|
||||
*
|
||||
* This file is part of Devinsy-utils.
|
||||
*
|
||||
* Devinsy-utils is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Devinsy-utils is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Devinsy-utils. If not, see <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
package fr.devinsy.util.unix;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Vector;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import fr.devinsy.util.cmdexec.CmdExec;
|
||||
import fr.devinsy.util.strings.StringListUtils;
|
||||
import fr.devinsy.util.unix.acl.Acl;
|
||||
import fr.devinsy.util.unix.acl.AclManager;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Christian Pierre MOMON (christian.momon@devinsy.fr)
|
||||
*/
|
||||
public class Unix
|
||||
{
|
||||
private static final Logger logger = LoggerFactory.getLogger(Unix.class);
|
||||
public static final String SUDO = "/usr/bin/sudo";
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public static void appendToFile(final String text, final String path) throws Exception
|
||||
{
|
||||
if ((text == null) || (text.length() == 0) || (path == null) || (path.length() == 0))
|
||||
{
|
||||
throw new Exception("Parameter undefined: [" + text + "][" + path + "].");
|
||||
}
|
||||
else
|
||||
{
|
||||
try
|
||||
{
|
||||
CmdExec.run(SUDO, "bash", "-c", "echo \"" + text + "\" >> " + path);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
throw new Exception("Error detected appending text to file [" + path + "].", exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public static void chmod(final String changes, final String path) throws Exception
|
||||
{
|
||||
if ((changes == null) || (changes.length() == 0) || (path == null) || (path.length() == 0))
|
||||
{
|
||||
throw new Exception("Parameter undefined: [" + changes + "][" + path + "].");
|
||||
}
|
||||
else if (!new File(path).exists())
|
||||
{
|
||||
throw new Exception("Path not found: [" + path + "].");
|
||||
}
|
||||
else
|
||||
{
|
||||
try
|
||||
{
|
||||
CmdExec.run(SUDO, "chmod", changes, path);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
throw new Exception("Error running chmod command for [" + changes + "][" + path + "].", exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public static void clearAcl(final String id, final String filePathName) throws Exception
|
||||
{
|
||||
AclManager.clearId(id, filePathName);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public static void clearAclGroup(final String group, final String filePathName) throws Exception
|
||||
{
|
||||
AclManager.clearGroup(group, filePathName);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public static void clearAclUser(final String login, final String filePathName) throws Exception
|
||||
{
|
||||
AclManager.clearUser(login, filePathName);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public static void createUserAccount(final String login) throws Exception
|
||||
{
|
||||
if ((login == null) || (login.length() == 0))
|
||||
{
|
||||
throw new Exception("Login parameter undefined.");
|
||||
}
|
||||
else
|
||||
{
|
||||
createUserAccount(login, login);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public static void createUserAccount(final String login, final String name) throws Exception
|
||||
{
|
||||
if ((login == null) || (login.length() == 0))
|
||||
{
|
||||
throw new Exception("Login parameter undefined.");
|
||||
}
|
||||
else if ((name == null) || (name.length() == 0))
|
||||
{
|
||||
throw new Exception("Name parameter undefined.");
|
||||
}
|
||||
else
|
||||
{
|
||||
createUserAccount(login, name, "/home/" + login);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public static void createUserAccount(final String login, final String name, final String home) throws Exception
|
||||
{
|
||||
if ((login == null) || (login.length() == 0))
|
||||
{
|
||||
throw new Exception("Login parameter undefined.");
|
||||
}
|
||||
else if ((name == null) || (name.length() == 0))
|
||||
{
|
||||
throw new Exception("Name parameter undefined.");
|
||||
}
|
||||
else if ((home == null) || (home.length() == 0))
|
||||
{
|
||||
throw new Exception("Home parameter undefined.");
|
||||
}
|
||||
else
|
||||
{
|
||||
try
|
||||
{
|
||||
logger.info("Creating user account for [" + login + "].");
|
||||
CmdExec.run(SUDO, "/usr/sbin/useradd", "-m", "-c", name, "-d", home, login);
|
||||
EtcPasswdFile.instance().update();
|
||||
logger.info("User account created for [" + login + "].");
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
throw new Exception("Error detected creating user account [" + login + "].", exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public static void createUserAccount(final String login, final String name, final String home, final String password) throws Exception
|
||||
{
|
||||
if ((password == null) || (password.length() == 0))
|
||||
{
|
||||
throw new Exception("Password parameter undefined.");
|
||||
}
|
||||
else if (Unix.isLogin(login))
|
||||
{
|
||||
throw new Exception("Login [" + login + "] already in use");
|
||||
}
|
||||
else
|
||||
{
|
||||
createUserAccount(login, name, home);
|
||||
setPassword(login, password);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public static void deleteGroup(final String group) throws Exception
|
||||
{
|
||||
if ((group == null) || (group.length() == 0))
|
||||
{
|
||||
throw new Exception("Group parameter undefined.");
|
||||
}
|
||||
else
|
||||
{
|
||||
try
|
||||
{
|
||||
logger.info("Deleting group for [" + group + "].");
|
||||
CmdExec.run(SUDO + " groupdel " + group);
|
||||
logger.info("Group deleted for [" + group + "].");
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
throw new Exception("Error running groupdel command for group [" + group + "].", exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public static void deleteUserAccount(final String login) throws Exception
|
||||
{
|
||||
if ((login == null) || (login.length() == 0))
|
||||
{
|
||||
throw new Exception("Login parameter undefined.");
|
||||
}
|
||||
else
|
||||
{
|
||||
try
|
||||
{
|
||||
logger.info("Deleting user account for [" + login + "].");
|
||||
CmdExec.run(SUDO + " /usr/sbin/userdel " + login);
|
||||
logger.info("User account delted for [" + login + "].");
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
throw new Exception("Error running userdel command for login [" + login + "].", exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public static String getAclData(final String filePathName) throws Exception
|
||||
{
|
||||
String result;
|
||||
|
||||
result = AclManager.getAclData(filePathName);
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public static String[] getAclUsers(final String filePathName) throws Exception
|
||||
{
|
||||
String[] result;
|
||||
|
||||
Acl acl = AclManager.getAcl(filePathName);
|
||||
|
||||
result = acl.currentAcl().getUserIds();
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public static boolean isGroup(final String groupName)
|
||||
{
|
||||
boolean result;
|
||||
|
||||
result = EtcGroupFile.instance().contains(groupName);
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public static boolean isLogin(final String login)
|
||||
{
|
||||
boolean result;
|
||||
|
||||
result = EtcPasswdFile.instance().contains(login);
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public static void link(final String sourcePath, final String targetPath) throws Exception
|
||||
{
|
||||
logger.info("[" + sourcePath + "][" + targetPath + "]");
|
||||
if ((sourcePath == null) || (sourcePath.length() == 0) || (targetPath == null) || (targetPath.length() == 0))
|
||||
{
|
||||
throw new Exception("Parameter undefined: [" + sourcePath + "][" + targetPath + "].");
|
||||
}
|
||||
else
|
||||
{
|
||||
File sourceFile = new File(sourcePath);
|
||||
File targetFile = new File(targetPath);
|
||||
|
||||
if (!sourceFile.exists())
|
||||
{
|
||||
throw new Exception("Source does not exist: [" + sourcePath + "].");
|
||||
}
|
||||
else if ((targetFile.exists()) && (!targetFile.isDirectory()))
|
||||
{
|
||||
throw new Exception("Target already exists: [" + targetPath + "].");
|
||||
}
|
||||
else
|
||||
{
|
||||
try
|
||||
{
|
||||
CmdExec.run(SUDO, "ln", "-s", sourcePath, targetPath);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
throw new Exception("Error detected linking [" + sourcePath + "][" + targetPath + "].", exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public static void modifyLogin(final String sourceLogin, final String targetLogin, final String sourceHomeDirectory) throws Exception
|
||||
{
|
||||
logger.info("Starting login modifying: [" + sourceLogin + "] -> [" + targetLogin + "]");
|
||||
if ((sourceLogin == null) || (sourceLogin.length() == 0))
|
||||
{
|
||||
throw new Exception("Original login parameters undefined");
|
||||
}
|
||||
else if ((targetLogin == null) || (targetLogin.length() == 0))
|
||||
{
|
||||
throw new Exception("New login parameters undefined");
|
||||
}
|
||||
else if (!Unix.isLogin(sourceLogin))
|
||||
{
|
||||
throw new Exception("Original login unknow: [" + sourceLogin + "].");
|
||||
}
|
||||
else if (Unix.isLogin(targetLogin))
|
||||
{
|
||||
throw new Exception("New login unknow: [" + targetLogin + "].");
|
||||
}
|
||||
else if (sourceHomeDirectory == null)
|
||||
{
|
||||
throw new Exception("sourceHomeDirectory parameter undefined, thus no home directory move.");
|
||||
}
|
||||
else if (!new File(sourceHomeDirectory).exists())
|
||||
{
|
||||
throw new Exception("Source home directory does not exist: [" + sourceHomeDirectory + "].");
|
||||
}
|
||||
else
|
||||
{
|
||||
String targetHomeDirectory = new File(sourceHomeDirectory).getParent().toString() + "/" + targetLogin;
|
||||
if (new File(targetHomeDirectory).exists())
|
||||
{
|
||||
throw new Exception("Target home directory already exists: [" + targetHomeDirectory + "].");
|
||||
}
|
||||
else
|
||||
{
|
||||
try
|
||||
{
|
||||
logger.info("Login modifying: [" + sourceLogin + "] -> [" + targetLogin + "]");
|
||||
CmdExec.run(SUDO + " usermod -l " + targetLogin + " " + sourceLogin);
|
||||
logger.info("Login modified: [" + sourceLogin + "] -> [" + targetLogin + "]");
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
throw new Exception("Login modification failed for [" + sourceLogin + "].", exception);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
logger.info("Renaming home directory: [" + sourceHomeDirectory + "] -> [" + targetLogin + "]");
|
||||
CmdExec.run(SUDO + " mv " + sourceHomeDirectory + " " + targetHomeDirectory);
|
||||
logger.info("Home directory renamed: [" + sourceHomeDirectory + "] -> [" + targetLogin + "]");
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
throw new Exception("Home directory rename failed for [" + sourceHomeDirectory + "].", exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
logger.info("Login modifying done: [" + sourceLogin + "] -> [" + targetLogin + "]");
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public static void recursiveChmod(final String changes, final String path) throws Exception
|
||||
{
|
||||
if ((changes == null) || (changes.length() == 0) || (path == null) || (path.length() == 0))
|
||||
{
|
||||
throw new Exception("Parameter undefined: [" + changes + "][" + path + "].");
|
||||
}
|
||||
else if (!new File(path).exists())
|
||||
{
|
||||
throw new Exception("Path not found: [" + path + "].");
|
||||
}
|
||||
else
|
||||
{
|
||||
try
|
||||
{
|
||||
CmdExec.run(SUDO, "chmod", "-R", changes, path);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
throw new Exception("Error running recursive chmod command for [" + changes + "][" + path + "].", exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public static void renameGroup(final String sourceGroup, final String targetGroup) throws Exception
|
||||
{
|
||||
logger.info("Starting group renaming: [" + sourceGroup + "] -> [" + targetGroup + "]");
|
||||
if ((sourceGroup == null) || (sourceGroup.length() == 0))
|
||||
{
|
||||
throw new Exception("Original group name parameters undefined");
|
||||
}
|
||||
else if ((targetGroup == null) || (targetGroup.length() == 0))
|
||||
{
|
||||
throw new Exception("New group name parameters undefined");
|
||||
}
|
||||
else if (!Unix.isGroup(sourceGroup))
|
||||
{
|
||||
throw new Exception("Original group unknow: [" + sourceGroup + "].");
|
||||
}
|
||||
else if (Unix.isGroup(targetGroup))
|
||||
{
|
||||
throw new Exception("New group unknow: [" + targetGroup + "].");
|
||||
}
|
||||
else
|
||||
{
|
||||
try
|
||||
{
|
||||
logger.info("Login modifying: [" + sourceGroup + "] -> [" + targetGroup + "]");
|
||||
CmdExec.run(SUDO + " groupmod -n " + targetGroup + " " + sourceGroup);
|
||||
logger.info("Login modified: [" + sourceGroup + "] -> [" + targetGroup + "]");
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
throw new Exception("Group renaming failed for [" + sourceGroup + "].", exception);
|
||||
}
|
||||
}
|
||||
logger.info("Group renaming done: [" + sourceGroup + "] -> [" + targetGroup + "]");
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public static Group searchGroup(final String groupName)
|
||||
{
|
||||
Group result;
|
||||
|
||||
result = EtcGroupFile.instance().get(groupName);
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public static User searchLogin(final String login)
|
||||
{
|
||||
User result;
|
||||
|
||||
result = EtcPasswdFile.instance().get(login);
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public static Vector<String> searchLoginGroups(final String login)
|
||||
{
|
||||
Vector<String> result;
|
||||
|
||||
result = EtcGroupFile.instance().getLoginGroups(login);
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public static void setfacl(final String... args) throws Exception
|
||||
{
|
||||
try
|
||||
{
|
||||
CmdExec.run(SUDO, "setfacl", args, 1, 6);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
throw new Exception("Error running setfacl command for " + StringListUtils.toStringWithBrackets(args) + ":" + exception.getMessage() + ".", exception);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* As 'passwd' command has not the option '--stdin' in all systems (eg.
|
||||
* Debian), this method uses the 'chpasswd' command.
|
||||
*/
|
||||
public static void setPassword(final String login, final String newPassword) throws Exception
|
||||
{
|
||||
if ((login == null) || (login.length() == 0))
|
||||
{
|
||||
throw new Exception("Login parameter undefined.");
|
||||
}
|
||||
else if (newPassword == null)
|
||||
{
|
||||
throw new Exception("New password parameter undefined.");
|
||||
}
|
||||
else
|
||||
{
|
||||
try
|
||||
{
|
||||
logger.info("Password setting for [" + login + "].");
|
||||
CmdExec.run(SUDO, "bash", "-c", "echo \"" + login + ":" + newPassword + "\"| chpasswd ");
|
||||
logger.info("Password set for [" + login + "].");
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
throw new Exception("Error detected on setting of the new password for [" + login + "].", exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* chfn [ -f full-name ] [ username ]
|
||||
*/
|
||||
public static void setRealName(final String login, final String newRealName) throws Exception
|
||||
{
|
||||
if ((login == null) || (login.length() == 0))
|
||||
{
|
||||
throw new Exception("Login parameter undefined.");
|
||||
}
|
||||
else if (newRealName == null)
|
||||
{
|
||||
throw new Exception("New real name parameter undefined.");
|
||||
}
|
||||
else
|
||||
{
|
||||
try
|
||||
{
|
||||
logger.info("Real name changing for user [" + login + "].");
|
||||
CmdExec.run(SUDO, "chfn", "-f", newRealName, login);
|
||||
EtcPasswdFile.instance().update();
|
||||
logger.info("Real name changed for user [" + login + "].");
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
throw new Exception("Error detected on setting of the new real name for [" + login + "].", exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public static void unlink(final String filePathName) throws Exception
|
||||
{
|
||||
logger.info("[" + filePathName + "]");
|
||||
if (filePathName == null)
|
||||
{
|
||||
throw new Exception("Parameter undefined: [" + filePathName + "].");
|
||||
}
|
||||
else
|
||||
{
|
||||
new File(filePathName).delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ////////////////////////////////////////////////////////////////////////
|
|
@ -1,26 +1,26 @@
|
|||
/*
|
||||
* Copyright (C) 2006-2021 Christian Pierre MOMON
|
||||
/**
|
||||
* Copyright (C) 2006-2010, 2013-2014 Christian Pierre MOMON
|
||||
*
|
||||
* This file is part of Devinsy-unix.
|
||||
* This file is part of Devinsy-utils.
|
||||
*
|
||||
* Devinsy-unix is free software: you can redistribute it and/or modify
|
||||
* Devinsy-utils is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Devinsy-unix is distributed in the hope that it will be useful,
|
||||
* Devinsy-utils is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Devinsy-unix. If not, see <http://www.gnu.org/licenses/>
|
||||
* along with Devinsy-utils. If not, see <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
package fr.devinsy.unix;
|
||||
package fr.devinsy.util.unix;
|
||||
|
||||
/**
|
||||
* The Class User.
|
||||
*
|
||||
* @author Christian Pierre MOMON (christian.momon@devinsy.fr)
|
||||
*/
|
||||
public class User
|
||||
{
|
||||
|
@ -42,16 +42,16 @@ public class User
|
|||
* };
|
||||
*/
|
||||
|
||||
private String login;
|
||||
private String password;
|
||||
private int uid;
|
||||
private int gid;
|
||||
private String realName;
|
||||
private String homeDirectory;
|
||||
private String shell;
|
||||
protected String login;
|
||||
protected String password;
|
||||
protected int uid;
|
||||
protected int gid;
|
||||
protected String realName;
|
||||
protected String homeDirectory;
|
||||
protected String shell;
|
||||
|
||||
/**
|
||||
* Instantiates a new user.
|
||||
*
|
||||
*/
|
||||
public User()
|
||||
{
|
||||
|
@ -65,10 +65,7 @@ public class User
|
|||
}
|
||||
|
||||
/**
|
||||
* Instantiates a new user.
|
||||
*
|
||||
* @param user
|
||||
* the user
|
||||
*
|
||||
*/
|
||||
public User(final User user)
|
||||
{
|
||||
|
@ -82,9 +79,7 @@ public class User
|
|||
}
|
||||
|
||||
/**
|
||||
* Gets the gid.
|
||||
*
|
||||
* @return the gid
|
||||
*
|
||||
*/
|
||||
public int getGid()
|
||||
{
|
||||
|
@ -93,13 +88,11 @@ public class User
|
|||
result = this.gid;
|
||||
|
||||
//
|
||||
return result;
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the home directory.
|
||||
*
|
||||
* @return the home directory
|
||||
*
|
||||
*/
|
||||
public String getHomeDirectory()
|
||||
{
|
||||
|
@ -108,13 +101,11 @@ public class User
|
|||
result = this.homeDirectory;
|
||||
|
||||
//
|
||||
return result;
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the login.
|
||||
*
|
||||
* @return the login
|
||||
*
|
||||
*/
|
||||
public String getLogin()
|
||||
{
|
||||
|
@ -123,13 +114,11 @@ public class User
|
|||
result = this.login;
|
||||
|
||||
//
|
||||
return result;
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the passwd.
|
||||
*
|
||||
* @return the passwd
|
||||
*
|
||||
*/
|
||||
public String getPasswd()
|
||||
{
|
||||
|
@ -138,13 +127,11 @@ public class User
|
|||
result = this.password;
|
||||
|
||||
//
|
||||
return result;
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the password.
|
||||
*
|
||||
* @return the password
|
||||
*
|
||||
*/
|
||||
public String getPassword()
|
||||
{
|
||||
|
@ -153,13 +140,11 @@ public class User
|
|||
result = this.password;
|
||||
|
||||
//
|
||||
return result;
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the real name.
|
||||
*
|
||||
* @return the real name
|
||||
*
|
||||
*/
|
||||
public String getRealName()
|
||||
{
|
||||
|
@ -168,13 +153,11 @@ public class User
|
|||
result = this.realName;
|
||||
|
||||
//
|
||||
return result;
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the shell.
|
||||
*
|
||||
* @return the shell
|
||||
*
|
||||
*/
|
||||
public String getShell()
|
||||
{
|
||||
|
@ -183,13 +166,11 @@ public class User
|
|||
result = this.shell;
|
||||
|
||||
//
|
||||
return result;
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the uid.
|
||||
*
|
||||
* @return the uid
|
||||
*
|
||||
*/
|
||||
public int getUid()
|
||||
{
|
||||
|
@ -198,13 +179,11 @@ public class User
|
|||
result = this.uid;
|
||||
|
||||
//
|
||||
return result;
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gid.
|
||||
*
|
||||
* @return the int
|
||||
*
|
||||
*/
|
||||
public int gid()
|
||||
{
|
||||
|
@ -213,13 +192,11 @@ public class User
|
|||
result = this.gid;
|
||||
|
||||
//
|
||||
return result;
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Home directory.
|
||||
*
|
||||
* @return the string
|
||||
*
|
||||
*/
|
||||
public String homeDirectory()
|
||||
{
|
||||
|
@ -228,13 +205,11 @@ public class User
|
|||
result = this.homeDirectory;
|
||||
|
||||
//
|
||||
return result;
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Login.
|
||||
*
|
||||
* @return the string
|
||||
*
|
||||
*/
|
||||
public String login()
|
||||
{
|
||||
|
@ -243,13 +218,11 @@ public class User
|
|||
result = this.login;
|
||||
|
||||
//
|
||||
return result;
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Passwd.
|
||||
*
|
||||
* @return the string
|
||||
*
|
||||
*/
|
||||
public String passwd()
|
||||
{
|
||||
|
@ -258,13 +231,11 @@ public class User
|
|||
result = this.password;
|
||||
|
||||
//
|
||||
return result;
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Real name.
|
||||
*
|
||||
* @return the string
|
||||
*
|
||||
*/
|
||||
public String realName()
|
||||
{
|
||||
|
@ -273,14 +244,11 @@ public class User
|
|||
result = this.realName;
|
||||
|
||||
//
|
||||
return result;
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the gid.
|
||||
*
|
||||
* @param gid
|
||||
* the new gid
|
||||
*
|
||||
*/
|
||||
public void setGid(final int gid)
|
||||
{
|
||||
|
@ -288,10 +256,7 @@ public class User
|
|||
}
|
||||
|
||||
/**
|
||||
* Sets the home directory.
|
||||
*
|
||||
* @param homeDirectory
|
||||
* the new home directory
|
||||
*
|
||||
*/
|
||||
public void setHomeDirectory(final String homeDirectory)
|
||||
{
|
||||
|
@ -299,10 +264,7 @@ public class User
|
|||
}
|
||||
|
||||
/**
|
||||
* Sets the login.
|
||||
*
|
||||
* @param login
|
||||
* the new login
|
||||
*
|
||||
*/
|
||||
public void setLogin(final String login)
|
||||
{
|
||||
|
@ -310,10 +272,7 @@ public class User
|
|||
}
|
||||
|
||||
/**
|
||||
* Sets the passwd.
|
||||
*
|
||||
* @param password
|
||||
* the new passwd
|
||||
*
|
||||
*/
|
||||
public void setPasswd(final String password)
|
||||
{
|
||||
|
@ -321,10 +280,7 @@ public class User
|
|||
}
|
||||
|
||||
/**
|
||||
* Sets the password.
|
||||
*
|
||||
* @param password
|
||||
* the new password
|
||||
*
|
||||
*/
|
||||
public void setPassword(final String password)
|
||||
{
|
||||
|
@ -332,10 +288,7 @@ public class User
|
|||
}
|
||||
|
||||
/**
|
||||
* Sets the real name.
|
||||
*
|
||||
* @param realName
|
||||
* the new real name
|
||||
*
|
||||
*/
|
||||
public void setRealName(final String realName)
|
||||
{
|
||||
|
@ -343,10 +296,7 @@ public class User
|
|||
}
|
||||
|
||||
/**
|
||||
* Sets the shell.
|
||||
*
|
||||
* @param shell
|
||||
* the new shell
|
||||
*
|
||||
*/
|
||||
public void setShell(final String shell)
|
||||
{
|
||||
|
@ -354,10 +304,7 @@ public class User
|
|||
}
|
||||
|
||||
/**
|
||||
* Sets the uid.
|
||||
*
|
||||
* @param uid
|
||||
* the new uid
|
||||
*
|
||||
*/
|
||||
public void setUid(final int uid)
|
||||
{
|
||||
|
@ -365,9 +312,7 @@ public class User
|
|||
}
|
||||
|
||||
/**
|
||||
* Shell.
|
||||
*
|
||||
* @return the string
|
||||
*
|
||||
*/
|
||||
public String shell()
|
||||
{
|
||||
|
@ -376,11 +321,11 @@ public class User
|
|||
result = this.shell;
|
||||
|
||||
//
|
||||
return result;
|
||||
return (result);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see java.lang.Object#toString()
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@Override
|
||||
public String toString()
|
||||
|
@ -390,13 +335,11 @@ public class User
|
|||
result = "|" + this.login + "|" + this.password + "|" + this.uid + "|" + this.gid + "|" + this.realName + "|" + this.homeDirectory + "|" + this.shell + "|";
|
||||
|
||||
//
|
||||
return result;
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Uid.
|
||||
*
|
||||
* @return the int
|
||||
*
|
||||
*/
|
||||
public int uid()
|
||||
{
|
||||
|
@ -405,6 +348,8 @@ public class User
|
|||
result = this.uid;
|
||||
|
||||
//
|
||||
return result;
|
||||
return (result);
|
||||
}
|
||||
}
|
||||
|
||||
// ////////////////////////////////////////////////////////////////////////
|
|
@ -1,29 +1,29 @@
|
|||
/*
|
||||
* Copyright (C) 2006-2021 Christian Pierre MOMON
|
||||
/**
|
||||
* Copyright (C) 2006-2010, 2013-2014 Christian Pierre MOMON
|
||||
*
|
||||
* This file is part of Devinsy-unix.
|
||||
* This file is part of Devinsy-utils.
|
||||
*
|
||||
* Devinsy-unix is free software: you can redistribute it and/or modify
|
||||
* Devinsy-utils is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Devinsy-unix is distributed in the hope that it will be useful,
|
||||
* Devinsy-utils is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Devinsy-unix. If not, see <http://www.gnu.org/licenses/>
|
||||
* along with Devinsy-utils. If not, see <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
package fr.devinsy.unix;
|
||||
package fr.devinsy.util.unix;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.Vector;
|
||||
|
||||
/**
|
||||
* The Class Users.
|
||||
*
|
||||
* @author Christian Pierre MOMON (christian.momon@devinsy.fr)
|
||||
*/
|
||||
public class Users extends Vector<User>
|
||||
{
|
||||
|
@ -33,43 +33,15 @@ public class Users extends Vector<User>
|
|||
// LoggerFactory.getLogger(Users.class);
|
||||
|
||||
/**
|
||||
* Instantiates a new users.
|
||||
*
|
||||
*/
|
||||
public Users()
|
||||
{
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the all.
|
||||
/*
|
||||
*
|
||||
* @param source
|
||||
* the source
|
||||
* @return true, if successful
|
||||
*/
|
||||
public boolean addAll(final Users source)
|
||||
{
|
||||
boolean result;
|
||||
|
||||
if (source == null)
|
||||
{
|
||||
result = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
result = super.addAll(source);
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Contains.
|
||||
*
|
||||
* @param uid
|
||||
* the uid
|
||||
* @return true, if successful
|
||||
*/
|
||||
public boolean contains(final int uid)
|
||||
{
|
||||
|
@ -85,15 +57,11 @@ public class Users extends Vector<User>
|
|||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Contains.
|
||||
*
|
||||
* @param login
|
||||
* the login
|
||||
* @return true, if successful
|
||||
/*
|
||||
*
|
||||
*/
|
||||
public boolean contains(final String login)
|
||||
{
|
||||
|
@ -109,15 +77,11 @@ public class Users extends Vector<User>
|
|||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the by login.
|
||||
*
|
||||
* @param login
|
||||
* the login
|
||||
* @return the by login
|
||||
*
|
||||
*/
|
||||
public User getByLogin(final String login)
|
||||
{
|
||||
|
@ -152,15 +116,11 @@ public class Users extends Vector<User>
|
|||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the by uid.
|
||||
*
|
||||
* @param uid
|
||||
* the uid
|
||||
* @return the by uid
|
||||
*
|
||||
*/
|
||||
public User getByUid(final int uid)
|
||||
{
|
||||
|
@ -188,11 +148,11 @@ public class Users extends Vector<User>
|
|||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
return (result);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see java.util.Vector#toString()
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@Override
|
||||
public String toString()
|
||||
|
@ -212,6 +172,8 @@ public class Users extends Vector<User>
|
|||
result = out.toString();
|
||||
|
||||
//
|
||||
return result;
|
||||
return (result);
|
||||
}
|
||||
}
|
||||
|
||||
// ////////////////////////////////////////////////////////////////////////
|
|
@ -1,25 +1,24 @@
|
|||
/*
|
||||
* Copyright (C) 2010, 2013-2014,2017-2018 Christian Pierre MOMON
|
||||
/**
|
||||
* Copyright (C) 2010, 2013-2014 Christian Pierre MOMON
|
||||
*
|
||||
* This file is part of Devinsy-unix.
|
||||
* This file is part of Devinsy-utils.
|
||||
*
|
||||
* Devinsy-unix is free software: you can redistribute it and/or modify
|
||||
* Devinsy-utils is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Devinsy-unix is distributed in the hope that it will be useful,
|
||||
* Devinsy-utils is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Devinsy-unix. If not, see <http://www.gnu.org/licenses/>
|
||||
* along with Devinsy-utils. If not, see <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
package fr.devinsy.unix.acl;
|
||||
package fr.devinsy.util.unix.acl;
|
||||
|
||||
/**
|
||||
* The Class Acl.
|
||||
*
|
||||
* @author Christian Pierre MOMON (christian.momon@devinsy.fr)
|
||||
*/
|
||||
|
@ -47,18 +46,15 @@ public class Acl
|
|||
default:other::---
|
||||
*/
|
||||
|
||||
private String filePathname;
|
||||
private String owner;
|
||||
private String group;
|
||||
protected String filePathname;
|
||||
protected String owner;
|
||||
protected String group;
|
||||
|
||||
private AclEntries currentAcl;
|
||||
private AclEntries defaultAcl;
|
||||
protected AclEntries currentAcl;
|
||||
protected AclEntries defaultAcl;
|
||||
|
||||
/**
|
||||
* Instantiates a new acl.
|
||||
*
|
||||
* @param filePathname
|
||||
* the file pathname
|
||||
*
|
||||
*/
|
||||
public Acl(final String filePathname)
|
||||
{
|
||||
|
@ -70,13 +66,7 @@ public class Acl
|
|||
}
|
||||
|
||||
/**
|
||||
* Contains.
|
||||
*
|
||||
* @param type
|
||||
* the type
|
||||
* @param id
|
||||
* the id
|
||||
* @return true, if successful
|
||||
*/
|
||||
public boolean contains(final AclEntry.Type type, final String id)
|
||||
{
|
||||
|
@ -92,15 +82,11 @@ public class Acl
|
|||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Contains group.
|
||||
*
|
||||
* @param group
|
||||
* the group
|
||||
* @return true, if successful
|
||||
*/
|
||||
public boolean containsGroup(final String group)
|
||||
{
|
||||
|
@ -109,15 +95,11 @@ public class Acl
|
|||
result = contains(AclEntry.Type.GROUP, group);
|
||||
|
||||
//
|
||||
return result;
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Contains id.
|
||||
*
|
||||
* @param id
|
||||
* the id
|
||||
* @return true, if successful
|
||||
*/
|
||||
public boolean containsId(final String id)
|
||||
{
|
||||
|
@ -133,15 +115,11 @@ public class Acl
|
|||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Contains user.
|
||||
*
|
||||
* @param login
|
||||
* the login
|
||||
* @return true, if successful
|
||||
*/
|
||||
public boolean containsUser(final String login)
|
||||
{
|
||||
|
@ -150,13 +128,11 @@ public class Acl
|
|||
result = contains(AclEntry.Type.USER, login);
|
||||
|
||||
//
|
||||
return result;
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Current acl.
|
||||
*
|
||||
* @return the acl entries
|
||||
*/
|
||||
public AclEntries currentAcl()
|
||||
{
|
||||
|
@ -165,13 +141,11 @@ public class Acl
|
|||
result = this.currentAcl;
|
||||
|
||||
//
|
||||
return result;
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Default acl.
|
||||
*
|
||||
* @return the acl entries
|
||||
*/
|
||||
public AclEntries defaultAcl()
|
||||
{
|
||||
|
@ -180,13 +154,11 @@ public class Acl
|
|||
result = this.defaultAcl;
|
||||
|
||||
//
|
||||
return result;
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
* File pathname.
|
||||
*
|
||||
* @return the string
|
||||
*/
|
||||
public String filePathname()
|
||||
{
|
||||
|
@ -195,13 +167,11 @@ public class Acl
|
|||
result = this.filePathname;
|
||||
|
||||
//
|
||||
return result;
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Group.
|
||||
*
|
||||
* @return the string
|
||||
*/
|
||||
public String group()
|
||||
{
|
||||
|
@ -210,13 +180,11 @@ public class Acl
|
|||
result = this.group;
|
||||
|
||||
//
|
||||
return result;
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Owner.
|
||||
*
|
||||
* @return the string
|
||||
*/
|
||||
public String owner()
|
||||
{
|
||||
|
@ -225,14 +193,11 @@ public class Acl
|
|||
result = this.owner;
|
||||
|
||||
//
|
||||
return result;
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the group.
|
||||
*
|
||||
* @param group
|
||||
* the new group
|
||||
*/
|
||||
public void setGroup(final String group)
|
||||
{
|
||||
|
@ -240,10 +205,7 @@ public class Acl
|
|||
}
|
||||
|
||||
/**
|
||||
* Sets the owner.
|
||||
*
|
||||
* @param owner
|
||||
* the new owner
|
||||
*/
|
||||
public void setOwner(final String owner)
|
||||
{
|
|
@ -1,28 +1,27 @@
|
|||
/*
|
||||
* Copyright (C) 2010, 2013-2014,2017-2018 Christian Pierre MOMON
|
||||
/**
|
||||
* Copyright (C) 2010, 2013-2014 Christian Pierre MOMON
|
||||
*
|
||||
* This file is part of Devinsy-unix.
|
||||
* This file is part of Devinsy-utils.
|
||||
*
|
||||
* Devinsy-unix is free software: you can redistribute it and/or modify
|
||||
* Devinsy-utils is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Devinsy-unix is distributed in the hope that it will be useful,
|
||||
* Devinsy-utils is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Devinsy-unix. If not, see <http://www.gnu.org/licenses/>
|
||||
* along with Devinsy-utils. If not, see <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
package fr.devinsy.unix.acl;
|
||||
package fr.devinsy.util.unix.acl;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.Vector;
|
||||
|
||||
/**
|
||||
* The Class AclEntries.
|
||||
*
|
||||
* @author Christian Pierre MOMON (christian.momon@devinsy.fr)
|
||||
*/
|
||||
|
@ -34,22 +33,13 @@ public class AclEntries extends Vector<AclEntry>
|
|||
// LoggerFactory.getLogger(AclEntries.class);
|
||||
|
||||
/**
|
||||
* Instantiates a new acl entries.
|
||||
*
|
||||
*/
|
||||
public AclEntries()
|
||||
{
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* Contains.
|
||||
*
|
||||
* @param type
|
||||
* the type
|
||||
* @param id
|
||||
* the id
|
||||
* @return true, if successful
|
||||
*/
|
||||
/*
|
||||
*
|
||||
*/
|
||||
|
@ -67,15 +57,11 @@ public class AclEntries extends Vector<AclEntry>
|
|||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Contains id.
|
||||
*
|
||||
* @param id
|
||||
* the id
|
||||
* @return true, if successful
|
||||
*/
|
||||
public boolean containsId(final String id)
|
||||
{
|
||||
|
@ -91,17 +77,11 @@ public class AclEntries extends Vector<AclEntry>
|
|||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the.
|
||||
*
|
||||
* @param type
|
||||
* the type
|
||||
* @param id
|
||||
* the id
|
||||
* @return the acl entry
|
||||
*
|
||||
*/
|
||||
public AclEntry get(final AclEntry.Type type, final String id)
|
||||
{
|
||||
|
@ -129,13 +109,11 @@ public class AclEntries extends Vector<AclEntry>
|
|||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the group ids.
|
||||
*
|
||||
* @return the group ids
|
||||
*
|
||||
*/
|
||||
public String[] getGroupIds()
|
||||
{
|
||||
|
@ -145,7 +123,7 @@ public class AclEntries extends Vector<AclEntry>
|
|||
|
||||
for (AclEntry entry : this)
|
||||
{
|
||||
if ((entry.type() == AclEntry.Type.GROUP) && (!ids.contains(entry.id())))
|
||||
if ((entry.type == AclEntry.Type.GROUP) && (!ids.contains(entry.id())))
|
||||
{
|
||||
ids.add(entry.id());
|
||||
}
|
||||
|
@ -154,13 +132,11 @@ public class AclEntries extends Vector<AclEntry>
|
|||
result = (String[]) ids.toArray();
|
||||
|
||||
//
|
||||
return result;
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the ids.
|
||||
*
|
||||
* @return the ids
|
||||
*
|
||||
*/
|
||||
public String[] getIds()
|
||||
{
|
||||
|
@ -179,13 +155,11 @@ public class AclEntries extends Vector<AclEntry>
|
|||
result = (String[]) ids.toArray();
|
||||
|
||||
//
|
||||
return result;
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the user ids.
|
||||
*
|
||||
* @return the user ids
|
||||
*
|
||||
*/
|
||||
public String[] getUserIds()
|
||||
{
|
||||
|
@ -196,7 +170,7 @@ public class AclEntries extends Vector<AclEntry>
|
|||
|
||||
for (AclEntry entry : this)
|
||||
{
|
||||
if ((entry.type() == AclEntry.Type.USER) && (!ids.contains(entry.id())))
|
||||
if ((entry.type == AclEntry.Type.USER) && (!ids.contains(entry.id())))
|
||||
{
|
||||
ids.add(entry.id());
|
||||
}
|
||||
|
@ -211,11 +185,11 @@ public class AclEntries extends Vector<AclEntry>
|
|||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
return (result);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see java.util.Vector#toString()
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@Override
|
||||
public String toString()
|
||||
|
@ -235,7 +209,7 @@ public class AclEntries extends Vector<AclEntry>
|
|||
result = out.toString();
|
||||
|
||||
//
|
||||
return result;
|
||||
return (result);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,25 +1,24 @@
|
|||
/*
|
||||
* Copyright (C) 2010,2013-2014,2017-2018 Christian Pierre MOMON
|
||||
/**
|
||||
* Copyright (C) 2010, 2013-2014 Christian Pierre MOMON
|
||||
*
|
||||
* This file is part of Devinsy-unix.
|
||||
* This file is part of Devinsy-utils.
|
||||
*
|
||||
* Devinsy-unix is free software: you can redistribute it and/or modify
|
||||
* Devinsy-utils is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Devinsy-unix is distributed in the hope that it will be useful,
|
||||
* Devinsy-utils is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Devinsy-unix. If not, see <http://www.gnu.org/licenses/>
|
||||
* along with Devinsy-utils. If not, see <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
package fr.devinsy.unix.acl;
|
||||
package fr.devinsy.util.unix.acl;
|
||||
|
||||
/**
|
||||
* The Class AclEntry.
|
||||
*
|
||||
* @author Christian Pierre MOMON (christian.momon@devinsy.fr)
|
||||
*/
|
||||
|
@ -40,26 +39,15 @@ public class AclEntry
|
|||
|
||||
public enum Type
|
||||
{
|
||||
NONE,
|
||||
USER,
|
||||
GROUP,
|
||||
MASK,
|
||||
OTHER
|
||||
NONE, USER, GROUP, MASK, OTHER
|
||||
};
|
||||
|
||||
private Type type;
|
||||
private String id;
|
||||
private String permission;
|
||||
protected Type type;
|
||||
protected String id;
|
||||
protected String permission;
|
||||
|
||||
/**
|
||||
* Instantiates a new acl entry.
|
||||
*
|
||||
* @param type
|
||||
* the type
|
||||
* @param id
|
||||
* the id
|
||||
* @param permission
|
||||
* the permission
|
||||
*
|
||||
*/
|
||||
public AclEntry(final Type type, final String id, final String permission)
|
||||
{
|
||||
|
@ -69,9 +57,7 @@ public class AclEntry
|
|||
}
|
||||
|
||||
/**
|
||||
* Id.
|
||||
*
|
||||
* @return the string
|
||||
*/
|
||||
public String id()
|
||||
{
|
||||
|
@ -80,13 +66,11 @@ public class AclEntry
|
|||
result = this.id;
|
||||
|
||||
//
|
||||
return result;
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Permission.
|
||||
*
|
||||
* @return the string
|
||||
*/
|
||||
public String permission()
|
||||
{
|
||||
|
@ -95,11 +79,11 @@ public class AclEntry
|
|||
result = this.type.toString() + ":" + this.id + ":" + this.permission;
|
||||
|
||||
//
|
||||
return result;
|
||||
return (result);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see java.lang.Object#toString()
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@Override
|
||||
public String toString()
|
||||
|
@ -109,13 +93,11 @@ public class AclEntry
|
|||
result = this.permission;
|
||||
|
||||
//
|
||||
return result;
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Type.
|
||||
*
|
||||
* @return the type
|
||||
*/
|
||||
public Type type()
|
||||
{
|
||||
|
@ -124,7 +106,7 @@ public class AclEntry
|
|||
result = this.type;
|
||||
|
||||
//
|
||||
return result;
|
||||
return (result);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,44 +1,42 @@
|
|||
/*
|
||||
* Copyright (C) 2010-2021 Christian Pierre MOMON
|
||||
/**
|
||||
* Copyright (C) 2010, 2013-2015 Christian Pierre MOMON
|
||||
*
|
||||
* This file is part of Devinsy-unix.
|
||||
* This file is part of Devinsy-utils.
|
||||
*
|
||||
* Devinsy-unix is free software: you can redistribute it and/or modify
|
||||
* Devinsy-utils is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Devinsy-unix is distributed in the hope that it will be useful,
|
||||
* Devinsy-utils is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Devinsy-unix. If not, see <http://www.gnu.org/licenses/>
|
||||
* along with Devinsy-utils. If not, see <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
package fr.devinsy.unix.acl;
|
||||
package fr.devinsy.util.unix.acl;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.apache.commons.lang3.ArrayUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import fr.devinsy.cmdexec.CmdExecException;
|
||||
import fr.devinsy.strings.StringsUtils;
|
||||
import fr.devinsy.unix.Unix;
|
||||
import fr.devinsy.unix.UnixException;
|
||||
import fr.devinsy.util.cmdexec.CmdExec;
|
||||
import fr.devinsy.util.strings.StringListUtils;
|
||||
import fr.devinsy.util.unix.Unix;
|
||||
|
||||
/**
|
||||
* The Class AclManager.
|
||||
*
|
||||
* @author Christian Pierre MOMON (christian.momon@devinsy.fr)
|
||||
*/
|
||||
public class AclManager
|
||||
{
|
||||
private static final Logger logger = LoggerFactory.getLogger(AclManager.class);
|
||||
public static final String SUDO = "/usr/bin/sudo";
|
||||
|
||||
final static public Pattern USER_PATTERN = Pattern.compile("^user:(.*):(.*)$");
|
||||
final static public Pattern GROUP_PATTERN = Pattern.compile("^group:(.*):(.*)$");
|
||||
|
@ -50,20 +48,13 @@ public class AclManager
|
|||
final static public Pattern DEFAULT_OTHER_PATTERN = Pattern.compile("^default:other:(.*):(.*)$");
|
||||
|
||||
/**
|
||||
* Clear group.
|
||||
*
|
||||
* @param group
|
||||
* the group
|
||||
* @param filePathName
|
||||
* the file path name
|
||||
* @throws UnixException
|
||||
* the exception
|
||||
*/
|
||||
public static void clearGroup(final String group, final String filePathName) throws UnixException
|
||||
public static void clearGroup(final String group, final String filePathName) throws Exception
|
||||
{
|
||||
if ((group == null) || (group.length() == 0) || (filePathName == null) || (filePathName.length() == 0))
|
||||
{
|
||||
throw new IllegalArgumentException("Bad parameters [" + group + "][" + filePathName + "].");
|
||||
throw new Exception("Bad parameters [" + group + "][" + filePathName + "].");
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -73,36 +64,22 @@ public class AclManager
|
|||
}
|
||||
|
||||
/**
|
||||
* Clear id.
|
||||
*
|
||||
* @param id
|
||||
* the id
|
||||
* @param filePathName
|
||||
* the file path name
|
||||
* @throws UnixException
|
||||
* the exception
|
||||
*/
|
||||
public static void clearId(final String id, final String filePathName) throws UnixException
|
||||
public static void clearId(final String id, final String filePathName) throws Exception
|
||||
{
|
||||
clearUser(id, filePathName);
|
||||
clearGroup(id, filePathName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear user.
|
||||
*
|
||||
* @param login
|
||||
* the login
|
||||
* @param filePathName
|
||||
* the file path name
|
||||
* @throws UnixException
|
||||
* the exception
|
||||
*/
|
||||
public static void clearUser(final String login, final String filePathName) throws UnixException
|
||||
public static void clearUser(final String login, final String filePathName) throws Exception
|
||||
{
|
||||
if ((login == null) || (login.length() == 0) || (filePathName == null) || (filePathName.length() == 0))
|
||||
{
|
||||
throw new IllegalArgumentException("Bad parameters [" + login + "][" + filePathName + "].");
|
||||
throw new Exception("Bad parameters [" + login + "][" + filePathName + "].");
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -112,15 +89,9 @@ public class AclManager
|
|||
}
|
||||
|
||||
/**
|
||||
* Gets the acl.
|
||||
*
|
||||
* @param filePathName
|
||||
* the file path name
|
||||
* @return the acl
|
||||
* @throws UnixException
|
||||
* the unix exception
|
||||
*/
|
||||
public static Acl getAcl(final String filePathName) throws UnixException
|
||||
public static Acl getAcl(final String filePathName) throws Exception
|
||||
{
|
||||
Acl result;
|
||||
|
||||
|
@ -204,50 +175,40 @@ public class AclManager
|
|||
}
|
||||
else
|
||||
{
|
||||
throw new UnixException("Unknow ACL entry line pattern for [" + entryLine + "].");
|
||||
throw new Exception("Unknow ACL entry line pattern for [" + entryLine + "].");
|
||||
}
|
||||
|
||||
logger.debug("Acl entry decoded: [" + entry.toString() + "]");
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the ACL data.
|
||||
*
|
||||
* @param filePathName
|
||||
* the file path name
|
||||
* @return the acl data
|
||||
* @throws UnixException
|
||||
* the unix exception
|
||||
*/
|
||||
public static String getAclData(final String filePathName) throws UnixException
|
||||
public static String getAclData(final String filePathName) throws Exception
|
||||
{
|
||||
String result;
|
||||
|
||||
try
|
||||
{
|
||||
logger.info("Getting Acl data for [" + filePathName + "].");
|
||||
result = Unix.sudo("/usr/bin/getfacl", "--no-effective", filePathName);
|
||||
result = CmdExec.run(SUDO, "/usr/bin/getfacl", "--no-effective", filePathName);
|
||||
logger.info("Acl data got for [" + filePathName + "].");
|
||||
}
|
||||
catch (CmdExecException exception)
|
||||
catch (Exception exception)
|
||||
{
|
||||
throw new UnixException("Error getting ACL for [" + filePathName + "].", exception);
|
||||
throw new Exception("Error getting ACL for [" + filePathName + "].", exception);
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the ACL entry lines.
|
||||
*
|
||||
* @param filePathName
|
||||
* the file path name
|
||||
* @return the acl entry lines
|
||||
*
|
||||
*/
|
||||
public static String[] getAclEntryLines(final String filePathName)
|
||||
{
|
||||
|
@ -263,61 +224,39 @@ public class AclManager
|
|||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if is used.
|
||||
*
|
||||
* @param type
|
||||
* the type
|
||||
* @param id
|
||||
* the id
|
||||
* @param filePathName
|
||||
* the file path name
|
||||
* @return true, if is used
|
||||
* @throws UnixException
|
||||
* the exception
|
||||
*/
|
||||
public static boolean isUsed(final AclEntry.Type type, final String id, final String filePathName) throws UnixException
|
||||
public static boolean isUsed(final AclEntry.Type type, final String id, final String filePathName) throws Exception
|
||||
{
|
||||
boolean result;
|
||||
|
||||
result = isUsed(type, id, filePathName, 0);
|
||||
|
||||
//
|
||||
return result;
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if is used.
|
||||
*
|
||||
* @param type
|
||||
* the type
|
||||
* @param id
|
||||
* the id
|
||||
* @param filePathName
|
||||
* the file path name
|
||||
* @param depth
|
||||
* the depth
|
||||
* @return true, if is used
|
||||
* @throws UnixException
|
||||
* the unix exception
|
||||
*
|
||||
*/
|
||||
public static boolean isUsed(final AclEntry.Type type, final String id, final String filePathName, final int depth) throws UnixException
|
||||
public static boolean isUsed(final AclEntry.Type type, final String id, final String filePathName, final int depth) throws Exception
|
||||
{
|
||||
boolean result;
|
||||
|
||||
if ((type == null) || (id == null) || (id.length() == 0) || (filePathName == null) || (filePathName.length() == 0) || (depth < 0))
|
||||
{
|
||||
throw new IllegalArgumentException("Bad parameter: [" + type + "][" + id + "][" + filePathName + "][" + depth + "].");
|
||||
throw new Exception("Bad parameter: [" + type + "][" + id + "][" + filePathName + "][" + depth + "].");
|
||||
}
|
||||
else
|
||||
{
|
||||
File file = new File(filePathName);
|
||||
if (!file.exists())
|
||||
{
|
||||
throw new IllegalArgumentException("File does not exist [" + filePathName + "].");
|
||||
throw new Exception("File does not exist [" + filePathName + "].");
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -338,27 +277,13 @@ public class AclManager
|
|||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if is used.
|
||||
*
|
||||
* @param type
|
||||
* the type
|
||||
* @param id
|
||||
* the id
|
||||
* @param filePath
|
||||
* the file path
|
||||
* @param filePathNames
|
||||
* the file path names
|
||||
* @param depth
|
||||
* the depth
|
||||
* @return true, if is used
|
||||
* @throws UnixException
|
||||
* the unix exception
|
||||
*
|
||||
*/
|
||||
public static boolean isUsed(final AclEntry.Type type, final String id, final String filePath, final String[] filePathNames, final int depth) throws UnixException
|
||||
public static boolean isUsed(final AclEntry.Type type, final String id, final String filePath, final String[] filePathNames, final int depth) throws Exception
|
||||
{
|
||||
boolean result;
|
||||
|
||||
|
@ -388,19 +313,11 @@ public class AclManager
|
|||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if is used.
|
||||
*
|
||||
* @param id
|
||||
* the id
|
||||
* @param filePathName
|
||||
* the file path name
|
||||
* @return true, if is used
|
||||
* @throws Exception
|
||||
* the exception
|
||||
*/
|
||||
public static boolean isUsed(final String id, final String filePathName) throws Exception
|
||||
{
|
||||
|
@ -409,36 +326,26 @@ public class AclManager
|
|||
result = isUsed(id, filePathName, 0);
|
||||
|
||||
//
|
||||
return result;
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if is used.
|
||||
*
|
||||
* @param id
|
||||
* the id
|
||||
* @param filePathName
|
||||
* the file path name
|
||||
* @param depth
|
||||
* the depth
|
||||
* @return true, if is used
|
||||
* @throws UnixException
|
||||
* the unix exception
|
||||
*
|
||||
*/
|
||||
public static boolean isUsed(final String id, final String filePathName, final int depth) throws UnixException
|
||||
public static boolean isUsed(final String id, final String filePathName, final int depth) throws Exception
|
||||
{
|
||||
boolean result;
|
||||
|
||||
if ((id == null) || (id.length() == 0) || (filePathName == null) || (filePathName.length() == 0) || (depth < 0))
|
||||
{
|
||||
throw new IllegalArgumentException("Bad parameter: [" + id + "][" + filePathName + "][" + depth + "].");
|
||||
throw new Exception("Bad parameter: [" + id + "][" + filePathName + "][" + depth + "].");
|
||||
}
|
||||
else
|
||||
{
|
||||
File file = new File(filePathName);
|
||||
if (!file.exists())
|
||||
{
|
||||
throw new IllegalArgumentException("File does not exist [" + filePathName + "].");
|
||||
throw new Exception("File does not exist [" + filePathName + "].");
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -459,23 +366,13 @@ public class AclManager
|
|||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if is used.
|
||||
*
|
||||
* @param id
|
||||
* the id
|
||||
* @param filePathNames
|
||||
* the file path names
|
||||
* @param depth
|
||||
* the depth
|
||||
* @return true, if is used
|
||||
* @throws UnixException
|
||||
* the unix exception
|
||||
*
|
||||
*/
|
||||
public static boolean isUsed(final String id, final String[] filePathNames, final int depth) throws UnixException
|
||||
public static boolean isUsed(final String id, final String[] filePathNames, final int depth) throws Exception
|
||||
{
|
||||
boolean result;
|
||||
|
||||
|
@ -505,27 +402,23 @@ public class AclManager
|
|||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the facl.
|
||||
*
|
||||
* @param args
|
||||
* the new facl
|
||||
* @throws UnixException
|
||||
* the exception
|
||||
*
|
||||
*/
|
||||
public static void setfacl(final String... args) throws UnixException
|
||||
public static void setfacl(final String... args) throws Exception
|
||||
{
|
||||
try
|
||||
{
|
||||
String[] newArgs = ArrayUtils.insert(0, args, "setfacl");
|
||||
Unix.sudo(newArgs);
|
||||
CmdExec.run(SUDO, "setfacl", args, 1, 5);
|
||||
}
|
||||
catch (CmdExecException exception)
|
||||
catch (Exception exception)
|
||||
{
|
||||
throw new UnixException("Error running setfacl command for " + StringsUtils.toStringWithBrackets(args) + ":" + exception.getMessage() + ".");
|
||||
throw new Exception("Error running setfacl command for " + StringListUtils.toStringWithBrackets(args) + ":" + exception.getMessage() + ".");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ////////////////////////////////////////////////////////////////////////
|
160
src/fr/devinsy/util/xml/XMLAttributes.java
Normal file
160
src/fr/devinsy/util/xml/XMLAttributes.java
Normal file
|
@ -0,0 +1,160 @@
|
|||
/**
|
||||
* Copyright (C) 2013-2014 Christian Pierre MOMON
|
||||
*
|
||||
* This file is part of Devinsy-utils.
|
||||
*
|
||||
* Devinsy-utils is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Devinsy-utils is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Devinsy-utils. If not, see <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
package fr.devinsy.util.xml;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.xml.stream.events.Attribute;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Christian Pierre MOMON (christian.momon@devinsy.fr)
|
||||
*/
|
||||
public class XMLAttributes extends HashMap<String, Attribute> implements Iterable<Attribute>
|
||||
{
|
||||
|
||||
private static final long serialVersionUID = 8456469741805779474L;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public XMLAttributes()
|
||||
{
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public XMLAttributes(final int capacity)
|
||||
{
|
||||
super(capacity);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public XMLAttributes(final Iterator<Attribute> source)
|
||||
{
|
||||
super();
|
||||
if (source != null)
|
||||
{
|
||||
while (source.hasNext())
|
||||
{
|
||||
Attribute attribute = source.next();
|
||||
|
||||
add(attribute);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public XMLAttributes(final XMLAttributes source)
|
||||
{
|
||||
super();
|
||||
addAll(source);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param attribute
|
||||
*/
|
||||
public void add(final Attribute attribute)
|
||||
{
|
||||
if (attribute != null)
|
||||
{
|
||||
put(attribute.getName().getLocalPart(), attribute);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param source
|
||||
*/
|
||||
public void addAll(final XMLAttributes source)
|
||||
{
|
||||
for (Attribute attribute : source)
|
||||
{
|
||||
this.add(attribute);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param label
|
||||
* @return
|
||||
*/
|
||||
public Attribute getByLabel(final String label)
|
||||
{
|
||||
Attribute result;
|
||||
|
||||
result = get(label);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@Override
|
||||
public Iterator<Attribute> iterator()
|
||||
{
|
||||
Iterator<Attribute> result;
|
||||
|
||||
result = this.values().iterator();
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public Set<String> labels()
|
||||
{
|
||||
Set<String> result;
|
||||
|
||||
result = this.keySet();
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public List<Attribute> toList()
|
||||
{
|
||||
List<Attribute> result;
|
||||
|
||||
result = new ArrayList<Attribute>(values());
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
}
|
81
src/fr/devinsy/util/xml/XMLBadFormatException.java
Normal file
81
src/fr/devinsy/util/xml/XMLBadFormatException.java
Normal file
|
@ -0,0 +1,81 @@
|
|||
/**
|
||||
* Copyright (C) 2013-2014 Christian Pierre MOMON
|
||||
*
|
||||
* This file is part of Devinsy-utils.
|
||||
*
|
||||
* Devinsy-utils is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Devinsy-utils is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Devinsy-utils. If not, see <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
package fr.devinsy.util.xml;
|
||||
|
||||
import org.slf4j.helpers.MessageFormatter;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Christian Pierre MOMON (christian.momon@devinsy.fr)
|
||||
*/
|
||||
public class XMLBadFormatException extends Exception
|
||||
{
|
||||
|
||||
private static final long serialVersionUID = 768256303984176512L;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param code
|
||||
* @param message
|
||||
*/
|
||||
public XMLBadFormatException(final String message)
|
||||
{
|
||||
super(message);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param code
|
||||
* @param message
|
||||
* @param exception
|
||||
*/
|
||||
public XMLBadFormatException(final String message, final Exception exception)
|
||||
{
|
||||
super(message, exception);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param format
|
||||
* @param arguments
|
||||
*/
|
||||
public XMLBadFormatException(final String format, final Object... arguments)
|
||||
{
|
||||
this(MessageFormatter.arrayFormat(format, arguments).getMessage());
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param message
|
||||
* @param cause
|
||||
*/
|
||||
public XMLBadFormatException(final String message, final Throwable cause)
|
||||
{
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param cause
|
||||
*/
|
||||
public XMLBadFormatException(final Throwable cause)
|
||||
{
|
||||
super(cause);
|
||||
}
|
||||
}
|
617
src/fr/devinsy/util/xml/XMLReader.java
Normal file
617
src/fr/devinsy/util/xml/XMLReader.java
Normal file
|
@ -0,0 +1,617 @@
|
|||
/**
|
||||
* Copyright (C) 2013-2014 Christian Pierre MOMON
|
||||
* Copyright (C) 2017 Christian Pierre MOMON
|
||||
*
|
||||
* This file is part of Devinsy-utils.
|
||||
*
|
||||
* Devinsy-utils is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Devinsy-utils is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Devinsy-utils. If not, see <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
package fr.devinsy.util.xml;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileReader;
|
||||
import java.io.InputStream;
|
||||
import java.io.Reader;
|
||||
|
||||
import javax.xml.namespace.QName;
|
||||
import javax.xml.stream.XMLEventReader;
|
||||
import javax.xml.stream.XMLInputFactory;
|
||||
import javax.xml.stream.XMLStreamException;
|
||||
import javax.xml.stream.events.XMLEvent;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import fr.devinsy.util.strings.StringList;
|
||||
import fr.devinsy.util.xml.XMLTag.TagType;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Christian Pierre MOMON (christian.momon@devinsy.fr)
|
||||
*/
|
||||
public class XMLReader
|
||||
{
|
||||
private static final Logger logger = LoggerFactory.getLogger(XMLReader.class);
|
||||
|
||||
protected XMLEventReader in;
|
||||
private XMLEvent nextEvent;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
protected XMLReader()
|
||||
{
|
||||
this.in = null;
|
||||
this.nextEvent = null;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param file
|
||||
* @throws XMLStreamException
|
||||
* @throws FileNotFoundException
|
||||
*/
|
||||
public XMLReader(final File file) throws FileNotFoundException, XMLStreamException
|
||||
{
|
||||
this.nextEvent = null;
|
||||
XMLInputFactory factory = XMLInputFactory.newInstance();
|
||||
this.in = factory.createXMLEventReader(new FileInputStream(file), "UTF-8");
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param target
|
||||
* @throws XMLStreamException
|
||||
*/
|
||||
public XMLReader(final InputStream source) throws XMLStreamException
|
||||
{
|
||||
this.nextEvent = null;
|
||||
XMLInputFactory factory = XMLInputFactory.newInstance();
|
||||
this.in = factory.createXMLEventReader(source);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param target
|
||||
* @throws XMLStreamException
|
||||
*/
|
||||
public XMLReader(final Reader source) throws XMLStreamException
|
||||
{
|
||||
this.nextEvent = null;
|
||||
XMLInputFactory factory = XMLInputFactory.newInstance();
|
||||
this.in = factory.createXMLEventReader(source);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws XMLStreamException
|
||||
*/
|
||||
public void close() throws XMLStreamException
|
||||
{
|
||||
if (this.in != null)
|
||||
{
|
||||
this.in.close();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This methods does a premonition act. Useful to detect end of a list.
|
||||
*
|
||||
* @param label
|
||||
* @return
|
||||
* @throws XMLStreamException
|
||||
*/
|
||||
public boolean hasNextStartTag(final String label) throws XMLStreamException
|
||||
{
|
||||
boolean result;
|
||||
|
||||
// Load next event.
|
||||
if (this.nextEvent == null)
|
||||
{
|
||||
if (this.in.hasNext())
|
||||
{
|
||||
this.nextEvent = this.in.nextEvent();
|
||||
}
|
||||
}
|
||||
|
||||
// Analyze next event.
|
||||
if (this.nextEvent == null)
|
||||
{
|
||||
result = false;
|
||||
}
|
||||
else if ((this.nextEvent.isStartElement()) && (StringUtils.equals(this.nextEvent.asStartElement().getName().getLocalPart(), label)))
|
||||
{
|
||||
result = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
result = false;
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param label
|
||||
* @return
|
||||
* @throws XMLBadFormatException
|
||||
* @throws XMLStreamException
|
||||
*/
|
||||
public XMLTag readContentTag(final String label) throws XMLBadFormatException, XMLStreamException
|
||||
{
|
||||
XMLTag result;
|
||||
|
||||
//
|
||||
result = readTag();
|
||||
|
||||
//
|
||||
if (result == null)
|
||||
{
|
||||
throw new XMLBadFormatException("XML file ends prematurely, content tag [" + label + "] is expected.");
|
||||
}
|
||||
else if (result.getType() != TagType.CONTENT)
|
||||
{
|
||||
throw new XMLBadFormatException("Content tag [" + label + "] is missing.");
|
||||
}
|
||||
else if (!StringUtils.equals(label, result.getLabel()))
|
||||
{
|
||||
throw new XMLBadFormatException("Tag with label [" + label + "] is missing.");
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param label
|
||||
* @return
|
||||
* @throws XMLStreamException
|
||||
* @throws XMLBadFormatException
|
||||
*/
|
||||
public XMLTag readEndTag(final String label) throws XMLStreamException, XMLBadFormatException
|
||||
{
|
||||
XMLTag result;
|
||||
|
||||
//
|
||||
result = readTag();
|
||||
|
||||
//
|
||||
if (result == null)
|
||||
{
|
||||
throw new XMLBadFormatException("XML file ends prematurely, end tag [" + label + "] is expected.");
|
||||
}
|
||||
else if (result.getType() != TagType.END)
|
||||
{
|
||||
throw new XMLBadFormatException("End tag [" + label + "] is missing.");
|
||||
}
|
||||
else if (!StringUtils.equals(result.getLabel(), label))
|
||||
{
|
||||
throw new XMLBadFormatException("Tag with label [" + label + "] is missing.");
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param label
|
||||
* @return
|
||||
* @throws XMLStreamException
|
||||
* @throws XMLBadFormatException
|
||||
* @throws Exception
|
||||
*/
|
||||
public XMLTag readListTag(final String label) throws XMLStreamException, XMLBadFormatException
|
||||
{
|
||||
XMLTag result;
|
||||
|
||||
//
|
||||
result = readTag();
|
||||
|
||||
//
|
||||
if (result == null)
|
||||
{
|
||||
throw new XMLBadFormatException("XML file ends prematurely, tag [" + label + "] is expected.");
|
||||
}
|
||||
else if ((result.getType() != TagType.START) && (result.getType() != TagType.EMPTY))
|
||||
{
|
||||
throw new XMLBadFormatException("List tag [" + label + "] is missing.");
|
||||
}
|
||||
else if (!StringUtils.equals(label, result.getLabel()))
|
||||
{
|
||||
throw new XMLBadFormatException("Tag with label [" + label + "] is missing.");
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param label
|
||||
* @return
|
||||
* @throws XMLStreamException
|
||||
* @throws XMLBadFormatException
|
||||
* @throws Exception
|
||||
*/
|
||||
public XMLTag readNullableContentTag(final String label) throws XMLStreamException, XMLBadFormatException
|
||||
{
|
||||
XMLTag result;
|
||||
|
||||
//
|
||||
result = readTag();
|
||||
|
||||
//
|
||||
if (result == null)
|
||||
{
|
||||
throw new XMLBadFormatException("XML file ends prematurely, tag [" + label + "] is expected.");
|
||||
}
|
||||
else if (!StringUtils.equals(label, result.getLabel()))
|
||||
{
|
||||
throw new XMLBadFormatException("Nullable content tag [" + label + "] is missing.");
|
||||
}
|
||||
else if ((result.getType() != TagType.EMPTY) && (result.getType() != TagType.CONTENT))
|
||||
{
|
||||
throw new XMLBadFormatException("Nullable content tag [" + label + "] is missing.");
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param label
|
||||
* @return
|
||||
* @throws XMLStreamException
|
||||
* @throws XMLBadFormatException
|
||||
* @throws Exception
|
||||
*/
|
||||
public XMLTag readNullableStartTag(final String label) throws XMLStreamException, XMLBadFormatException
|
||||
{
|
||||
XMLTag result;
|
||||
|
||||
//
|
||||
result = readTag();
|
||||
|
||||
//
|
||||
if (result == null)
|
||||
{
|
||||
throw new XMLBadFormatException("XML file ends prematurely, start tag [" + label + "] is expected.");
|
||||
}
|
||||
else if ((result.getType() != TagType.START) && (result.getType() != TagType.EMPTY))
|
||||
{
|
||||
throw new XMLBadFormatException("Start tag [" + label + "] is missing.");
|
||||
}
|
||||
else if (!StringUtils.equals(result.getLabel(), label))
|
||||
{
|
||||
throw new XMLBadFormatException("Tag with label [" + label + "] is missing.");
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param label
|
||||
* @return
|
||||
* @throws XMLStreamException
|
||||
* @throws XMLBadFormatException
|
||||
* @throws Exception
|
||||
*/
|
||||
public XMLTag readStartTag(final String label) throws XMLStreamException, XMLBadFormatException
|
||||
{
|
||||
XMLTag result;
|
||||
|
||||
//
|
||||
result = readTag();
|
||||
|
||||
//
|
||||
if (result == null)
|
||||
{
|
||||
throw new XMLBadFormatException("XML file ends prematurely, start tag [" + label + "] is expected.");
|
||||
}
|
||||
else if (result.getType() != TagType.START)
|
||||
{
|
||||
throw new XMLBadFormatException("Start tag [" + label + "] is missing.");
|
||||
}
|
||||
else if (!StringUtils.equals(result.getLabel(), label))
|
||||
{
|
||||
throw new XMLBadFormatException("Tag with label [" + label + "] is missing.");
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Transducer graph :
|
||||
* <ul>
|
||||
* <li>START_DOCUMENT => HEADER TAG
|
||||
* <li>START_ELEMENT(X) + START_ELEMENT(Y) => <X><Y> => START TAG
|
||||
* <li>START_ELEMENT(X) + CHARACTERS(C) + START_ELEMENT(Y) => <X>SPACES<Y>=>
|
||||
* START TAG
|
||||
* <li>START_ELEMENT(X) + CHARACTERS(C) + END_ELEMENT(X) => <X>C</X> =>
|
||||
* CONTENT TAG
|
||||
* <li>START_ELEMENT(X) + END_ELEMENT(X) => <X></X> => <X/> => EMPTY
|
||||
* <li>END_ELEMENT(X) => </X> => END TAG
|
||||
* <li>END_DOCUMENT => FOOTER TAG
|
||||
* </ul>
|
||||
*
|
||||
* @throws XMLStreamException
|
||||
* @throws XMLBadFormatException
|
||||
*
|
||||
*/
|
||||
public XMLTag readTag() throws XMLStreamException, XMLBadFormatException
|
||||
{
|
||||
XMLTag result;
|
||||
|
||||
int level = 1;
|
||||
boolean ended = false;
|
||||
result = null;
|
||||
XMLAttributes attributesBuffer = null;
|
||||
QName nameBuffer = null;
|
||||
StringList contentBuffer = null;
|
||||
while (!ended)
|
||||
{
|
||||
//
|
||||
XMLEvent event;
|
||||
if (this.nextEvent != null)
|
||||
{
|
||||
event = this.nextEvent;
|
||||
this.nextEvent = null;
|
||||
}
|
||||
else if (this.in.hasNext())
|
||||
{
|
||||
event = this.in.nextEvent();
|
||||
}
|
||||
else
|
||||
{
|
||||
event = null;
|
||||
}
|
||||
|
||||
if (event == null)
|
||||
{
|
||||
ended = true;
|
||||
result = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
logger.debug("eventType=" + XMLTools.toString(event));
|
||||
switch (level)
|
||||
{
|
||||
case 1:
|
||||
switch (event.getEventType())
|
||||
{
|
||||
case XMLEvent.START_DOCUMENT:
|
||||
// START_DOCUMENT => START DOCUMENT TAG
|
||||
ended = true;
|
||||
result = new XMLTag(null, TagType.HEADER, null);
|
||||
break;
|
||||
case XMLEvent.START_ELEMENT:
|
||||
// START_ELEMENT(X) => ...
|
||||
nameBuffer = event.asStartElement().getName();
|
||||
attributesBuffer = new XMLAttributes(event.asStartElement().getAttributes());
|
||||
level += 1;
|
||||
break;
|
||||
case XMLEvent.END_ELEMENT:
|
||||
// END_ELEMENT(X) => </X> => END TAG
|
||||
ended = true;
|
||||
result = new XMLTag(event.asEndElement().getName(), TagType.END, null);
|
||||
break;
|
||||
case XMLEvent.END_DOCUMENT:
|
||||
// END_DOCUMENT => END DOCUMENT TAG
|
||||
ended = true;
|
||||
result = new XMLTag(null, TagType.FOOTER, null);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case 2:
|
||||
switch (event.getEventType())
|
||||
{
|
||||
case XMLEvent.START_ELEMENT:
|
||||
// START_ELEMENT(X) + START_ELEMENT(Y) => <X><Y>
|
||||
// => START TAG
|
||||
ended = true;
|
||||
result = new XMLTag(nameBuffer, TagType.START, attributesBuffer);
|
||||
this.nextEvent = event;
|
||||
break;
|
||||
case XMLEvent.CHARACTERS:
|
||||
// START_ELEMENT(X) + CHARACTERS(C) => ...
|
||||
contentBuffer = new StringList(50);
|
||||
contentBuffer.append(event.asCharacters().getData());
|
||||
level += 1;
|
||||
break;
|
||||
case XMLEvent.END_ELEMENT:
|
||||
// START_ELEMENT(X) + END_ELEMENT(X) => <X></X>
|
||||
// => <X/> => EMPTY
|
||||
ended = true;
|
||||
result = new XMLTag(nameBuffer, TagType.EMPTY, attributesBuffer);
|
||||
break;
|
||||
default:
|
||||
throw new XMLBadFormatException("Unexpected XMLEvent [" + event.getEventType() + "].");
|
||||
}
|
||||
break;
|
||||
case 3:
|
||||
switch (event.getEventType())
|
||||
{
|
||||
case XMLEvent.START_ELEMENT:
|
||||
// START_ELEMENT(X) + CHARACTERS(C) +
|
||||
// START_ELEMENT(Y) =>
|
||||
// <X>SPACES<Y> => START TAG
|
||||
ended = true;
|
||||
result = new XMLTag(nameBuffer, TagType.START, attributesBuffer);
|
||||
this.nextEvent = event;
|
||||
break;
|
||||
case XMLEvent.CHARACTERS:
|
||||
// START_ELEMENT(X) + CHARACTERS(C1) +
|
||||
// CHARACTERS(C2)=> ...
|
||||
contentBuffer.append(event.asCharacters().getData());
|
||||
break;
|
||||
case XMLEvent.END_ELEMENT:
|
||||
// START_ELEMENT(X) + CHARACTERS(C) +
|
||||
// END_ELEMENT(X) => <X>C</X>
|
||||
// => CONTENT TAG
|
||||
ended = true;
|
||||
result = new XMLTag(nameBuffer, TagType.CONTENT, attributesBuffer);
|
||||
result.setContent(contentBuffer.toString());
|
||||
break;
|
||||
default:
|
||||
throw new XMLBadFormatException("Unexpected XMLEvent [" + event.getEventType() + "].");
|
||||
}
|
||||
break;
|
||||
default:
|
||||
throw new XMLBadFormatException("Unexpected level [" + level + "].");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
logger.debug("=> " + XMLTools.toString(result));
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param label
|
||||
* @return
|
||||
* @throws XMLStreamException
|
||||
* @throws XMLBadFormatException
|
||||
*/
|
||||
public XMLTag readXMLFooter() throws XMLStreamException, XMLBadFormatException
|
||||
{
|
||||
XMLTag result;
|
||||
|
||||
//
|
||||
result = readTag();
|
||||
|
||||
//
|
||||
if (result == null)
|
||||
{
|
||||
throw new XMLBadFormatException("XML file ends prematurely, end document event is expected.");
|
||||
}
|
||||
else if (result.getType() != TagType.FOOTER)
|
||||
{
|
||||
throw new XMLBadFormatException("End document tag is missing.");
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
* @throws XMLException
|
||||
* @throws XMLStreamException
|
||||
* @throws XMLBadFormatException
|
||||
*/
|
||||
public XMLTag readXMLHeader() throws XMLStreamException, XMLBadFormatException
|
||||
{
|
||||
XMLTag result;
|
||||
|
||||
//
|
||||
result = readTag();
|
||||
|
||||
//
|
||||
if (result == null)
|
||||
{
|
||||
throw new XMLBadFormatException("XML file ends prematurely, start document event is expected.");
|
||||
}
|
||||
else if (result.getType() != TagType.HEADER)
|
||||
{
|
||||
throw new XMLBadFormatException("XML header is missing.");
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param args
|
||||
* @throws Exception
|
||||
*/
|
||||
public static void main(final String args[]) throws Exception
|
||||
{
|
||||
|
||||
XMLInputFactory factory = XMLInputFactory.newInstance();
|
||||
XMLEventReader in = factory.createXMLEventReader(new FileReader("/home/cpm/C/Puck/Dev/Puck/test/TT/t3.puc"));
|
||||
|
||||
XMLEvent event;
|
||||
while (in.hasNext())
|
||||
{
|
||||
event = in.nextEvent();
|
||||
|
||||
switch (event.getEventType())
|
||||
{
|
||||
case XMLEvent.ATTRIBUTE:
|
||||
System.out.println("ATTRIBUTE ");
|
||||
break;
|
||||
case XMLEvent.CDATA:
|
||||
System.out.println("CDATA");
|
||||
break;
|
||||
case XMLEvent.CHARACTERS:
|
||||
System.out.println("CHARACTERS [" + event.asCharacters().getData() + "]");
|
||||
break;
|
||||
case XMLEvent.COMMENT:
|
||||
System.out.println("COMMENT");
|
||||
break;
|
||||
case XMLEvent.DTD:
|
||||
System.out.println("DTD");
|
||||
break;
|
||||
case XMLEvent.END_DOCUMENT:
|
||||
System.out.println("END_DOCUMENT");
|
||||
break;
|
||||
case XMLEvent.END_ELEMENT:
|
||||
System.out.println("END_ELEMENT " + event.asEndElement().getName());
|
||||
break;
|
||||
case XMLEvent.ENTITY_DECLARATION:
|
||||
System.out.println("ENTITY_DECLARATION");
|
||||
break;
|
||||
case XMLEvent.ENTITY_REFERENCE:
|
||||
System.out.println("ENTITY_REFERENCE");
|
||||
break;
|
||||
case XMLEvent.NAMESPACE:
|
||||
System.out.println("NAMESPACE");
|
||||
break;
|
||||
case XMLEvent.NOTATION_DECLARATION:
|
||||
System.out.println("NOTATION_DECLARATION");
|
||||
break;
|
||||
case XMLEvent.PROCESSING_INSTRUCTION:
|
||||
System.out.println("PROCESSING_INSTRUCTION");
|
||||
break;
|
||||
case XMLEvent.SPACE:
|
||||
System.out.println("SPACE");
|
||||
break;
|
||||
case XMLEvent.START_DOCUMENT:
|
||||
System.out.println("START_DOCUMENT");
|
||||
break;
|
||||
case XMLEvent.START_ELEMENT:
|
||||
System.out.println("START_ELEMENT [name=" + event.asStartElement().getName() + "][namespaceURI=" + event.asStartElement().getName().getNamespaceURI() + "][prefix="
|
||||
+ event.asStartElement().getName().getPrefix() + "][localPart=" + event.asStartElement().getName().getLocalPart() + "]");
|
||||
break;
|
||||
default:
|
||||
System.out.println("DEFAULT");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
148
src/fr/devinsy/util/xml/XMLTag.java
Normal file
148
src/fr/devinsy/util/xml/XMLTag.java
Normal file
|
@ -0,0 +1,148 @@
|
|||
/**
|
||||
* Copyright (C) 2013-2014 Christian Pierre MOMON
|
||||
*
|
||||
* This file is part of Devinsy-utils.
|
||||
*
|
||||
* Devinsy-utils is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Devinsy-utils is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Devinsy-utils. If not, see <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
package fr.devinsy.util.xml;
|
||||
|
||||
import javax.xml.namespace.QName;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Christian Pierre MOMON (christian.momon@devinsy.fr)
|
||||
*/
|
||||
public class XMLTag
|
||||
{
|
||||
|
||||
public enum TagType
|
||||
{
|
||||
HEADER, START, END, EMPTY, CONTENT, FOOTER
|
||||
}
|
||||
|
||||
private QName name;
|
||||
private TagType type;
|
||||
private XMLAttributes attributes;
|
||||
private String content;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public XMLTag(final QName name, final TagType type, final XMLAttributes attributes)
|
||||
{
|
||||
this.name = name;
|
||||
this.type = type;
|
||||
this.attributes = attributes;
|
||||
this.content = null;
|
||||
}
|
||||
|
||||
public XMLAttributes attributes()
|
||||
{
|
||||
return this.attributes;
|
||||
}
|
||||
|
||||
public String getContent()
|
||||
{
|
||||
return this.content;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String getLabel()
|
||||
{
|
||||
String result;
|
||||
|
||||
if (this.name == null)
|
||||
{
|
||||
result = "";
|
||||
}
|
||||
else
|
||||
{
|
||||
result = this.name.getLocalPart();
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
public QName getName()
|
||||
{
|
||||
return this.name;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String getNamespaceURI()
|
||||
{
|
||||
String result;
|
||||
|
||||
if (this.name == null)
|
||||
{
|
||||
result = "";
|
||||
}
|
||||
else
|
||||
{
|
||||
result = this.name.getNamespaceURI();
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String getPrefix()
|
||||
{
|
||||
String result;
|
||||
|
||||
if (this.name == null)
|
||||
{
|
||||
result = "";
|
||||
}
|
||||
else
|
||||
{
|
||||
result = this.name.getPrefix();
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
public TagType getType()
|
||||
{
|
||||
return this.type;
|
||||
}
|
||||
|
||||
public void setContent(final String content)
|
||||
{
|
||||
this.content = content;
|
||||
}
|
||||
|
||||
public void setName(final QName name)
|
||||
{
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public void setType(final TagType type)
|
||||
{
|
||||
this.type = type;
|
||||
}
|
||||
}
|
389
src/fr/devinsy/util/xml/XMLTools.java
Normal file
389
src/fr/devinsy/util/xml/XMLTools.java
Normal file
|
@ -0,0 +1,389 @@
|
|||
/**
|
||||
* Copyright (C) 2013-2014 Christian Pierre MOMON
|
||||
*
|
||||
* This file is part of Devinsy-utils.
|
||||
*
|
||||
* Devinsy-utils is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Devinsy-utils is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Devinsy-utils. If not, see <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
package fr.devinsy.util.xml;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.zip.ZipInputStream;
|
||||
|
||||
import javax.xml.stream.events.XMLEvent;
|
||||
import javax.xml.transform.sax.SAXSource;
|
||||
import javax.xml.transform.stream.StreamSource;
|
||||
import javax.xml.validation.Schema;
|
||||
import javax.xml.validation.SchemaFactory;
|
||||
import javax.xml.validation.Validator;
|
||||
|
||||
import org.apache.commons.lang3.StringEscapeUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.xml.sax.InputSource;
|
||||
import org.xml.sax.SAXException;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Christian Pierre MOMON (christian.momon@devinsy.fr)
|
||||
*/
|
||||
public class XMLTools
|
||||
{
|
||||
/**
|
||||
*
|
||||
* @param source
|
||||
* @return
|
||||
*/
|
||||
public static String escapeXmlBlank(final String source)
|
||||
{
|
||||
String result;
|
||||
|
||||
if (StringUtils.isBlank(source))
|
||||
{
|
||||
result = "";
|
||||
}
|
||||
else
|
||||
{
|
||||
result = StringEscapeUtils.escapeXml(source);
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @param source
|
||||
* @throws SAXException
|
||||
* @throws IOException
|
||||
* @throws PuckException
|
||||
*/
|
||||
public static boolean isValid(final File xmlFile, final File xsdFile) throws SAXException, IOException
|
||||
{
|
||||
boolean result;
|
||||
|
||||
//
|
||||
InputStream xmlSource;
|
||||
if (isZipFile(xmlFile))
|
||||
{
|
||||
ZipInputStream zin = new ZipInputStream(new FileInputStream(xmlFile));
|
||||
zin.getNextEntry();
|
||||
xmlSource = zin;
|
||||
}
|
||||
else
|
||||
{
|
||||
xmlSource = new FileInputStream(xmlFile);
|
||||
}
|
||||
|
||||
//
|
||||
result = isValid(xmlSource, new FileInputStream(xsdFile));
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @param source
|
||||
* @throws SAXException
|
||||
* @throws IOException
|
||||
* @throws PuckException
|
||||
*/
|
||||
public static boolean isValid(final File xmlFile, final InputStream xsdSource) throws SAXException, IOException
|
||||
{
|
||||
boolean result;
|
||||
|
||||
//
|
||||
InputStream xmlSource;
|
||||
if (isZipFile(xmlFile))
|
||||
{
|
||||
ZipInputStream zin = new ZipInputStream(new FileInputStream(xmlFile));
|
||||
zin.getNextEntry();
|
||||
xmlSource = zin;
|
||||
}
|
||||
else
|
||||
{
|
||||
xmlSource = new FileInputStream(xmlFile);
|
||||
}
|
||||
|
||||
//
|
||||
result = isValid(xmlSource, xsdSource);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @param source
|
||||
* @throws SAXException
|
||||
* @throws IOException
|
||||
* @throws PuckException
|
||||
*/
|
||||
public static boolean isValid(final InputStream xmlSource, final InputStream xsdSource) throws SAXException, IOException
|
||||
{
|
||||
boolean result;
|
||||
|
||||
if (xmlSource == null)
|
||||
{
|
||||
result = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
try
|
||||
{
|
||||
//
|
||||
SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
|
||||
InputSource sourceentree = new InputSource(xsdSource);
|
||||
SAXSource sourceXSD = new SAXSource(sourceentree);
|
||||
Schema schema = factory.newSchema(sourceXSD);
|
||||
Validator validator = schema.newValidator();
|
||||
|
||||
//
|
||||
validator.validate(new StreamSource(xmlSource));
|
||||
result = true;
|
||||
|
||||
}
|
||||
catch (final IllegalArgumentException exception)
|
||||
{
|
||||
exception.printStackTrace();
|
||||
result = false;
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param file
|
||||
* @return
|
||||
* @throws IOException
|
||||
*/
|
||||
public static boolean isZipFile(final File file) throws IOException
|
||||
{
|
||||
boolean result;
|
||||
|
||||
//
|
||||
byte[] buffer = new byte[4];
|
||||
FileInputStream is = null;
|
||||
try
|
||||
{
|
||||
is = new FileInputStream(file);
|
||||
is.read(buffer);
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (is != null)
|
||||
{
|
||||
is.close();
|
||||
}
|
||||
}
|
||||
|
||||
// 50 4B 3 4
|
||||
if ((buffer[0] == 0x50) && (buffer[1] == 0x4B) && (buffer[2] == 0x03) && (buffer[3] == 0x04))
|
||||
{
|
||||
result = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
result = false;
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public static String readTag(final BufferedReader in) throws Exception
|
||||
{
|
||||
String result;
|
||||
|
||||
Pattern TAG_PATTERN = Pattern.compile("^<([\\w-_\\.]+)>.*</([\\w-_\\.]+)>$");
|
||||
Pattern SHORT_TAG_PATTERN = Pattern.compile("^<.+/>$");
|
||||
|
||||
result = in.readLine();
|
||||
boolean ended = false;
|
||||
while (!ended)
|
||||
{
|
||||
/*
|
||||
* DEBUG Matcher tagMatcher2 = TAG_PATTERN.matcher(result); if
|
||||
* (tagMatcher2.find()) { logger.info("group count,0,1,2 = [" +
|
||||
* tagMatcher2.groupCount() + "][" + tagMatcher2.group(0) + "][" +
|
||||
* tagMatcher2.group(1) + "][" + tagMatcher2.group(2) + "]"); }
|
||||
*/
|
||||
|
||||
Matcher tagMatcher = TAG_PATTERN.matcher(result);
|
||||
Matcher shortTagMatcher = SHORT_TAG_PATTERN.matcher(result);
|
||||
|
||||
if ((tagMatcher.find()) && (tagMatcher.groupCount() == 2) && (tagMatcher.group(1).equals(tagMatcher.group(2))))
|
||||
{
|
||||
ended = true;
|
||||
}
|
||||
else if (shortTagMatcher.matches())
|
||||
{
|
||||
ended = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
result += in.readLine();
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param source
|
||||
* @return
|
||||
*/
|
||||
public static String toHTLM5(final String source)
|
||||
{
|
||||
String result;
|
||||
|
||||
if (StringUtils.isBlank(source))
|
||||
{
|
||||
result = "";
|
||||
}
|
||||
else
|
||||
{
|
||||
result = source.replace(" ", " ");
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param value
|
||||
* @return
|
||||
*/
|
||||
public static String toString(final XMLEvent source)
|
||||
{
|
||||
String result;
|
||||
|
||||
switch (source.getEventType())
|
||||
{
|
||||
case XMLEvent.ATTRIBUTE:
|
||||
result = "ATTRIBUTE ";
|
||||
break;
|
||||
case XMLEvent.CDATA:
|
||||
result = "CDATA";
|
||||
break;
|
||||
case XMLEvent.CHARACTERS:
|
||||
result = "CHARACTERS [" + source.asCharacters().getData() + "]";
|
||||
break;
|
||||
case XMLEvent.COMMENT:
|
||||
result = "COMMENT";
|
||||
break;
|
||||
case XMLEvent.DTD:
|
||||
result = "DTD";
|
||||
break;
|
||||
case XMLEvent.END_DOCUMENT:
|
||||
result = "END_DOCUMENT";
|
||||
break;
|
||||
case XMLEvent.END_ELEMENT:
|
||||
result = "END_ELEMENT " + source.asEndElement().getName();
|
||||
break;
|
||||
case XMLEvent.ENTITY_DECLARATION:
|
||||
result = "ENTITY_DECLARATION";
|
||||
break;
|
||||
case XMLEvent.ENTITY_REFERENCE:
|
||||
result = "ENTITY_REFERENCE";
|
||||
break;
|
||||
case XMLEvent.NAMESPACE:
|
||||
result = "NAMESPACE";
|
||||
break;
|
||||
case XMLEvent.NOTATION_DECLARATION:
|
||||
result = "NOTATION_DECLARATION";
|
||||
break;
|
||||
case XMLEvent.PROCESSING_INSTRUCTION:
|
||||
result = "PROCESSING_INSTRUCTION";
|
||||
break;
|
||||
case XMLEvent.SPACE:
|
||||
result = "SPACE";
|
||||
break;
|
||||
case XMLEvent.START_DOCUMENT:
|
||||
result = "START_DOCUMENT";
|
||||
break;
|
||||
case XMLEvent.START_ELEMENT:
|
||||
result = "START_ELEMENT [name=" + source.asStartElement().getName() + "][namespaceURI=" + source.asStartElement().getName().getNamespaceURI() + "][prefix="
|
||||
+ source.asStartElement().getName().getPrefix() + "][localPart=" + source.asStartElement().getName().getLocalPart() + "]";
|
||||
break;
|
||||
default:
|
||||
result = null;
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param event
|
||||
* @return
|
||||
*/
|
||||
public static String toString(final XMLTag source)
|
||||
{
|
||||
String result;
|
||||
|
||||
if (source == null)
|
||||
{
|
||||
result = "null";
|
||||
}
|
||||
else
|
||||
{
|
||||
result = "[label=" + source.getLabel() + "][type=" + source.getType().toString() + "][content=" + source.getContent() + "]";
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param source
|
||||
* @return
|
||||
*/
|
||||
public static String unescapeXmlBlank(final String source)
|
||||
{
|
||||
String result;
|
||||
|
||||
if (StringUtils.isBlank(source))
|
||||
{
|
||||
result = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
result = StringEscapeUtils.unescapeXml(source);
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
}
|
315
src/fr/devinsy/util/xml/XMLWriter.java
Normal file
315
src/fr/devinsy/util/xml/XMLWriter.java
Normal file
|
@ -0,0 +1,315 @@
|
|||
/**
|
||||
* Copyright (C) 2013-2014 Christian Pierre MOMON
|
||||
*
|
||||
* This file is part of Devinsy-utils.
|
||||
*
|
||||
* Devinsy-utils is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Devinsy-utils is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Devinsy-utils. If not, see <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
package fr.devinsy.util.xml;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.PrintWriter;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.io.Writer;
|
||||
|
||||
import org.apache.commons.lang3.ArrayUtils;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Christian Pierre MOMON (christian.momon@devinsy.fr)
|
||||
*/
|
||||
public class XMLWriter
|
||||
{
|
||||
|
||||
protected PrintWriter out;
|
||||
|
||||
/**
|
||||
* Default constructor (useful for extend this class).
|
||||
*/
|
||||
protected XMLWriter()
|
||||
{
|
||||
this.out = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize a XML Writer to a file.
|
||||
*
|
||||
* @param file
|
||||
* Where write the XML data.
|
||||
*
|
||||
* @throws FileNotFoundException
|
||||
* @throws UnsupportedEncodingException
|
||||
*/
|
||||
public XMLWriter(final File file) throws UnsupportedEncodingException, FileNotFoundException
|
||||
{
|
||||
this.out = new PrintWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize a XML Writer to a <code>OutputStream</code>.
|
||||
*
|
||||
* @param target
|
||||
* Where write the XML data.
|
||||
*
|
||||
* @throws UnsupportedEncodingException
|
||||
*/
|
||||
public XMLWriter(final OutputStream target) throws UnsupportedEncodingException
|
||||
{
|
||||
this.out = new PrintWriter(new OutputStreamWriter(target, "UTF-8"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize a XML Writer to a <code>Writer</code>.
|
||||
*
|
||||
* @param target
|
||||
* Where write the XML data.
|
||||
*
|
||||
* @throws UnsupportedEncodingException
|
||||
*/
|
||||
public XMLWriter(final Writer target) throws UnsupportedEncodingException
|
||||
{
|
||||
this.out = new PrintWriter(target);
|
||||
}
|
||||
|
||||
/**
|
||||
* This method closes the target stream.
|
||||
*/
|
||||
public void close() throws IOException
|
||||
{
|
||||
if (this.out != null)
|
||||
{
|
||||
this.out.flush();
|
||||
this.out.close();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This method flushes the target stream.
|
||||
*/
|
||||
public void flush() throws IOException
|
||||
{
|
||||
if (this.out != null)
|
||||
{
|
||||
this.out.flush();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This method writes a XML comment.
|
||||
*
|
||||
* @param comment
|
||||
* The comment to write.
|
||||
*/
|
||||
public void writeComment(final String comment)
|
||||
{
|
||||
this.out.print("<!-- ");
|
||||
if (comment != null)
|
||||
{
|
||||
writeTagContent(comment);
|
||||
}
|
||||
this.out.print(" -->");
|
||||
}
|
||||
|
||||
/**
|
||||
* This method writes a XML tag with no content.
|
||||
*/
|
||||
public void writeEmptyTag(final String label, final String... attributes)
|
||||
{
|
||||
this.out.print("<");
|
||||
this.out.print(label);
|
||||
writeTagAttributes(attributes);
|
||||
this.out.print("/>");
|
||||
}
|
||||
|
||||
/**
|
||||
* This method writes a XML ender tag.
|
||||
*/
|
||||
public void writeEndTag(final String label)
|
||||
{
|
||||
this.out.print("</");
|
||||
this.out.print(label);
|
||||
this.out.print(">");
|
||||
}
|
||||
|
||||
/**
|
||||
* This method writes a XML start tag.
|
||||
*/
|
||||
public void writeStartTag(final String label, final String... attributes)
|
||||
{
|
||||
this.out.print("<");
|
||||
this.out.print(label);
|
||||
writeTagAttributes(attributes);
|
||||
this.out.print(">");
|
||||
}
|
||||
|
||||
/**
|
||||
* This method write a XML tag with attributes and boolean content data.
|
||||
*
|
||||
* @param label
|
||||
* @param content
|
||||
* @param attributes
|
||||
*/
|
||||
public void writeTag(final String label, final boolean content, final String... attributes)
|
||||
{
|
||||
writeStartTag(label, attributes);
|
||||
writeTagContent(String.valueOf(content));
|
||||
writeEndTag(label);
|
||||
}
|
||||
|
||||
/**
|
||||
* This method write a XML tag with attributes and long content data.
|
||||
*
|
||||
* @param label
|
||||
* @param content
|
||||
* @param attributes
|
||||
*/
|
||||
public void writeTag(final String label, final long content, final String... attributes)
|
||||
{
|
||||
writeStartTag(label, attributes);
|
||||
writeTagContent(String.valueOf(content));
|
||||
writeEndTag(label);
|
||||
}
|
||||
|
||||
/**
|
||||
* This method write a XML tag with attributes and content data. Content
|
||||
* data are converted in XML format.
|
||||
*
|
||||
* @param label
|
||||
* @param content
|
||||
* @param attributes
|
||||
*/
|
||||
public void writeTag(final String label, final String content, final String... attributes)
|
||||
{
|
||||
if (content == null)
|
||||
{
|
||||
writeEmptyTag(label, attributes);
|
||||
}
|
||||
else
|
||||
{
|
||||
writeStartTag(label, attributes);
|
||||
writeTagContent(content);
|
||||
writeEndTag(label);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This method writes attributes of a tag.
|
||||
*
|
||||
* @param attributes
|
||||
*/
|
||||
private void writeTagAttributes(final String... attributes)
|
||||
{
|
||||
//
|
||||
if ((attributes != null) && (attributes.length > 0))
|
||||
{
|
||||
for (int count = 0; count < attributes.length; count += 2)
|
||||
{
|
||||
this.out.print(" ");
|
||||
this.out.print(attributes[count]);
|
||||
this.out.print("=\"");
|
||||
this.out.print(attributes[count + 1]);
|
||||
this.out.print("\"");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This method writes content using XML escape.
|
||||
*
|
||||
* @param content
|
||||
* data to write in XML format.
|
||||
*/
|
||||
private void writeTagContent(final String content)
|
||||
{
|
||||
//
|
||||
for (int count = 0; count < content.length(); count++)
|
||||
{
|
||||
//
|
||||
char car = content.charAt(count);
|
||||
|
||||
switch (car)
|
||||
{
|
||||
case '<':
|
||||
this.out.print("<");
|
||||
break;
|
||||
case '>':
|
||||
this.out.print(">");
|
||||
break;
|
||||
case '&':
|
||||
this.out.print("&");
|
||||
break;
|
||||
case '"':
|
||||
this.out.print(""");
|
||||
break;
|
||||
case '\'':
|
||||
this.out.print("'");
|
||||
break;
|
||||
default:
|
||||
this.out.print(car);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This method writes a XML header with attributes.
|
||||
*
|
||||
* @param attributes
|
||||
*/
|
||||
public void writeXMLHeader(final String... attributes)
|
||||
{
|
||||
|
||||
//
|
||||
this.out.print("<?xml");
|
||||
|
||||
//
|
||||
if (!ArrayUtils.contains(attributes, "version"))
|
||||
{
|
||||
this.out.print(" version=\"1.0\"");
|
||||
}
|
||||
|
||||
//
|
||||
if (!ArrayUtils.contains(attributes, "encoding"))
|
||||
{
|
||||
this.out.print(" encoding=\"UTF-8\"");
|
||||
}
|
||||
|
||||
//
|
||||
if (!ArrayUtils.contains(attributes, "encoding"))
|
||||
{
|
||||
this.out.print(" standalone=\"no\"");
|
||||
}
|
||||
|
||||
//
|
||||
if (attributes != null)
|
||||
{
|
||||
//
|
||||
for (int count = 0; count < attributes.length; count += 2)
|
||||
{
|
||||
this.out.print(" ");
|
||||
this.out.print(attributes[count]);
|
||||
this.out.print("=\"");
|
||||
this.out.print(attributes[count + 1]);
|
||||
this.out.print("\"");
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
this.out.print(" ?>");
|
||||
}
|
||||
}
|
68
src/fr/devinsy/util/xml/XMLZipReader.java
Normal file
68
src/fr/devinsy/util/xml/XMLZipReader.java
Normal file
|
@ -0,0 +1,68 @@
|
|||
/**
|
||||
* Copyright (C) 2013-2014 Christian Pierre MOMON
|
||||
*
|
||||
* This file is part of Devinsy-utils.
|
||||
*
|
||||
* Devinsy-utils is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Devinsy-utils is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Devinsy-utils. If not, see <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
package fr.devinsy.util.xml;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.zip.ZipInputStream;
|
||||
|
||||
import javax.xml.stream.XMLInputFactory;
|
||||
import javax.xml.stream.XMLStreamException;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Christian Pierre MOMON (christian.momon@devinsy.fr)
|
||||
*/
|
||||
public class XMLZipReader extends XMLReader
|
||||
{
|
||||
|
||||
/**
|
||||
*
|
||||
* @param file
|
||||
* @throws IOException
|
||||
* @throws XMLStreamException
|
||||
*/
|
||||
public XMLZipReader(final File file) throws IOException, XMLStreamException
|
||||
{
|
||||
super();
|
||||
|
||||
XMLInputFactory factory = XMLInputFactory.newInstance();
|
||||
ZipInputStream zis = new ZipInputStream(new FileInputStream(file));
|
||||
zis.getNextEntry();
|
||||
this.in = factory.createXMLEventReader(zis, "UTF-8");
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param target
|
||||
* @throws IOException
|
||||
* @throws XMLStreamException
|
||||
*/
|
||||
public XMLZipReader(final InputStream source) throws IOException, XMLStreamException
|
||||
{
|
||||
super();
|
||||
|
||||
XMLInputFactory factory = XMLInputFactory.newInstance();
|
||||
ZipInputStream zis = new ZipInputStream(source);
|
||||
zis.getNextEntry();
|
||||
this.in = factory.createXMLEventReader(zis, "UTF-8");
|
||||
}
|
||||
}
|
149
src/fr/devinsy/util/xml/XMLZipWriter.java
Normal file
149
src/fr/devinsy/util/xml/XMLZipWriter.java
Normal file
|
@ -0,0 +1,149 @@
|
|||
/**
|
||||
* Copyright (C) 2013-2014 Christian Pierre MOMON
|
||||
*
|
||||
* This file is part of Devinsy-utils.
|
||||
*
|
||||
* Devinsy-utils is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Devinsy-utils is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Devinsy-utils. If not, see <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
package fr.devinsy.util.xml;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.PrintWriter;
|
||||
import java.util.zip.Deflater;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
|
||||
import fr.devinsy.util.FileTools;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Christian Pierre MOMON (christian.momon@devinsy.fr)
|
||||
*/
|
||||
public class XMLZipWriter extends XMLWriter
|
||||
{
|
||||
private ZipOutputStream zos;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param file
|
||||
* @throws IOException
|
||||
*/
|
||||
public XMLZipWriter(final File file) throws IOException
|
||||
{
|
||||
super();
|
||||
|
||||
this.zos = new ZipOutputStream(new FileOutputStream(file));
|
||||
this.zos.setLevel(Deflater.BEST_COMPRESSION);
|
||||
this.zos.setMethod(ZipOutputStream.DEFLATED);
|
||||
this.zos.setComment("Generated by XMLZipWriter");
|
||||
this.zos.putNextEntry(new ZipEntry(FileTools.setExtension(file, ".xml").getName()));
|
||||
this.out = new PrintWriter(new OutputStreamWriter(this.zos, "UTF-8"));
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param file
|
||||
* @throws IOException
|
||||
*/
|
||||
public XMLZipWriter(final File file, final String generator) throws IOException
|
||||
{
|
||||
super();
|
||||
|
||||
this.zos = new ZipOutputStream(new FileOutputStream(file));
|
||||
this.zos.setLevel(Deflater.BEST_COMPRESSION);
|
||||
this.zos.setMethod(ZipOutputStream.DEFLATED);
|
||||
this.zos.setComment(generator);
|
||||
this.zos.putNextEntry(new ZipEntry(FileTools.setExtension(file, ".xml").getName()));
|
||||
this.out = new PrintWriter(new OutputStreamWriter(this.zos, "UTF-8"));
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param target
|
||||
* @throws IOException
|
||||
*/
|
||||
public XMLZipWriter(final OutputStream target, final String generator) throws IOException
|
||||
{
|
||||
super();
|
||||
this.zos = new ZipOutputStream(target);
|
||||
this.zos.setLevel(Deflater.BEST_COMPRESSION);
|
||||
this.zos.setMethod(ZipOutputStream.DEFLATED);
|
||||
if (generator != null)
|
||||
{
|
||||
this.zos.setComment(generator);
|
||||
}
|
||||
this.out = new PrintWriter(new OutputStreamWriter(this.zos, "UTF-8"));
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param target
|
||||
* @throws IOException
|
||||
*/
|
||||
public XMLZipWriter(final OutputStream target, final String fileName, final String generator) throws IOException
|
||||
{
|
||||
super();
|
||||
this.zos = new ZipOutputStream(target);
|
||||
this.zos.setLevel(Deflater.BEST_COMPRESSION);
|
||||
this.zos.setMethod(ZipOutputStream.DEFLATED);
|
||||
if (generator != null)
|
||||
{
|
||||
this.zos.setComment(generator);
|
||||
}
|
||||
openEntry(fileName);
|
||||
this.out = new PrintWriter(new OutputStreamWriter(this.zos, "UTF-8"));
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws IOException
|
||||
*
|
||||
*/
|
||||
@Override
|
||||
public void close() throws IOException
|
||||
{
|
||||
closeEntry();
|
||||
super.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws IOException
|
||||
*
|
||||
*/
|
||||
public void closeEntry() throws IOException
|
||||
{
|
||||
flush();
|
||||
this.zos.closeEntry();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param fileName
|
||||
* @throws IOException
|
||||
*/
|
||||
public void openEntry(final String fileName) throws IOException
|
||||
{
|
||||
if (fileName == null)
|
||||
{
|
||||
throw new NullPointerException("fileName is null.");
|
||||
}
|
||||
else
|
||||
{
|
||||
this.zos.putNextEntry(new ZipEntry(FileTools.setExtension(fileName, ".xml")));
|
||||
}
|
||||
}
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show more
Loading…
Reference in a new issue