Saving documents using OpenOffice java api throws Exception - java

FYI / Context: I am running a portable installation of libreoffice on windows 10 (getting the same exception on a mac with normal installation though).
Reading the document
Maybe this is important... I am reading the document through an InputStream, because the other method fails due to a different exception (probably a story for another time).
public XComponent openFileViaStream(File file) throws CommandAbortedException, Exception {
Object fileAccess = this.componentFactory.createInstanceWithContext(SimpleFileAccessClass, this.context);
XSimpleFileAccess xSimpleFileAccess = (XSimpleFileAccess) UnoRuntime.queryInterface(XSimpleFileAccess.class,
fileAccess);
XStream xInputStream = xSimpleFileAccess.openFileReadWrite(file.getAbsolutePath());
PropertyValue[] loadProps = new PropertyBuilder().add("InputStream", xInputStream).build();
return loader.loadComponentFromURL("private:stream", "_blank", 0, loadProps);
}
Writing the document
PropertyBuilder is a utility class that just builds an Array of PropertyValues for ease of use.
public void save(Object storeMe, File destination) throws IOException, MalformedURLException {
//#formatter:off
PropertyValue[] propertyValue = new PropertyBuilder()
.add("Overwrite", Boolean.TRUE)
.add("FilterName", "StarOffice XML")
.build();
//#formatter:on
XStorable2 st = UnoRuntime.queryInterface(XStorable2.class, storeMe);
// already tried
// st.storeAsURL(destination.toURI().toURL().toString(), propertyValue);
// st.storeToURL(destination.toURI().toString(), propertyValue);
// st.storeToURL(destination.toURI().toURL().toString(), propertyValue);
st.storeAsURL(destination.toURI().toString(), propertyValue);
}
The exception
I couldn't find a solution while searching on stackoverflow...
com.sun.star.task.ErrorCodeIOException: SfxBaseModel::impl_store <file:/E:/test/abc.odt> failed: 0x81a(Error Area:Io Class:Parameter Code:26)
at com.sun.star.lib.uno.environments.remote.Job.remoteUnoRequestRaisedException(Job.java:173)
at com.sun.star.lib.uno.environments.remote.Job.execute(Job.java:139)
at com.sun.star.lib.uno.environments.remote.JobQueue.enter(JobQueue.java:334)
at com.sun.star.lib.uno.environments.remote.JobQueue.enter(JobQueue.java:303)
at com.sun.star.lib.uno.environments.remote.JavaThreadPool.enter(JavaThreadPool.java:87)
at com.sun.star.lib.uno.bridges.java_remote.java_remote_bridge.sendRequest(java_remote_bridge.java:636)
at com.sun.star.lib.uno.bridges.java_remote.ProxyFactory$Handler.request(ProxyFactory.java:146)
at com.sun.star.lib.uno.bridges.java_remote.ProxyFactory$Handler.invoke(ProxyFactory.java:128)
at com.sun.proxy.$Proxy10.storeAsURL(Unknown Source)
at DocumentHandler.save(DocumentHandler.java:54)
at Main.test(Main.java:14)
at Main.main(Main.java:19)
I really have no idea what I am doing wrong. I've looked at examples from api.libreoffice.org etc.
Am I missing something? A PropertyValue?
Thank you in advance!

See if any of these ideas help.
The URI should look like file:///E:/test/abc.odt.
Set the filter name to StarOffice XML (Writer) or writer8. Or don't set it at all; pass one property instead of two.
Verify you have the authorization to write to the file, for example by using standard Java libraries to create a file in that location. Be sure the file is not locked by some other process.

Related

How to get files from resources folder. Spring Framework

I'm trying to unmarshal my xml file:
public Object convertFromXMLToObject(String xmlfile) throws IOException {
FileInputStream is = null;
File file = new File(String.valueOf(this.getClass().getResource("xmlToParse/companies.xml")));
try {
is = new FileInputStream(file);
return getUnmarshaller().unmarshal(new StreamSource(is));
} finally {
if (is != null) {
is.close();
}
}
}
But I get this errors:
java.io.FileNotFoundException: null (No such file or directory)
Here is my structure:
Why I can't get files from resources folder? Thanks.
Update.
After refactoring,
URL url = this.getClass().getResource("/xmlToParse/companies.xml");
File file = new File(url.getPath());
I can see an error more clearly:
java.io.FileNotFoundException: /content/ROOT.war/WEB-INF/classes/xmlToParse/companies.xml (No such file or directory)
It tries to find WEB-INF/classes/
I have added folder there, but still get this error :(
I had the same problem trying to load some XML files into my test classes. If you use Spring, as one can suggest from your question, the easiest way is to use org.springframework.core.io.Resource - the one Raphael Roth already mentioned.
The code is really straight forward. Just declare a field of the type org.springframework.core.io.Resource and annotate it with org.springframework.beans.factory.annotation.Value - like that:
#Value(value = "classpath:xmlToParse/companies.xml")
private Resource companiesXml;
To obtain the needed InputStream, just call
companiesXml.getInputStream()
and you should be okay :)
But forgive me, I have to ask one thing: Why do you want to implement a XML parser with the help of Spring? There are plenty build in :) E.g. for web services there are very good solutions that marshall your XMLs into Java Objects and back...
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("fileName").getFile());
you are suppose to give an absolute path (so add a loading ´/´, where resource-folder is the root-folder):
public Object convertFromXMLToObject(String xmlfile) throws IOException {
FileInputStream is = null;
File file = new File(String.valueOf(this.getClass().getResource("/xmlToParse/companies.xml")));
try {
is = new FileInputStream(file);
return getUnmarshaller().unmarshal(new StreamSource(is));
} finally {
if (is != null) {
is.close();
}
}
}

Exception:Missing the manifest.properties

I am new to opennlp, I am getting Missing the manifest.properties! exception when i excute the following code,please tell me suggestion to avoid this.
public class PrePostProcessing_Peregrine {
public Map<String,Set<String>> btntMap;
public Map<String, String> fishMap;
public SentenceModel sModel;
public SentenceDetectorME sentDet;
public Map<String,Set<String>> topBottomTermSet;
public PrePostProcessing_Peregrine() throws IOException {
FileInputStream str=new FileInputStream("/home/rajendraprasad.yk/Desktop/data/en-sent.bin");
System.out.println(str+"===================>");
SentenceModel sModel = new SentenceModel(str);
System.out.println("===================model =================>"+sModel);
sentDet = new SentenceDetectorME(sModel);
System.out.println("===================>sentDet "+sentDet);
System.err.println("Sentence Detector Initialized");
Exception is:
opennlp.tools.util.InvalidFormatException: Missing the manifest.properties!
at opennlp.tools.util.model.BaseModel.validateArtifactMap(BaseModel.java:217)
at opennlp.tools.sentdetect.SentenceModel.validateArtifactMap(SentenceModel.java:78)
at opennlp.tools.util.model.BaseModel.<init>(BaseModel.java:142)
at opennlp.tools.sentdetect.SentenceModel.<init>(SentenceModel.java:73)
at com.molcon.Text_Mining.PrePostProcessing_Peregrine.<init>(PrePostProcessing_Peregrine.java:66)
at com.molcon.Text_Mining.TextMining.peregrineRun(TextMining.java:207)
at com.molcon.Text_Mining.TextMining.process_journals_for_Mining(TextMining.java:108)
I made mistake at FileInputStream modelIn = new FileInputStream("/home/rajendraprasad.yk/Desktop/data/en-sent.bin"); now I changed to InputStream modelIn = new FileInputStream("/home/rajendraprasad.yk/Desktop/data/en-sent.bin"); from this changes am not getting any exception but not able to load file from SentenceModel.
when i execute this line SentenceModel sModel = new SentenceModel(modelIn); am not getting any response,please help
For what I know there are two possible causes:
the model you are using is corrupted, try to download it again
the version of the model and of the OpenNLP library that you are using are not matching. As I read from the official website models are version specific, so you should try to understand if this is you case, and act accordingly.
If you check this constructor you will see that manifest.properties is not a file, it is a set of hard-coded properties:
Properties manifest = new Properties();
manifest.setProperty(MANIFEST_VERSION_PROPERTY, "1.0");
...
artifactMap.put("manifest.properties", manifest);
When you compare it to the InputStream constructor you can see that manifest.properties is nowhere to be found, only loadModel(in) which leads to conclusion that manifest.properties should be in the model file.
Why this works in a standalone application and not inside Tomcat requires some debugging.
As #5agado suggested, your model's version might be different than the library's.

One API to handle adding and updating files

I'm using SVNKIT 1.8 with SVN 1.8.5 and the SVN protocol to attempt to add files in bulk to my SVN repository. I would like to have one method for adding and updating files and the below code successfully handles both when using the FILE protocol since the editor.addFile(file, null, -1) throws an SVNException. When I switch to the SVN protocol (desired protocol), the editor.addFile(file, null, -1); doesn't throw an exception. Instead the editor.closeEdit(); throws an exception which is not desired. Any ideas on how to use one API for both adding and updating files?
public void addFiles(Map<String, String> data) throws Exception {
TreeSet<String> filesToCreate = new TreeSet<String>(data.keySet());
SVNRepository repo = null;
ISVNEditor editor = null;
try {
repo = openSession();
editor = repo.getCommitEditor("Adding files.", null);
editor.openRoot(-1);
for (String file : filesToCreate) {
try {
editor.addFile(file, null, -1);
} catch (SVNException e) {
editor.openFile(file, -1);
}
editor.applyTextDelta(file, null);
SVNDeltaGenerator gen = new SVNDeltaGenerator();
String checksum = gen.sendDelta(file, new ByteArrayInputStream(data.get(file).getBytes()), editor, true);
editor.closeFile(file, checksum);
}
editor.closeEdit();
} catch (Exception ex) {
abort(editor);
throw new Exception(ex.toString(), ex);
} finally {
closeSession(repo);
}
}
This is a side effect of an optimization in the svn:// protocol. During an editor drive the server does not send any response unless there is an error and as such the client can't tell that a specific action succeeded. I haven't looked at SVNKit's code but I'd bet that you could potentially get the exception from any of the editor methods since the error will be detected in the next editor drive call after the server responds. In this case your changes are so small that the editor drive sending happens before the response from the server can be detected and so you end up seeing the error when you do closeEdit().
The svnmucc command in Subversion has a similar problem as what you're trying to solve. It has a put operation that adds or updates a file. It uses the same technique that Dmitry advised you to use on the svnkit-users mailing list (link1, link2). Specifically running a check_path before determining to add or create the file.
You're not going to be able to do anything better than this because of the way the protocol works.

Mapr using Java

I am new to Hadoop, Mapr and Pivotal. I have written java code to write into pivotal but facing issue while writing into Mapr.
public class HadoopFileSystemManager {
private String url;
public void writeFile(String filePath,String data) throws IOException, URISyntaxException {
Path fPath = new Path(filePath);
String url = url = "hdfs://"+ip+":"+"8020";
FileSystem fs = FileSystem.get(new URI(url),new Configuration());
System.out.println(fs.getWorkingDirectory());
FSDataOutputStream writeStream = fs.create(fPath);
writeStream.writeChars(data);
writeStream.close();
}
}
This code works fine with pivoatal but fails with Mapr.
For Mapr i am using port = 7222.
I am getting the following error
"An existing connection was forcibly closed by the remote host"
Please let me know if am using the right port or anything needs to be changed in the code specific to Mapr.
I have stopped the iptables.
Any info is much appreciated.
Thanks
Heading
Try this code. But make sure you have MapR client setup in the node from where you are executing the test.
public class HadoopFileSystemManager {
private String url;
public void writeFile(String filePath,String data) throws IOException, URISyntaxException {
System.setProperty( "java.library.path", "/opt/mapr/lib" );
Path fPath = new Path(filePath);
String url = url = "hdfs://"+ip+":"+"8020";
FileSystem fs = FileSystem.get(new URI(url),new Configuration());
System.out.println(fs.getWorkingDirectory());
FSDataOutputStream writeStream = fs.create(fPath);
writeStream.writeChars(data);
writeStream.close();
}
}
Add the following to the classpath:
/opt/mapr/hadoop/hadoop-0.20.2/conf:/opt/mapr/hadoop/hadoop-0.20.2/lib/hadoop-0.20.2-dev-core.jar:/opt/mapr/hadoop/hadoop-0.20.2/lib/maprfs-0.1.jar:.:/opt/mapr/hadoop/hadoop-0.20.2/lib/commons-logging-1.0.4.jar:/opt/mapr/hadoop/hadoop-0.20.2/lib/zookeeper-3.3.2.jar
This statement in the above code: System.setProperty( "java.library.path", "/opt/mapr/lib" ); can be removed and can be supplied using -Djava.library.path too, if you are running your program from terminal when building.
/opt/mapr may not be your path to mapr files. If that's the case replace the path accordingly wherever applicable.
After comment:
If you are using Maven to build your project, try using the following in the pom.xml,
and with scope provided. MapR is compatible with the normal Apache Hadoop distribution too. So, while building you can use the same. Then when you run your program, you would supply the mapR jars in the classpath.
<dependency>
<groupid>hadoop</groupid>
<artifactid>hadoop</artifactid>
<version>0.20.2</version>
<scope>provided</scope>
</dependency>

Can Xerces support XMLCatalogResolver and <xs:include/> at the same time?

Xerces claims to allow XML Catalog support to be added to a reader like this:
XMLCatalogResolver resolver = new XMLCatalogResolver();
resolver.setPreferPublic(true);
resolver.setCatalogList(catalogs);
XMLReader reader = XMLReaderFactory.createXMLReader(
"org.apache.xerces.parsers.SAXParser");
reader.setProperty("http://apache.org/xml/properties/internal/entity-resolver",
resolver);
But as soon as I do this then any <xs:include/> tags in my schemas are no longer processed. It seems like the XMLCatalogResolver becomes the only go-to place for entity resolution once it's added, so includes can't work anymore. Eclipse OTOH successfully validates using the same catalog, so it should be possilbe.
Is there a way around this, or are there any other Java based validators that support catalogs?
Thanks, Dominic.
I finally solved this by overriding the XMLCatalogResolver and logging the various calls made to the resolveEntity() method. I observed 3 types of call being made, only one of which made sense to be resolved using the XML catalog. So, I merely returned a FileInputStream directly for the other two call types.
Here is the code I used inside my custom XMLCatalogResolver class:
public XMLInputSource resolveEntity(XMLResourceIdentifier resourceIdentifier)
throws IOException
{
if(resourceIdentifier.getExpandedSystemId() != null)
{
return new XMLInputSource(resourceIdentifier.getPublicId(),
resourceIdentifier.getLiteralSystemId(),
resourceIdentifier.getBaseSystemId(),
new FileReader(getFile(resourceIdentifier.getExpandedSystemId())),
"UTF-8");
}
else if((resourceIdentifier.getBaseSystemId() != null) &&
(resourceIdentifier.getNamespace() == null))
{
return new XMLInputSource(resourceIdentifier.getPublicId(),
resourceIdentifier.getLiteralSystemId(),
resourceIdentifier.getBaseSystemId(),
new FileReader(getFile(resourceIdentifier.getBaseSystemId())),
"UTF-8");
}
else
{
return super.resolveEntity(resourceIdentifier);
}
}
private File getFile(String urlString) throws MalformedURLException
{
URL url = new URL(urlString);
return new File(url.toURI());
}
I'm not sure why this wouldn't be done by default within Xerces, but hopefully this helps the next person that encounters this problem.

Categories