generate pdf file using fop in .net core 3.1 - asp.net-core-3.1

To generate pdf file from fop file I Used FOP.dll and It work well in .net 4.5
public string GeneratePdf(string foFile, string pdfFile)
{
java.io.OutputStream os = new java.io.BufferedOutputStream(new java.io.FileOutputStream(new java.io.File(pdfFile)));
string ret = "";
try
{
org.apache.fop.apps.FopFactory fopFactory = org.apache.fop.apps.FopFactory.newInstance();
fopFactory.setUserConfig(new java.io.File("fo-config.xml"));
org.apache.fop.apps.Fop fop = fopFactory.newFop("application/pdf", os);
org.apache.fop.apps.FOUserAgent foUserAgent = fop.getUserAgent();
javax.xml.transform.TransformerFactory factory = javax.xml.transform.TransformerFactory.newInstance();
javax.xml.transform.Transformer transformer = factory.newTransformer();
javax.xml.transform.Source src = new javax.xml.transform.stream.StreamSource(new java.io.File(foFile));
javax.xml.transform.Result res = new javax.xml.transform.sax.SAXResult(fop.getDefaultHandler());
transformer.transform(src, res);
ret = pdfFile;
}
catch (Exception ex)
{
throw ex;
}
finally
{
os.close();
}
return ret;
}
but when Convert my Code To .net Core 3.1, It Not Working.
java.io.File throw exception: "System.TypeInitializationException: 'The type initializer for 'java.io.File' threw an exception".
the Inner Exception Is :"Could not load file or assembly 'System.Configuration.ConfigurationManager, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The system cannot find the file specified"
How can I convert a FOP file to PDF exclusively using .netcore 3.1

Related

How to set the path of the file writer to location of the jar using it?

So i recently switch from starting the jar file from a service rather than a script that first goes to the relevant directory. Is there a way to make sure that the path to the jar file is used instead of wherever the service started it? Inside the application i use the code below to get the correct path.
try {
Path p = Path.of(getClass().getProtectionDomain().getCodeSource().getLocation().toURI());
workPath = p.getParent().toString();
if( workPath.matches(".*[lib]")) { // Meaning used as a lib
workPath = Path.of(workPath).getParent().toString();
}
settingsFile = Path.of(workPath, "settings.xml");
} catch (URISyntaxException e) {
Logger.error(e);
}
Store your work path as system property:
try {
Path p = Path.of(getClass().getProtectionDomain().getCodeSource().getLocation().toURI());
workPath = p.getParent().toString();
if(workPath.matches(".*[lib]")) { // Meaning used as a lib
workPath = Path.of(workPath).getParent().toString();
}
System.setProperty("tinylog.directory", workPath); // Set work path as system property
settingsFile = Path.of(workPath, "settings.xml");
} catch (URISyntaxException e) {
Logger.error(e);
}
Use the system property in tinylog.properties:
writer = file
writer.file = #{tinylog.directory}/log.txt
writer.format = {date: HH:mm:ss.SSS} {level}: {message}

How-to get the code for downloading metadata along with images in IBM content manager

I am trying to find the code in java API which works with cm IBM.. the sample code is there but it is just for logging in.. can anyone help to get the code to download the images along with metadata
as you said you have basic connection code, use the below function to download the document..
public String retrieveDocument(CMBConnection connection, CMBItem item)
throws CMBException, IOException, Exception
{
// Get an instance of data management bean
CMBDataManagement dataManagement = connection.getDataManagement();
// Set the current data item
dataManagement.setDataObject(item);
// Retrieve the original file name
CMBObject object = dataManagement.getContent(0);
String inputFileName = object.getOriginalFileName();
// Parse the file name from the full path
int pos=inputFileName.lastIndexOf("\\");
inputFileName = inputFileName.substring(pos+1);
// Write the document content to a new file
String fileName = System.getProperty("user.dir")
+ File.separator + inputFileName;
System.out.println("Output file name " + fileName);
FileOutputStream fileoutstream = new FileOutputStream(fileName);
fileoutstream.write(dataManagement.getContent(0).getData());
fileoutstream.close();
// Return file name
return fileName;
}

Reading a file from /etc/designs in java

I'm in need of storing a string in JCR that'll be used in an OSGi bundle(so that it can be modified as needed in CRXde)
I have stored my string at the location /etc/designs/shc/components/linkcheck/regex.txt/jcr:content, where regex.txt is the file that contains my regex string.
I'm using the following code to read that String in a bundle but with no luck. It is throwing NullPointerException at resourceResolver.getResource
Resource dataResource = resourceResolver.getResource("/etc/designs/shc/components/linkcheck/regex.txt/jcr:content");
Node node = dataResource.adaptTo(Node.class);
String regex = node.getProperty("jcr:data").getValue().toString();
regex.txt is of type nt:file and the data is visible in CRXde but the code fails.
Any direction will be highly appreciated!
this should work-
Resource dataResource = resourceResolver.getResource("/etc/designs/shc/components/linkcheck/regex.txt");
Node jcnode = dataResource.adaptTo(Node.class).getNode("jcr:content");
InputStream is = jcnode.getProperty("jcr:data").getBinary().getStream();
StringBuilder sb = new StringBuilder();String line;
try {
br = new BufferedReader(new InputStreamReader(is));
while ((line = br.readLine()) != null) {
sb.append(line);
}
} catch (IOException e) {
//do something
}
//do whatever with sb
Since the NullPointerException is thrown at resourceResolver.getResource(...), please check the resourceResolver is not null.

java.io.FileNotFoundException - Can't read my properties file

Eclipse/Linux - Java Web Application.
I added a file named dao.properties to my project. I put it in a package : com.cdp.dao . My class which uses this file is in the same package.
When I try to load it I have a
java.io.FileNotFoundException
/myproject/src/com/cdp/dao/dao.properties (No such file or directory) OR
dao.properties (No such file or directory)
This my code :
Properties prop;
FileInputStream fis;
File file = new File("/myproject/src/com/cdp/dao/dao.properties");
//File file = new File("dao.properties"); doesn't work either
try {
prop = new Properties();
fis = new FileInputStream(file);
prop.load(fis);
dbUrl = prop.getProperty("dbUrl");
user = prop.getProperty("user");
pwd = prop.getProperty("pwd");
driver = prop.getProperty("driver");
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Use
this.getClass().getResourceAsStream("dao.properties");
to load the properties file
From Class, the path is relative to the package of the class unless
you include a leading slash, so if you don't want to use the current package,
include a slash like this:
InputStream in = this.getClass().getResourceAsStream("/dir/SomeTextFile.txt");
but if you need to use classpath use just filename like below
InputStream in = this.getClass().getResourceAsStream("dao.properties");

How can I obtain the version number of a custom Eclipse feature at runtime?

I would like to display the version number of a custom Eclipse feature I am developing in the title bar of its perspective. Is there a way to obtain the version number from the runtime plugin and/or workbench?
Something like:
Platform.getBundle("my.feature.id").getHeaders().get("Bundle-Version");
should do the trick.
Note (from this thread) that it can not be used anywhere within the plugin itself:
this.getBundle() is not valid until AFTER super.start(BundleContext) has been called on your plugin.
So if you are using this.getBundle() within your constructor or within your start(BundleContext) before calling super.start() then it will return null.
If that fails, you have here a more complete "version":
public static String getPlatformVersion() {
String version = null;
try {
Dictionary dictionary =
org.eclipse.ui.internal.WorkbenchPlugin.getDefault().getBundle().getHeaders();
version = (String) dictionary.get("Bundle-Version"); //$NON-NLS-1$
} catch (NoClassDefFoundError e) {
version = getProductVersion();
}
return version;
}
public static String getProductVersion() {
String version = null;
try {
// this approach fails in "Rational Application Developer 6.0.1"
IProduct product = Platform.getProduct();
String aboutText = product.getProperty("aboutText"); //$NON-NLS-1$
String pattern = "Version: (.*)\n"; //$NON-NLS-1$
Pattern p = Pattern.compile(pattern);
Matcher m = p.matcher(aboutText);
boolean found = m.find();
if (found) {
version = m.group(1);
}
} catch (Exception e) {
}
return version;
}
I use the first option:
protected void fillStatusLine(IStatusLineManager statusLine) {
statusItem = new StatusLineContributionItem("LastModificationDate"); //$NON-NLS-1$
statusItem.setText("Ultima Actualizaci\u00f3n: "); //$NON-NLS-1$
statusLine.add(statusItem);
Dictionary<String, String> directory = Platform.getBundle("ar.com.cse.balanza.core").getHeaders();
String version = directory.get("Bundle-Version");
statusItem = new StatusLineContributionItem("CopyRight"); //$NON-NLS-1$
statusItem.setText(Messages.AppActionBar_18);
statusLine.add(statusItem);
}
As #zvikico says above, the accepted answer does not work for Features, only Plug-ins (OSGi Bundles, which Features are not). The way to get info about installed features is via org.eclipse.core.runtime.Platform.getBundleGroupProviders() as described here.
A version of what VonC provided to retrieve the primary Eclipse version number, but one that doesn't reference internal classes (which you should avoid doing):
Platform.getBundle(PlatformUI.PLUGIN_ID).getHeaders().get("Bundle-Version");