-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(#106) Refactored and fixed reading of Flow Yaml ArraySequence of text
- Loading branch information
Showing
4 changed files
with
202 additions
and
32 deletions.
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
src/main/java/org/fusionsoft/lib/text/IterableOfRegexpMatches.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Copyright (C) 2018-2021 FusionSoft | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* You may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, | ||
* either express or implied. | ||
* | ||
* See the License for the specific language governing permissions | ||
* and limitations under the License. | ||
*/ | ||
package org.fusionsoft.lib.text; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
import org.cactoos.Func; | ||
import org.cactoos.Scalar; | ||
import org.cactoos.Text; | ||
import org.cactoos.iterable.IterableEnvelope; | ||
import org.cactoos.iterable.IterableOf; | ||
import org.cactoos.text.TextOfString; | ||
|
||
public class IterableOfRegexpMatches extends IterableEnvelope<Text> { | ||
|
||
public IterableOfRegexpMatches( | ||
final Scalar<Pattern> pattern, | ||
final Func<Matcher, String> group, | ||
final Text origin | ||
) { | ||
super( | ||
new IterableOf<>( | ||
() -> { | ||
final List<Text> matches = new ArrayList<>(); | ||
final Matcher matcher = pattern.value().matcher(origin.asString()); | ||
while (matcher.find()) { | ||
matches.add(new TextOfString(group.apply(matcher))); | ||
} | ||
return matches.iterator(); | ||
} | ||
) | ||
); | ||
} | ||
|
||
public IterableOfRegexpMatches( | ||
final Text regexp, | ||
final Func<Matcher, String> group, | ||
final Text origin | ||
) { | ||
this( | ||
() -> Pattern.compile(regexp.asString()), | ||
group, | ||
origin | ||
); | ||
} | ||
|
||
} |
52 changes: 52 additions & 0 deletions
52
src/main/java/org/fusionsoft/lib/yaml/artefacts/RegexpOfFlowYamlSequenceValues.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Copyright (C) 2018-2021 FusionSoft | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* You may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, | ||
* either express or implied. | ||
* | ||
* See the License for the specific language governing permissions | ||
* and limitations under the License. | ||
*/ | ||
package org.fusionsoft.lib.yaml.artefacts; | ||
|
||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
import org.cactoos.scalar.Sticky; | ||
import org.cactoos.scalar.Ternary; | ||
import org.cactoos.scalar.Unchecked; | ||
|
||
public class RegexpOfFlowYamlSequenceValues { | ||
|
||
private final Unchecked<Pattern> scalar; | ||
|
||
public RegexpOfFlowYamlSequenceValues() { | ||
this.scalar = new Unchecked<>( | ||
new Sticky<>( | ||
() -> Pattern.compile( | ||
"(?:\\s*(?:\"((?>[^\"]|[\"]{2})*)\"|([^,]+))\\s*,?|(?<=,),?)+?" | ||
) | ||
) | ||
); | ||
} | ||
|
||
public final Pattern pattern() { | ||
return scalar.value(); | ||
} | ||
|
||
public final String extract(final Matcher matcher) { | ||
return new Unchecked<>( | ||
new Ternary<>( | ||
() -> matcher.group(1) == null, | ||
() -> matcher.group(2), | ||
() -> matcher.group(1) | ||
) | ||
).value(); | ||
} | ||
|
||
} |
53 changes: 53 additions & 0 deletions
53
src/main/java/org/fusionsoft/lib/yaml/artefacts/TextIterableOfFlowYamlSequence.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Copyright (C) 2018-2021 FusionSoft | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* You may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, | ||
* either express or implied. | ||
* | ||
* See the License for the specific language governing permissions | ||
* and limitations under the License. | ||
*/ | ||
package org.fusionsoft.lib.yaml.artefacts; | ||
|
||
import com.amihaiemil.eoyaml.YamlNode; | ||
import org.cactoos.Text; | ||
import org.cactoos.iterable.IterableEnvelope; | ||
import org.cactoos.iterable.Mapped; | ||
import org.cactoos.text.Replaced; | ||
import org.cactoos.text.Sub; | ||
import org.fusionsoft.lib.text.IterableOfRegexpMatches; | ||
|
||
public class TextIterableOfFlowYamlSequence extends IterableEnvelope<Text> { | ||
|
||
public TextIterableOfFlowYamlSequence(final Text text, final RegexpOfFlowYamlSequenceValues regexp) { | ||
super( | ||
new Mapped<Text>( | ||
x -> new Replaced(x, "\"\"", "\""), | ||
new IterableOfRegexpMatches( | ||
regexp::pattern, | ||
regexp::extract, | ||
new Sub( | ||
text, | ||
1, | ||
() -> text.asString().length() - 1 | ||
) | ||
) | ||
) | ||
); | ||
} | ||
|
||
public TextIterableOfFlowYamlSequence(final Text text) { | ||
this(text, new RegexpOfFlowYamlSequenceValues()); | ||
} | ||
|
||
public TextIterableOfFlowYamlSequence(final YamlNode node) { | ||
this(new TextOfScalarNode(node)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters