Skip to content

Commit

Permalink
IDE-157 Refactor StringFinder and StringFinderTest to kotlin (#4931)
Browse files Browse the repository at this point in the history
* IDE-157 Refactor StringFinder and StringFinderTest to kotlin

* IDE-157 Refactor findBetween  in StringFinder
  • Loading branch information
coki1405 authored Mar 15, 2024
1 parent 4daad3a commit bf0264e
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 176 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Catroid: An on-device visual programming system for Android devices
* Copyright (C) 2010-2022 The Catrobat Team
* Copyright (C) 2010-2024 The Catrobat Team
* (<http://developer.catrobat.org/credits>)
*
* This program is free software: you can redistribute it and/or modify
Expand Down Expand Up @@ -703,11 +703,13 @@ public static boolean renameProject(File xmlFile, String destinationName) throws
String currentXml = Files.asCharSource(xmlFile, Charsets.UTF_8).read();
StringFinder stringFinder = new StringFinder();

if (!stringFinder.findBetween(currentXml, PROGRAM_NAME_START_TAG, PROGRAM_NAME_END_TAG)) {
String sourceName = stringFinder.findBetween(currentXml, PROGRAM_NAME_START_TAG,
PROGRAM_NAME_END_TAG);

if (sourceName == null) {
return false;
}

String sourceName = stringFinder.getResult();
destinationName = getXMLEncodedString(destinationName);

if (sourceName.equals(destinationName)) {
Expand Down Expand Up @@ -894,11 +896,7 @@ public static String extractDefaultSceneNameFromXml(File projectDir) {

try {
String xml = Files.asCharSource(xmlFile, Charsets.UTF_8).read();
if (!stringFinder.findBetween(xml, "<scenes>\\s*<scene>\\s*<name>", "</name>")) {
return null;
} else {
return stringFinder.getResult();
}
return stringFinder.findBetween(xml, "<scenes>\\s*<scene>\\s*<name>", "</name>");
} catch (IOException e) {
Log.e(TAG, Log.getStackTraceString(e));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Catroid: An on-device visual programming system for Android devices
* Copyright (C) 2010-2022 The Catrobat Team
* Copyright (C) 2010-2024 The Catrobat Team
* (<http://developer.catrobat.org/credits>)
*
* This program is free software: you can redistribute it and/or modify
Expand All @@ -20,42 +20,22 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package org.catrobat.catroid.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringFinder {

private boolean matcherRun;
private String result;

public static String encodeSpecialChars(String string) {
return Pattern.quote(string);
}

public boolean findBetween(String string, String start, String end) {
Pattern pattern = Pattern.compile(start + "(.*?)" + end, Pattern.DOTALL);
Matcher matcher = pattern.matcher(string);

matcherRun = true;

if (matcher.find()) {
result = matcher.group(1);
return true;
}

result = null;
return false;
}

public String getResult() {
if (!matcherRun) {
throw new IllegalStateException("You must call findBetween(String string, String start, String end) "
+ "first.");
}
matcherRun = false;
return result;
}
package org.catrobat.catroid.utils

import java.util.regex.Pattern

class StringFinder {
fun findBetween(searchableString: String, start: String, end: String): String? {
val pattern = Pattern.compile("$start(.*?)$end", Pattern.DOTALL)
val matcher = pattern.matcher(searchableString)
if (matcher.find()) {
return matcher.group(1)
}
return null
}

companion object {
@JvmStatic
fun encodeSpecialChars(string: String): String = Pattern.quote(string)
}
}
16 changes: 9 additions & 7 deletions catroid/src/main/java/org/catrobat/catroid/utils/Utils.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Catroid: An on-device visual programming system for Android devices
* Copyright (C) 2010-2022 The Catrobat Team
* Copyright (C) 2010-2024 The Catrobat Team
* (<http://developer.catrobat.org/credits>)
*
* This program is free software: you can redistribute it and/or modify
Expand Down Expand Up @@ -422,22 +422,24 @@ public static boolean isDefaultProject(Project projectToCheck, Context context)

StringFinder stringFinder = new StringFinder();

if (!stringFinder.findBetween(defaultProjectXml, "<scenes>", "</scenes>")) {
String defaultProjectSpriteList = stringFinder.findBetween(defaultProjectXml,
"<scenes>", "</scenes>");

if (defaultProjectSpriteList == null) {
return false;
}

String defaultProjectSpriteList = stringFinder.getResult();

saveProjectSerial(projectToCheck, context);

String projectToCheckXML = XstreamSerializer.getInstance().getXmlAsStringFromProject(projectToCheck);

if (!stringFinder.findBetween(projectToCheckXML, "<scenes>", "</scenes")) {
String projectToCheckSpriteList = stringFinder.findBetween(projectToCheckXML,
"<scenes>", "</scenes");

if (projectToCheckSpriteList == null) {
return false;
}

String projectToCheckSpriteList = stringFinder.getResult();

String scriptIdRegex = "((?s)<scriptId>.*?</scriptId>)";
String brickIdRegex = "(?s)<brickId>.*?</brickId>";
String scriptIdReplacement = "<scriptId></scriptId>";
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* Catroid: An on-device visual programming system for Android devices
* Copyright (C) 2010-2024 The Catrobat Team
* (<http://developer.catrobat.org/credits>)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* An additional term exception under section 7 of the GNU Affero
* General Public License, version 3, is available at
* http://developer.catrobat.org/license_additional_term
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.catrobat.catroid.test.utiltests

import org.catrobat.catroid.utils.StringFinder
import org.catrobat.catroid.utils.StringFinder.Companion.encodeSpecialChars
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNull
import org.junit.Rule
import org.junit.Test
import org.junit.rules.ExpectedException
import org.junit.runner.RunWith
import org.junit.runners.JUnit4

@RunWith(JUnit4::class)
class StringFinderTest {
@Rule
@JvmField
val exception: ExpectedException = ExpectedException.none()

private val singleLine =
"Mos eisley spaceport. You will never find a more wretched hive of scum and villainy."
private val multiLine =
"""
I must not fear.
Fear is the mind-killer.
Fear is the little-death that brings total obliteration.
I will face my fear.
I will permit it to pass over me and through me.
And when it has gone past I will turn the inner eye to see its path.
Where the fear has gone there will be nothing. Only I will remain.
""".trimIndent()

@Test
fun testMatchBetween() {
val start = "find"
val end = "of"

val stringFinder = StringFinder()
assertEquals(" a more wretched hive ", stringFinder.findBetween(singleLine, start, end))
}

@Test
fun testMatchWithNewLineChars() {
val start = "fear.\n"
val end = "\nFear is the little-death that brings total obliteration."

val stringFinder = StringFinder()
assertEquals("Fear is the mind-killer.", stringFinder.findBetween(multiLine, start, end))
}

@Test
fun testMultipleStartStringMatches() {
val start = "Fear"
val end = "I will face my fear."

val stringFinder = StringFinder()
assertEquals(
"""
| is the mind-killer.
|Fear is the little-death that brings total obliteration.
|
""".trimMargin(), stringFinder.findBetween(multiLine, start, end)
)
}

@Test
fun testMultipleEndStringMatches() {
val start = "find"
val end = encodeSpecialChars(".")

val stringFinder = StringFinder()
assertEquals(
" a more wretched hive of scum and villainy",
stringFinder.findBetween(singleLine, start, end)
)
}

@Test
fun testNoMatchForEnd() {
val start = "find"
val end = "I won't be found"

val stringFinder = StringFinder()
assertNull(stringFinder.findBetween(singleLine, start, end))
}
}

0 comments on commit bf0264e

Please sign in to comment.