I am using docx4j to run through my document and extract the text. I am using the code below to check if I got the texts right
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new File(tempName));
String XPATH_TO_SELECT_TEXT_NODES = "//w:t";
List<Object> texts;
texts = wordMLPackage.getMainDocumentPart().getJAXBNodesViaXPath(XPATH_TO_SELECT_TEXT_NODES,true);
for (Object obj : texts) {
Text text = (Text) ((JAXBElement) obj).getValue();
System.out.println("line "+ i+": "+text.getValue());
i++;
}
However, I am getting an error below
Caused by: java.lang.ClassNotFoundException: org.apache.xml.serializer.SerializationHandler.
I already imported the important dependencies for this to work:
dependency 'org.docx4j:docx4j:6.1.2'
Thank you! Hope you can help me.
Related
I am working on an application where I have to update the pom.xml file programmatically.
Steps which I am following.
Create JAVA POJO from maven XSD (http://maven.apache.org/xsd/maven-4.0.0.xsd).
Using the following program to load pom.xml file in POJO and updated value in the model then updated the same pom.xml file.
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
File xml = new File("C:\\DS Designer Forum\\pomfiles\\pom.xml");
Document document = db.parse(xml);
JAXBContext jc = JAXBContext.newInstance(Model.class);
Binder<Node> binder = jc.createBinder();
Model model = (Model) binder.unmarshal(document);
Dependencies dependencies = model.getDependencyManagement().getDependencies();
if(dependencies != null) {
for(Dependency dependency : dependencies.getDependency()) {
if(!StringUtils.isEmpty(dependency.getScope()) && dependency.getScope().contains("provided")) {
String scope = dependency.getScope().replace("provided", StringUtils.EMPTY);
dependency.setScope(scope);
}
}
}
binder.updateXML(model);
TransformerFactory tf = TransformerFactory.newInstance();
tf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, false);
Transformer t = tf.newTransformer();
t.transform(new DOMSource(document), new StreamResult(new File("C:\\DS Designer Forum\\pomfiles\\aaaaapom.xml")));
getting the following error.
**
Exception in thread "main" javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"project"). Expected elements are <{http://maven.apache.org/POM/4.0.0}project>
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:726)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:247)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:242)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(Loader.java:109)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext$DefaultRootLoader.childElement(UnmarshallingContext.java:1131)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:556)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:538)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.InterningXmlVisitor.startElement(InterningXmlVisitor.java:60)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.SAXConnector.startElement(SAXConnector.java:153)
at com.sun.xml.internal.bind.unmarshaller.DOMScanner.visit(DOMScanner.java:229)
at com.sun.xml.internal.bind.unmarshaller.DOMScanner.scan(DOMScanner.java:112)
at com.sun.xml.internal.bind.unmarshaller.DOMScanner.scan(DOMScanner.java:95)
at com.sun.xml.internal.bind.unmarshaller.DOMScanner.scan(DOMScanner.java:88)
at com.sun.xml.internal.bind.v2.runtime.BinderImpl.associativeUnmarshal(BinderImpl.java:146)
at com.sun.xml.internal.bind.v2.runtime.BinderImpl.unmarshal(BinderImpl.java:117)
at com.sunlife.innovation.update.LoadInfoFromPOMFile.main(LoadInfoFromPOMFile.java:52)
**
Please help me.
Thanks
I am using apache commons CSV parser to convert the CSV to a map. In the map I couldnt able to read some values through intellij debuger. if I manually type map.get("key") the value is null. However, if I copy paste the key from the map, I am getting data. Couldnt understand what is going wrong. Any pointers would help. Thanks
Here is my CSV parser code:
private CSVParser parseCSV(InputStream inputStream) {
System.out.println("What is the encoding "+ new InputStreamReader(inputStream).getEncoding());
try {
return new CSVParser(new InputStreamReader(inputStream), CSVFormat.DEFAULT
.withFirstRecordAsHeader()
.withIgnoreHeaderCase()
.withSkipHeaderRecord()
.withTrim());
} catch (IOException e) {
throw new IPRSException(e);
}
}
There was a weird character in the strings (Reference: Reading UTF-8 - BOM marker). The below syntax help to resolve the issue
header = header("\uFEFF", "");
in java use UnicodeReader:
String path = "demo.csv";
CSVFormat.Builder builder = CSVFormat.RFC4180.builder();
CSVFormat format = builder.setQuote(null).setHeader().build();
InputStream in = new FileInputStream(new File(path));
CSVParser parser = new CSVParser(new BufferedReader(new UnicodeReader(in)), format);
I am new to Spring Boot. I have this emailprop.properties in src/main/resource:
//your private key
mail.smtp.dkim.privatekey=classpath:/emailproperties/private.key.der
But I am getting the error as
classpath:\email properties\private.key.der (The filename, directory
name, or volume label syntax is incorrect)
How do I properly load this file?
Update-1
my java code is
dkimSigner = new DKIMSigner(emailProps.getProperty("mail.smtp.dkim.signingdomain"), emailProps.getProperty("mail.smtp.dkim.selector"),
emailProps.getProperty("mail.smtp.dkim.privatekey"));
its working as "D:\\WorkShop\\MyDemoProj\\EmailService\\src\\main\\resources\\private.key.der"Instead of emailProps.getProperty("mail.smtp.dkim.privatekey")
Update-2
i have tried java code is
String data = "";
ClassPathResource cpr = new ClassPathResource("private.key.der");
try {
byte[] bdata = FileCopyUtils.copyToByteArray(cpr.getInputStream());
data = new String(bdata, StandardCharsets.UTF_8);
} catch (IOException e) {
e.printStackTrace();
}
dkimSigner = new DKIMSigner(emailProps.getProperty("mail.smtp.dkim.signingdomain"), emailProps.getProperty("mail.smtp.dkim.selector"),data);
Error is : java.io.FileNotFoundException: class path resource [classpath:private.key.der] cannot be resolved to URL because it does not exist
Tried Code is :
ClassPathResource resource = new ClassPathResource(emailProps.getProperty("mail.smtp.dkim.privatekey"));
File file = resource.getFile();
String absolutePath = file.getAbsolutePath();
Still same error..
please update the answer..
If you want to load this file runtime then you need to use ResourceLoader please have a look here for the documentation - section 8.4.
Resource resource = resourceLoader.getResource("classpath:/emailproperties/private.key.der");
Now if you want to keep this exact path in properties file you can keep it there and then load it in your Autowired constructor/field like that:
#Value("${mail.smtp.dkim.privatekey}") String pathToPrivateKey
and then pass this to the resource loader.
Full example you can find here. I don't want to copy paste it.
If your file is located here:
"D:\\WorkShop\\MyDemoProj\\EmailService\\src\\main\\resources\\private.key.der"
then it should be:
mail.smtp.dkim.privatekey=classpath:private.key.der
EDIT:
I see now, you are using DKIMSigner, which expects file-path string,
Try changing your code like this:
ClassPathResource resource = new ClassPathResource(emailProps.getProperty("mail.smtp.dkim.privatekey"));
File file = resource.getFile();
String absolutePath = file.getAbsolutePath();
dkimSigner = new DKIMSigner(emailProps.getProperty("mail.smtp.dkim.signingdomain"), emailProps.getProperty("mail.smtp.dkim.selector"),absolutePath
);
I need to produce i18n reports with existing code using JasperReports (4.7.1 originally but same problem with 5.6.1).
I did the following:
Report name is: x_report.jrxml
Added attribute resourceBundle="x_report" to the jasperReport tag in the jrxml file
Replaced text with $R{} tags in jrxml file
Built file
Added JRParameter.REPORT_LOCALE and JRParameter.REPORT_RESOURCE_BUNDLE to the parameters to pass to the JasperFillManager:
File reportFile = new File(getClass().getResource("/reports").getFile(), report.getReportFileName());
Map<String, Object> fillParams = (Map<String, Object>) report.getFillParameters();
java.util.Locale locale = new java.util.Locale("it");
fillParams.put(JRParameter.REPORT_LOCALE, locale);
String resBundleName = ...
ResourceBundle resBundle = ResourceBundle.getBundle(resBundleName, locale);
fillParams.put(JRParameter.REPORT_RESOURCE_BUNDLE, resBundle);
...
The JasperFillManager getting the params (with locale and resource bundle) and the report path:
BeanReport report = (BeanReport) this.report;
Collection<?> beans = report.getBeans();
JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(beans);
JasperPrint print = JasperFillManager.fillReport(reportFile.getPath(), fillParams, ds);
...
if (httpSession != null) {
httpSession.setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, print);
exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, "ReportImage?image=");
}
exporter is a JRExporter:
exporter.setParameter(JRExporterParameter.JASPER_PRINT, print);
exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "UTF-8");
exporter.exportReport();
The resource bundle is found but my generated report still has the $R{} tags instead of the localized text.
What could be missing?
Thank you for your help!
I'm not sure if you are using JasperReports Server or not, but if you are what worked for me was to drop my properties bundle files into the ../jasperserver-pro/WEB-INF/classes folder.
I would like to know why this code:
String inputfilepath = "D:\\DFADFADSF";
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File(inputfilepath + ".docx"));
MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
wordMLPackage.setFontMapper(new IdentityPlusMapper());
FOSettings foSettings = Docx4J.createFOSettings();
foSettings.setWmlPackage(wordMLPackage);
String outputfilepath = "D:\\OUT_FontContent.pdf";
OutputStream os = new java.io.FileOutputStream(outputfilepath);
Docx4J.toPDF(wordMLPackage,os);
Throws this exception:
org.docx4j.openpackaging.exceptions.Docx4JException: Exception exporting package
org.docx4j.openpackaging.exceptions.Docx4JException: Exception executing transformer: org.apache.fop.fo.ValidationException: "fo:flow" is missing child elements. Required content model: marker* (%block;)+
Although there are similar posts, I haven't seen one about this exception...
Maybe I should add aditional code to configure the conversion...