IDEMPIERE-4893 NPE on PDF report of Test record (#1180)

- Fix NPE trying to print any HTML content in PDF as form using internal engine
- Fix problem printing HTML content as plain text
This commit is contained in:
Carlos Ruiz 2022-02-12 13:27:12 +01:00 committed by GitHub
parent 26aac62a4a
commit 69d11d0370
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 7 deletions

View File

@ -21,6 +21,7 @@ import java.awt.Rectangle;
import java.awt.geom.Point2D;
import java.util.Properties;
import java.util.logging.Level;
import java.util.regex.Pattern;
/**
* HTML Form Print ELement.
@ -141,13 +142,19 @@ public class HTMLElement extends PrintElement
{
if (content == null)
return false;
String s = content.toString();
if (s.length() < 20) // assumption
return false;
s = s.trim().toUpperCase();
if (s.startsWith("<HTML>"))
return true;
return false;
// code borrowed from https://denofdevelopers.com/how-to-detect-if-string-is-html-or-not-in-android/
final String TAG_START = "<\\w+((\\s+\\w+(\\s*=\\s*(?:\".*?\"|'.*?'|[^'\">\\s]+))?)+\\s*|\\s*)>";
final String TAG_END = "</\\w+>";
final String TAG_SELF_CLOSING = "<\\w+((\\s+\\w+(\\s*=\\s*(?:\".*?\"|'.*?'|[^'\">\\s]+))?)+\\s*|\\s*)/>";
final String HTML_ENTITY = "&[a-zA-Z][a-zA-Z0-9]+;";
final Pattern htmlPattern = Pattern
.compile("(" + TAG_START + ".*" + TAG_END + ")|(" + TAG_SELF_CLOSING + ")|(" + HTML_ENTITY + ")", Pattern.DOTALL);
boolean isHTML = false;
String htmlString = content.toString();
if (htmlString != null) {
isHTML = htmlPattern.matcher(htmlString).find();
}
return isHTML;
} // isHTML
} // HTMLElement

View File

@ -16,6 +16,7 @@
*****************************************************************************/
package org.compiere.print.layout;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.Shape;
@ -98,6 +99,7 @@ public class HTMLRenderer extends View implements Externalizable
m_factory = f;
m_view = v;
m_view.setParent(this);
m_container = new Container();
m_element = m_view.getElement();
// initially layout to the preferred size
setSize(m_view.getPreferredSpan(X_AXIS), m_view.getPreferredSpan(Y_AXIS));
@ -106,6 +108,7 @@ public class HTMLRenderer extends View implements Externalizable
private int m_width;
private View m_view;
private ViewFactory m_factory;
private Container m_container;
private Element m_element;
private Rectangle m_allocation;
private float m_viewWidth;
@ -436,4 +439,10 @@ public class HTMLRenderer extends View implements Externalizable
float height = in.readFloat();
setSize(width, height);
}
@Override
public Container getContainer() {
return m_container;
}
} // HTMLRenderer