How to read attribute from HDF5 file? - java

I am using libraries provided by ch.systemsx.cisd.hdf5.HDF5Factory(JHDF5) for reading an HDF5 file. Their documentation link is not working as well and I do not know whom to approach to get a solution for this.
Does anyone here know how to read attribute value from HDF5 file using above java libraries ?

After lot of trial and error found a solution for this. Please find code below to read attribute value from HDF5 file.
nwbFile - Name of HDF5 file (It should be opened).
attributeName: Name of the attribute.
path - path of the Node in file whose attribute needs to be read.
DataFormat dataset = (Dataset) FileFormat.findObject(nwbFile, path);
List<Attribute> attributes = dataset.getMetadata();
for(Attribute a : attributes)
{
if(a.getName().equals(attributeName))
{
Object obj = a.getValue();
if (obj instanceof double[])
{
Double d = ((double[]) obj)[0];
return d.toString();
}
else if (obj instanceof String[])
{
return ((String[]) obj)[0];
}
}
}

Related

List all files in (sub)directories in Azure

I am developing an azure function using Java. I need to iterate all the files in the following folder
aDirectory/aSubdirectoryWithManyFiles/
There are many files in that path,:
aDirectory/aSubdirectoryWithManyFiles/file1
aDirectory/aSubdirectoryWithManyFiles/file2
aDirectory/aSubdirectoryWithManyFiles/file3
aDirectory/aSubdirectoryWithManyFiles/file4
aDirectory/aSubdirectoryWithManyFiles/file5
so I wrote the following code in order to get them all:
// myCloudBlobContainer is a CloudBlobContainer
// I expected to get all files thanks to the next row
Iterable<ListBlobItem> blobs = myCloudBlobContainer.listBlobs();
// The only blob found in the container is the directory itself
for (ListBlobItem blob : blobs) {
//log the current blob URI
if (blob instanceof CloudBlob) { // this never happens
CloudBlob cloudBlob = (CloudBlob) blob;
//make nice things with every found file
}
}
The only blob iterated in the for is the directory, noone of the expected files. so in logs i get only the following URI:
https://blablablabla.blob.core.windows.net/aDirectory/aSubdirectoryWithManyFiles/
What should I do in order to access every file?
And in case I would have more than one subdirectory, as in the following example?
aDirectory/aSubdirectoryWithManyFiles/files(1-5)
aDirectory/anotherSubdirectoryWithManyFiles/files(6-10)
Thanks in advance
Edit
In order to make methods testable, the project uses wrappers and interfaces instead of directly using directly a CloudBlobContainer; basically, the CloudBlobContainer is given by CloudBlobClient.getContainerReference("containername")
After the answer to this question, I changed teh code to the following
so I used listBlobs with parameters myCloudBlobContainer.listBlobs("aDirectory", true) and I wrote the following code in order to get them all:
// myCloudBlobClient is a CloudBlobClient
CloudBlobContainer myCloudBlobContainer = myCloudBlobClient.getContainerReference("containername")
// I expected to get all files thanks to the next row
Iterable<ListBlobItem> blobs = myCloudBlobContainer.listBlobs("aDirectory", true); // HERE THE CHANGE
// No blob found this time
for (ListBlobItem blob : blobs) { // NEVER IN THE FOR
//log the current blob URI
if (blob instanceof CloudBlob) {
CloudBlob cloudBlob = (CloudBlob) blob;
//make nice things with every found file
}
}
But this time, it doesn't go at all in the for...
I must say that the previous answer made me to waste time; the problem was in the fact that only one for is not enough to find files in folders. The first for finds the folders and subfolders, plus (maybe, i didn't check) files that are in the "root" (let's call it like that).
Having the folders, for each of them we have to cast as CloudBlobDirectory in order to see and iterate all contained files with another for.
Here the solution that works for me:
// myCloudBlobClient is a CloudBlobClient
CloudBlobContainer myCloudBlobContainer = myCloudBlobClient.getContainerReference("containername")
// I expected to get all files thanks to the next row
Iterable<ListBlobItem> blobs = myCloudBlobContainer.listBlobs();
// only directories here, another for needed to scan files
for (ListBlobItem blob : blobs) {
if (blob instanceof CloudBlobDirectory) {
CloudBlobDirectory directory = (CloudBlobDirectory)blob;
//next is in try/catch
Iterable<ListBlobItem> fileBlobs = directory.listBlobs();
for (ListBlobItem fileBlob : fileBlobs) {
if (fileBlob instanceof CloudBlob) {
CloudBlob cloudBlob = (CloudBlob) fileBlob;
//make nice things with every found file
}
}
} // else: may be we found a cloudBlob in root?
}
This helped me to find the right way:
https://social.msdn.microsoft.com/Forums/en-US/1cfdc91f-e588-4839-a878-9650339a0a06/list-all-blobs-in-c?forum=windowsazuredata
Try using the following override of listBlobs method:
listBlobs(String prefix, boolean useFlatBlobListing)
So your code would be:
Iterable<ListBlobItem> blobs = myCloudBlobContainer.listBlobs("aDirectory", true);
This will list all blobs inside "aDirectory" virtual folder in your blob container.

How to fetch change set by work item link targetRef URI?

First please let me know the difference between these two
com.ibm.team.filesystem.workitems.change_set
com.ibm.team.workitem.linktype.scm.tracksChanges
Because I have found some change set links using trackschanges Id and after that when I try to fetchCompleteItem for the link it return null or exception like that data is not present in the database. I am using the below code to fetchCompleteItem.
Really stuck here please help:
for(Object wI : links){
ILink link = (ILink) wI;
Object source = link.getTargetRef().resolve();
IChangeSetHandle changeSetHandle = (IChangeSetHandle) link.getTargetRef().resolve();
wiHandles.add((IChangeSetHandle) link.getTargetRef().resolve());
if (source instanceof IChangeSetHandle) {
changeSet = (IChangeSet) repo.itemManager().fetchCompleteItem(
changeSetHandle,
IItemManager.DEFAULT, monitor);
System.out.println("changeset---1"+changeSet);
}
}

Variables did not replaced

I wrote some code to replace variables in docx tamplate file header.
List<SectionWrapper> sectionWrappers = this.wordMLPackage.getDocumentModel().getSections();
for (SectionWrapper sw : sectionWrappers) {
HeaderFooterPolicy hfp = sw.getHeaderFooterPolicy();
HeaderPart defaultHP = hfp.getDefaultHeader();
if (defaultHP != null) {
defaultHP.variableReplace(getVariablesForChange());
if (hfp.getFirstHeader() != null) {
hfp.getFirstHeader().variableReplace(getVariablesForChange());
}
}
}
getVariablesForChange() is a Map has contains the variables and values.
When I running the unit test the replace is corectly fine but I use this in my web application on Tomee Plume the variables does not replaced.
For example the variable is: ${TOCHANGE} it looks like this after change TOCHANGE.
Docx4j version is: 3.3.6
Please help me to resolve this issue.
It won't work if your KEY is split across separate runs in your docx.
See https://github.com/plutext/docx4j/blob/master/src/main/java/org/docx4j/model/datastorage/migration/VariablePrepare.java

OrientDB - Java create a field as EMBEDDEDLIST type

I try to create a field as type EMBEDDEDLIST from Java.
But when I try to create it, is considered as LINK.
If I define a field by Studio as EMBEDDELIST with linked class, Java works properly.
My code:
String fieldName = "trialEmbedded";
List<ODocument> fieldDataItem = doc.getData().field(fieldName);
DataItem di = DataItemFactory.create(dtValidita, importo, descrizione, db);
if (fieldDataItem == null) {
fieldDataItem = new ArrayList<ODocument>();
}
fieldDataItem.add(di.getData());
doc.setField(fieldName, fieldDataItem);
In the doc variable (type ODocument) when I save it, on DB (querying by Studio) I've got in column "trialEmbedded" a link (orange box with #rid clickable), if I specify field as EMBEDDEDLIST works properly.
I resolved in very simple way.
I used the signature of setField with OType parameter, like this:
this.data.field(fieldName, fieldDataItem, OType.EMBEDDEDLIST);

read, replace placeholders and write a word file with docx4j

I have a Word File look like this:
You don't need to understand the content, just take a look to my placeholders <env> and <applikationsabkürzung>. There are 10 pages with these placeholders and now I should replace them with other content. The black and yellow box are company pictures which I won't share.
Now I started to read the whole docx4j doc and generate after some time the following code:
public void manipulateWord(String path, String env, String appl) {
try {
WordprocessingMLPackage wpml = WordprocessingMLPackage.load(new File(path));
MainDocumentPart mdp = wpml.getMainDocumentPart();
List<Object> content = mdp.getContent();
// Include all Strings to replace
HashMap<String, String> mappings = new HashMap<String, String>();
mappings.put("<env>", env);
mappings.put("<applikationsabkürzung>", appl);
for (Object object : content) {
Text textElement = (Text) object;
String textToReplace = textElement.getValue();
if (mappings.keySet().contains(textToReplace)) {
textElement.setValue(mappings.get(textToReplace));
}
}
wpml.save(new File("C:\\Users\\kristina\\Desktop\\outputfile.docx"));
} catch (Docx4JException e) {
LOG.error(e);
}
Some explanaition:
String path is the path of the file in the picture above
String env is the value which should replace <env>
String appl is the value which should replace <applikationsabkürzung>
But when I run the method, nothing happen, my console just print just some infos. If they're important, i'll edit the post, but i don't think so.
So where is my fault? Would it work like that? I'm shortly before to despair...
MainDocumentPart.getContent() will return all OpenXml components within the main document flow (things like headers and footers have their own elements). Your code is assuming that the result of List<Object> content will be a collection of Text elements, which is not necessarily the case. For example, a typical (simple) document structure would be like this:
P // Paragraph element
-> R // Run element
-> Text // Text element
… so getContent() is, in all likelihood, going to spit out a load of P objects for a start.
There are a few ways to traverse docx4 files -- see the main docx4j site for more -- but one approach is shown in the method below. You can pass in MaindocumentPart as the first Object, and Text.class as the object type to search for. This should then assist in identifying all Text elements which contain one of your mapping values:
public List<Object> getAllElementFromObject(Object obj, Class<?> toSearch) {
List<Object> result = new ArrayList<Object>();
if (obj instanceof JAXBElement)
obj = ((JAXBElement<?>) obj).getValue();
if (obj.getClass().equals(toSearch))
result.add(obj);
else if (obj instanceof ContentAccessor) {
List<?> children = ((ContentAccessor) obj).getContent();
for (Object child : children) {
result.addAll(getAllElementFromObject(child, toSearch));
}
}
return result;
}

Categories