From e9f2da131d644133956a20ac38c000e8c8be2820 Mon Sep 17 00:00:00 2001 From: raouldandresy Date: Fri, 22 Feb 2019 12:51:45 +0100 Subject: [PATCH] Method for nested Div and List --- .../xml/pipeline/end/PdfWriterPipeline.java | 74 +++++++++++++++---- 1 file changed, 60 insertions(+), 14 deletions(-) diff --git a/xmlworker/src/main/java/com/itextpdf/tool/xml/pipeline/end/PdfWriterPipeline.java b/xmlworker/src/main/java/com/itextpdf/tool/xml/pipeline/end/PdfWriterPipeline.java index ce8c1bdc9f..ae57bc11aa 100644 --- a/xmlworker/src/main/java/com/itextpdf/tool/xml/pipeline/end/PdfWriterPipeline.java +++ b/xmlworker/src/main/java/com/itextpdf/tool/xml/pipeline/end/PdfWriterPipeline.java @@ -141,24 +141,70 @@ private void write(final WorkerContext context, final ProcessObject po) throws P while (null != (writable = po.poll())) { if (writable instanceof WritableElement) { for (Element e : ((WritableElement) writable).elements()) { - try { - if (!doc.add(e) && LOG.isLogging(Level.TRACE)) { - LOG.trace(String.format( - LocaleMessages.getInstance().getMessage(LocaleMessages.ELEMENT_NOT_ADDED), - e.toString())); - } - } catch (DocumentException e1) { - if (!continuousWrite) { - throw new PipelineException(e1); - } else { - LOG.error( - LocaleMessages.getInstance().getMessage(LocaleMessages.ELEMENT_NOT_ADDED_EXC), - e1); - } + applyNested(e, continuousWrite, doc); + } + } + } + } + } + + /** + * @param Element + * @param boolean + * @param Document + * @throws PipelineException + * Method for nested Div especially if you have inside a nested list (ex ol/ul) + */ + private void applyNested(Element e, boolean continuousWrite, Document doc) throws PipelineException{ + try { + Class noparams[] = {}; + Class clazz = Class.forName(e.getClass().getName()); + try{ + Method method = clazz.getDeclaredMethod("getContent", noparams); + Class lista = method.invoke(e).getClass(); + Class listzz = Class.forName(lista.getName()); + Method methodSize = listzz.getDeclaredMethod("size", noparams); + if (method.invoke(e) != null && (Integer) methodSize.invoke(method.invoke(e)) > 0) { + for (Element par : (Iterable) method.invoke(e)) { + try{ + Class clazzNested = Class.forName(par.getClass().getName()); + Method methodNested = clazzNested.getDeclaredMethod("getContent", noparams); + Class listaNested = methodNested.invoke(par).getClass(); + Class listzzNested = Class.forName(listaNested.getName()); + Method methodSizeNested = listzzNested.getDeclaredMethod("size", noparams); + if(methodNested.invoke(e) != null && (Integer) methodSizeNested.invoke(methodNested.invoke(par)) > 0){ + applyNested(par, continuousWrite, doc); + } + } catch(NoSuchMethodException err) { + //it has'nt method getContent, so it's not a Div + doc.add(par); } } + } else { + doc.add(e); } + } catch(NoSuchMethodException err) { + //non ha metodo getContent dunque non รจ un DIV + doc.add(e); + } + + } catch (DocumentException e1) { + if (!continuousWrite) { + throw new PipelineException(e1); + } else { + LOG.error(LocaleMessages.getInstance().getMessage(LocaleMessages.ELEMENT_NOT_ADDED_EXC), + e1); } + } catch (ClassNotFoundException e1) { + LOG.error(e1.toString()); + } catch (IllegalAccessException e1) { + LOG.error(e1.toString()); + } catch (SecurityException e1) { + LOG.error(e1.toString()); + } catch (IllegalArgumentException e1) { + LOG.error(e1.toString()); + } catch (InvocationTargetException e1) { + LOG.error(e1.toString()); } }