Take array element text before element containing specific text - java

I'm getting specific project name based on the folder root. Using the code:
private String getProjectName(Scenario scenario) {
List<String> pathArray = Arrays.asList(Path.of(scenario.getUri()).toUri().getPath().split("/"));
return pathArray.get(pathArray.size() - 1);
}
For such structure as it's shown on below screenshot it works. However, when more then one folder before .feature (number 9) file name will be added then it will fail.
My idea is to:
Get the index of an array which contains text: "features"
Move to the next index (project name) and get the proper text + return it
Is it good idea?

I updated your method to return the split value after "features".
private String getProjectName(Scenario scenario) {
String desiredString = "features";
String[] pathArray = Path.of(scenario.getUri()).toUri().getPath().split("/");
for (int i = 0; i < pathArray.length; i++) {
if (pathArray[i].equals(desiredString)) {
return (pathArray[i + 1]);
}
}
throw new IllegalArgumentException(String.format("The url provided does not contain <%s>", desiredString));
}

As you do not know how nested your folder structure is, you need some pattern to recognize the element that you know will exist in the project structure, in your example with "features" text, and then move to the next index. So your idea looks fine to me.

Related

Iteration is not writing on the right Array Index

I found a code that iterates trough a folder and return the fileNames.
I created a String[] Array with 8000 positions, since I have around 7500 files and getting more.
If I use the code that I found with system.out.println it returns every single file name, but if I use my code to write the position into the Array index, I only get 1 position and rest full of null.
Unlucky I cannot figure out the problem.
import java.io.*;
public class Tester {
public static void main(String[] args) {
File folder = new File("mypath\\myfilefolder");
File[] listOfFiles = folder.listFiles();
String[] fileNames = new String[8000];
for (int i = 0; i < listOfFiles.length; i++) {
int c = 0;
if (listOfFiles[i].isFile()) {
fileNames[c++] = listOfFiles[i].getName();
// Seems not to be doing anything like supposed
// System.out.println(listOfFiles[i].getName());
// Prints every filename on a new line
} else if (listOfFiles[i].isDirectory()) {
System.out.println("Directory " + listOfFiles[i].getName());
}
}
for (String element: fileNames) {
System.out.println(element);
}
}
}
I expect to be able to iterate trough fileNames and get the filesNames so I can work with it.
But actually only the first positions get changed, all the others are still null.
The value changed at 'c++' is never used. Every time before your code execute to c++ the variable of c being reset to 0.
And this would be the same algorithm with the use of the stream API (Java 1.8 and above):
String[] fileNames = Arrays.stream(folder.listFiles()) // stream all the files in the directory
.filter(file -> !file.isDirectory()) // filter any directory files in there
.map(File::getName) // map an item to its name
.toArray(String[]::new); // collect as a String array

Find occurrences of a specific tag in an XML file in java using recursion

I need to return the number of occurrences of the given tag, for example, a user will provide a link to an xml file and the name of the tag to find and it will return the number of occurrences of that specific tag. My code so far only works for the child of the parent node, whereas I need to check all the child of the child nodes as well, and I quite don't understand how to iterate through all of the elements of the xml file.
Modify your code to make use of recursion properly. You need to ALWAYS recurse, not only if a tag has the name you are looking for, because the children still might have the name you are looking for. Also, you need to add the result of the recursive call to the sum. Something like this:
private static int tagCount(XMLTree xml, String tag) {
assert xml != null : "Violation of: xml is not null";
assert tag != null : "Violation of: tag is not null";
int count = 0;
if (xml.isTag()) {
for (int i = 0; i < xml.numberOfChildren(); i++) {
if (xml.child(i).label().equals(tag)) {
count++;
}
count = count + tagCount(xml.child(i), tag);
}
}
return count;
}

is there a difference between searching in txt files and searching in html file in java-android

[]this is a brief explanation of my program:
1- I made a listview of drug names(+8000 name)
2-Every name has a unique html document in assets folder
3- I made a search bar that search in the listview
[]problem: i wanted to imoprove the search function so that it can search inside every html file .
and this is my code :
// Filter Class to search in titles and inside html files
public void filter(String charText) throws FileNotFoundException {
charText = charText.toLowerCase(Locale.getDefault());
druglist.clear();
if (charText.length() == 0) {
druglist.addAll(arraylist);
} else {
int i=-1;
for (drugPopulation wp : arraylist) {
i++;
if (wp.getitem().toLowerCase(Locale.getDefault()).contains(charText)||Searchfor(charText, arraylisthtml.get(i).getitem())) {
druglist.add(wp);
}
}
}
notifyDataSetChanged();
}
*and this is my searchfor function :
private boolean Searchfor(String search, String s ) throws FileNotFoundException {
String path = "file:///android_asset/"+s;
Boolean yes=false;
final Scanner scanner = new Scanner(path);
while (scanner.hasNextLine()) {
final String lineFromFile = scanner.nextLine();
if(lineFromFile.contains(search)) {
// a match!
yes=true;
break;
}
}
return yes;
}
[*]Results and questions :
1-When i run the app and try this search utility it doesn't return true results(i think the searchfor function always returns false ) ,is there a difference between searching in html files and searching in txt files and what can be the cause of these wrong results ?
2-the search is extremely slow , is there a way to improve it ?
Thanks in advance .
I can see that you only check if the file contains the certain word String search in this case, so you don't actually have to read the file line by line, you can just read it as a single string and then call .contains(search) on it, this should make the search a lot faster
Also:
Searchfor(charText, arraylisthtml.get(i).getitem())
Instead of Searchfor(charText, arraylisthtml.get(i).getitem()) you can just call Searchfor(charText, wp.getitem()) and get rid of the i.
But most of all, you should probably "preprocess" your data, by that I mean that if you have a way to group and order the files your are going to search trough, do so, and then search them with an optimized algorithm , i.e. alphabetical order - binary search.
You should really try doing that if you want to improve the speed of your search. Good luck! hope I was helpful.

Java String.split() and compare

Im working with flying saucer and want to export an xhtml to an pdf.
Everything works fine, but now I want to add an empty column, for example for descriptions or something.
I want to create a method addColumn(). which should add in every row of the table at the end a new, empty cell.
I tried following code:
String[] arr = content.split("<td");
String test = "";
for (int i = 0; i < arr.length; i++) {
if(i != 0){
arr[i] = "<td" + arr[i];
test += arr[i];
}
}
This should split the content on every beginning "td" tag.
String.split("<td") removes the "<td" from the content so i want to add it again.
But if i compare those:
if(test.equalsIgnoreCase(content)){
System.out.println("SUCCESS");
}
else{
System.out.println("FAIL");
}
I always fail.
Just help me to get the right content back out of the array, this would make me go a step in the right direction!
Thank you.
Try to replace your split line with this:
String[] arr = content.split("<td", -1);
Otherwise you will loose some input in arr, see the split(String) API doc:
This method works as if by invoking the two-argument split method with the given expression and a limit argument of zero. Trailing empty strings are therefore not included in the resulting array.
The added -1 makes sure that your content can also contain "<tr" at its beginning, for example. See the split(String, int) API doc for further explanations.

How can I determine if a HTML document is well formed or not in JAVA?

Heyy guys, I need to determine if a given HTML Document is well formed or not.
I just need a simple implementation using only Java core API classes i.e. no third party stuff like JTIDY or something. Thanks.
Actually, what is exactly needed is an algorithm that scans a list of TAGS. If it finds an open tag, and the next tag isn't its corresponding close tag, then it should be another open tag which in turn should have its close tag as the next tag, and if not it should be another open tag and then its corresponding close tag next, and the close tags of the previous open tags in reverse order coming next on the list. I've already written methods to convert a tag to a close tag. If the list conforms to this order then it returns true or else false.
Here is the skeleton code of what I've started working on already. Its not too neat, but it should give you guys a basic idea of what I'm trying to do.
public boolean validateHtml(){
ArrayList<String> tags = fetchTags();
//fetchTags returns this [<html>, <head>, <title>, </title>, </head>, <body>, <h1>, </h1>, </body>, </html>]
//I create another ArrayList to store tags that I haven't found its corresponding close tag yet
ArrayList<String> unclosedTags = new ArrayList<String>();
String temp;
for (int i = 0; i < tags.size(); i++) {
temp = tags.get(i);
if(!tags.get(i+1).equals(TagOperations.convertToCloseTag(tags.get(i)))){
unclosedTags.add(tags.get(i));
if(){
}
}else{
return true;//well formed html
}
}
return true;
}
Yeah string manipulation can seem like a pickle sometimes,
you need to do something like
First copy html into an array
bool tag = false;
string str = "";
List<string> htmlTags = new List();
for(int i = 0; i < array.length; i++)
{
//Check for the start of a tag
if(array[i] == '<')
{
tag == true;
}
//If the current char is part of a tag start copying
if(tag)
{
str += char;
}
//When a tag ends add the tag to your tag list
if(array[i] == '>')
{
htmlTags.Add(str);
str = "";
tag == false;
}
}
Something like this should get you started, you should end up with an array of tags, this is only pseudo code so it wont shouldn't compile
Don't think you can do this without undertaking a huge amount of work, would be much easier to use a third party package
Try validating against HTML4 or 4.1 or XHTML 1 DTD
"strict.dtd"
"loose.dtd"
"frameset.dtd"
Which might help !

Categories

Resources