Save Results in a list and give the list out to a csv - list

I work with Tika File Detector. It checks which file Typ my file is.
At the moment my Code is like this
if (Type.endsWith("application/msword")){ //Match if its .doc
}
else if (Type.endsWith("application/vnd.ms-powerpoint")){ //Match if its .ppt
}
else if (Type.endsWith("application/vnd.ms-excel")){ //Match if its .xls
}
else if (Type.endsWith("application/vnd.openxmlformats-officedocument.wordprocessingml.document")){ //Match if its .docx
Now I want to Store the Result in a list, which list has two entries. When I checked all files I want to save the list in a csv file.
I tried this with a hashmap but that didn't work.

You could use parallel arrays. I'm guessing one for the file name and one for the file type, but there is no need to store the info in a temporary data structure if you are just writing to .csv.
If you want to write filename, mime string and extension to a csv, do something like this, where you iterate through your files in main()...
static Tika tika = new Tika();
static MimeTypes mimeTypes = TikaConfig.getDefaultConfig().getMimeRepository();
static void processFile(Path p, Writer writer) throws IOException, MimeTypeException {
String mimeString = tika.detect(p);
MimeType mt = mimeTypes.forName(mimeString);
writer.write(String.format("%s,%s,%s,%n",
p.getFileName(),mimeString,mt.getExtension()));
}
You'll want to add exception handling, and it is always better to use a genuine CSV writer (see Apache Commons csv) than to "hope" than none of your data has a comma/newline or to roll your own.

Related

C# UWP textfile to list of strings

I'm creating a UWP application (latest windows IoT) for my raspberry.
I'm trying to get a list of strings from a .txtfile located in the project folder under a map called Words.
This is my code so far.
public async void GenereerGokWoord(int character)
{
StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(
new Uri("ms-appx:///Words//5-letterwoorden.txt")
);
}
By setting a breakpoint at the end of the } I can confirm that this code can find the .txt file.
But now I don't know how to get a list of strings from this point on.
The .txt file looks like things
word 1
word 2
word 3
If you want contents of file as a List with each line as an Item, Use FileIO.ReadLinesAsync()
IList<string> data = await FileIO.ReadLinesAsync(file);
List<string> finallist = data.ToList();
Your finallist should contain all words as List.

how to save a xml list from response to a csv file in JMeter

one of my tests uses a Loop Controller, CSV Data Set Config and an If Controller to iterate through a csv list to do one request with several parameters defined in an csv file.
I want to change this testcase to use a list of parameters that i get from an GET response in an xml format.
for example using this xml list: http://www.w3schools.com/xml/cd_catalog.xml
now i want to iterate through all < TITLE > values.
i tried to save the response with the 'Save Responses to a file' Listener and then use a BeanShell Listener to read the file and transform it to a csv list which contains only the < TITLE > values. but i'm not sure how to to this transformation part in the BeanShell Listener.
import org.apache.jmeter.services.FileServer;
import org.apache.commons.io.FileUtils;
File xmlFile = new File(FileServer.getFileServer().getBaseDir()+"/resources/data/csv/response1.xml", "UTF-8");
String fileData = FileUtils.readFileToString(xmlFile);
fileData = fileData.replaceAll("<?xml version="1.0" encoding="UTF-8"?>", "");
fileData = fileData.replaceAll("<CATALOG>", "");
//...
FileUtils.writeStringToFile(xmlFile, fileData);
/*
f = new FileOutputStream(FileServer.getFileServer().getBaseDir()+"/resources/data/csv/parameterlist.csv", true);
p = new PrintStream(f);
p.println(fileData);
p.close();
f.close();
*/
there must be some solution using regex, xpath or XSL transformation on all < TITLE > elements or is there an easier way that i didn't thought of?
found a different and easier method that works for my purpose:
Get Request to get the xml list.
add XPath Extractor
Reference Name: 'titles'
XPAth query: '//title'
ForEach Controller
Input variable prefix: 'titles'
Output variable name: 'title'

Get filename in Dynamics Ax

I need to extract a file name from complete path and I see it strange that I when I split the path, I need to iterate through the list to get the file name. Why can't I just get the value simply by calling myList(3) as in DotNet, instead of having to instantiate an iterator, then loop through the records.
Here is my code;
List strlist=new List(Types::String);
strlist = strSplit(CompletePath, #"\");
After doing this I should have a list of all the different parts.
Is there any simple form to read the list, like FileName = strlist[2]; instead of having to do the below;
iterator = new ListIterator(strlist);
while(iterator.more())
{
FileName = iterator.value();
if (_Value == "myFile")
{
_NotFound=boolean::false;
}
Here again, if at that very moment, I don't know the file name, how do I check?
Global::fileNameSplit(fileName)
returns a container [path, file name, extension]
Should be used over the .NET methods recommended by Matej.
Use System.IO.Path::GetFileName(CompletePath) or System.IO.Path::GetFileNameWithoutExtension.

TideSDK How to save a cookie's information to be accessed in different file?

I am trying to use TideSDK's Ti.Network to set the name and value of my cookie.
But how do I get this cookie's value from my other pages?
var httpcli;
httpcli = Ti.Network.createHTTPCookie();
httpcli.setName(cname); //cname is my cookie name
httpcli.setValue(cvalue); //cvalue is the value that I am going to give my cookie
alert("COOKIE value is: "+httpcli.getValue());
How would I retrieve this cookie value from my next page? Thank you in advance!
ok, there are a lot of ways to create storage content on tidesdk. cookies could be one of them, but not necessary mandatory.
In my personal oppinion, cookies are too limited to store information, so I suggest you to store user information in a JSON File, so you can store from single pieces of information to large structures (depending of the project). Supposing you have a project in which the client have to store the app configuration like 'preferred path' to store files or saving strings (such first name, last name) you can use Ti.FileSystem to store and read such information.:
in the following example, I use jQuery to read a stored json string in a file:
File Contents (conf.json):
{
"fname" : "erick",
"lname" : "rodriguez",
"customFolder" : "c:\\myApp\\userConfig\\"
}
Note : For some reason, Tidesdk cannot parse a json structure like because it interprets conf.json as a textfile, so the parsing will work if you remove all the tabs and spaces:
{"fname":"erick","lname":"rodriguez","customFolder":"c:\\myApp\\userConfig\\"}
now let's read it.... (myappfolder is the path of your storage folder)
readfi = Ti.Filesystem.getFile(myappfolder,"conf.json");
Stream = Ti.Filesystem.getFileStream(readfi);
Stream.open(Ti.Filesystem.MODE_READ);
contents = Stream.read();
contents = JSON.parse(contents.toString);
console.log(contents);
now let's store it....
function saveFile(pathToFile) {
var readfi,Stream,contents;
readfi = Ti.Filesystem.getFile(pathToFile);
Stream = Ti.Filesystem.getFileStream(readfi);
Stream.open(Ti.Filesystem.MODE_READ);
contents = Stream.read();
return contents.toString();
};
//if a JSON var is defined into js, there is no problem
var jsonObject = {
"fname":"joe",
"lname":"doe",
"customFolder" : "c:\\joe\\folder\\"
}
var file = pathToMyAppStorage + "\\" + "conf.json";
var saved = writeTextFile(file,JSON.stringify(jsonObject));
console.log(saved);

How to replace text in content control after, XML binding using docx4j

I am using docx4j 2.8.1 with Content Controls in my .docx file. I can replace the CustomXML part by injecting my own XML and then calling BindingHandler.applyBindings after supplying the input XML. I can add a token in my XML such as ¶ then I would like to replace that token in the MainDocumentPart, but using that approach, when I iterate through the content in the MainDocumentPart with this (link) method none of my text from my XML is even in the collection extracted from the MainDocumentPart. I am thinking that even after binding the XML, it remains separate from the MainDocumentPart (??)
I haven't tried this with anything more than a little test doc yet. My token is the Pilcrow: ¶. Since it's a single character, it won't be split in separate runs. My code is:
private void injectXml (WordprocessingMLPackage wordMLPackage) throws JAXBException {
MainDocumentPart part = wordMLPackage.getMainDocumentPart();
String xml = XmlUtils.marshaltoString(part.getJaxbElement(), true);
xml = xml.replaceAll("¶", "</w:t><w:br/><w:t>");
Object obj = XmlUtils.unmarshalString(xml);
part.setJaxbElement((Document) obj);
}
The pilcrow character comes from the XML and is injected by applying the XML bindings to the content controls. The problem is that the content from the XML does not seem to be in the MainDocumentPart so the replace doesn't work.
(Using docx4j 2.8.1)