final PageSize size, final List hf, final Map properties,
final OutputStream out) throws CConvertException {
final List files = new ArrayList();
try {
final Tidy tidy = this.getTidy();
final CShaniDomParser parser = this.getCShaniDomParser();
final _ITextRenderer renderer = this.getITextRenderer();
final Reader r = CXmlParser.getReader(in);
final StringBuffer s = new StringBuffer();
final char[] buffer = new char[2048];
int inb = -1;
while ((inb = r.read(buffer)) != -1) {
s.append(buffer, 0, inb);
}
r.close();
ByteArrayOutputStream bout = new ByteArrayOutputStream();
tidy.parse(
new ByteArrayInputStream(s.toString().getBytes("utf-8")),
bout);
final String result = CHtmlToPdfFlyingSaucerTransformer
.removeScript(new String(bout.toByteArray(), "utf-8"));
Document theDoc = parser
.parse(new InputStreamReader(new ByteArrayInputStream(
result.getBytes("utf-8")), "utf-8"));
if (theDoc.toString().length() == 0) {
theDoc = parser.parse(new StringReader(
CHtmlToPdfFlyingSaucerTransformer.removeScript(s
.toString())));
}
this.convertInputToVisibleHTML(theDoc);
this.convertComboboxToVisibleHTML(theDoc);
this.convertTextAreaToVisibleHTML(theDoc);
final NodeList styles = theDoc.getElementsByTagName("style");
for (int i = 0; i < styles.getLength(); i++) {
final Node n = styles.item(i);
final StringBuffer style = new StringBuffer();
while (n.getChildNodes().getLength() > 0) {
final Node child = n.getChildNodes().item(0);
if (child.getNodeType() == Node.COMMENT_NODE) {
final Comment c = (Comment) child;
style.append(c.getData());
} else if (child.getNodeType() == Node.TEXT_NODE) {
final Text c = (Text) child;
style.append(c.getData());
} else if (child.getNodeType() == Node.CDATA_SECTION_NODE) {
final CDATASection c = (CDATASection) child;
style.append(c.getData());
}
n.removeChild(child);
}
final String content = style.toString().trim();
final Text start = theDoc.createTextNode("/*");
final CDATASection cd = theDoc.createCDATASection("*/\n"
+ content + "\n/*");
final Text end = theDoc.createTextNode("*/\n");
n.appendChild(start);
n.appendChild(cd);
n.appendChild(end);
}
final List toRemove = new ArrayList();
final NodeList tnl = theDoc.getChildNodes();
for (int i = 0; i < tnl.getLength(); i++) {
final Node n = tnl.item(i);
if (n != theDoc.getDocumentElement()) {
toRemove.add(n);
}
}
final Node title = theDoc.getDocumentElement()
.getElementsByTagName("title").item(0);
if ((title != null)
&& (properties.get(IHtmlToPdfTransformer.PDF_TITLE) == null)) {
properties.put(IHtmlToPdfTransformer.PDF_TITLE, title
.getTextContent());
}
Node body = theDoc.getDocumentElement()
.getElementsByTagName("body").item(0);
Node head = theDoc.getDocumentElement()
.getElementsByTagName("head").item(0);
for (int i = 0; i < toRemove.size(); i++) {
final Node n = (Node) toRemove.get(i);
n.getParentNode().removeChild(n);
if (n.getNodeType() == Node.TEXT_NODE) {
final Text t = (Text) n;
if (t.getData().trim().length() == 0) {
continue;
}
}
if ("link".equals(n.getNodeName())
|| "style".equals(n.getNodeName())) {
head.appendChild(n);
} else {
body.appendChild(n);
}
}
final DocumentAndSize docs[] = CDocumentCut.cut(theDoc, size);
for (int jj = 0; jj < docs.length; jj++) {
Document mydoc = docs[jj].doc;
body = mydoc.getDocumentElement().getElementsByTagName("body")
.item(0);
head = mydoc.getDocumentElement().getElementsByTagName("head")
.item(0);
try {
String surlForBase = ((Element) mydoc.getElementsByTagName(
"base").item(0)).getAttribute("href");
if ((surlForBase == null) || "".equals(surlForBase)) {
surlForBase = null;
}
if (surlForBase != null) {
urlForBase = surlForBase;
}
} catch (final Exception ignore) {
}
if (urlForBase != null) {
((ADocument) mydoc).setDocumentURI(urlForBase);
}
final NodeList nl = mydoc.getElementsByTagName("base");
if (nl.getLength() == 0) {
final ADocument doc = (ADocument) mydoc;
final Element base = doc.createElement("base");
base.setAttribute("href", urlForBase);
if (head.getFirstChild() != null) {
head.insertBefore(base, head.getFirstChild());
} // end if
else {
head.appendChild(base);
} // end else
} else {
final Element base = (Element) nl.item(0);
base.setAttribute("href", urlForBase);
}
/* STEF: remove this, because it works well without this code, and when enabled, CSS selectors
* separated by commas are ignored for some reason.
final NodeList linknl = mydoc.getElementsByTagName("link");
for (int zi=0;zi<linknl.getLength();zi++) {
try {
Element e = (Element)linknl.item(zi);
if ("stylesheet".equals(e.getAttribute("rel"))) {
String url = normalizeLink(urlForBase, e.getAttribute("href"));
InputStream iin = null;
try {
iin = new URL(url).openStream();
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
int iNbByteRead = -1;
byte bbuffer[] = new byte[16384];
while ((iNbByteRead = iin.read(bbuffer)) != -1) {
bOut.write(bbuffer, 0, iNbByteRead);
} // end while
String document = new String(bOut.toByteArray(), "utf-8");
CSSStyleSheet sheet = CCSSParser.parse(document, null,
null, url);
if (sheet != null) {
Element style = mydoc.createElement("style");
style.setAttribute("type", "text/css");
style.setTextContent(sheet.toString());
e.getParentNode().insertBefore(style, e);
e.getParentNode().removeChild(e);
}
} // end try
catch (final Throwable ignore) {
} // end catch
finally {
try{iin.close();}catch(Throwable ignore){}
}
}
}
catch (Throwable ignore){}
}
*/
final NumberFormat nf = NumberFormat.getInstance(Locale.US);
nf.setMaximumFractionDigits(2);
nf.setMinimumFractionDigits(0);
final Element style = mydoc.createElement("style");
style.setAttribute("type", "text/css");
final double[] dsize = docs[jj].size.getCMSize();
final double[] dmargin = docs[jj].size.getCMMargin();
style.setTextContent("\n@page {\n" + "size: "
+ nf.format(dsize[0] / 2.54) + "in "
+ nf.format(dsize[1] / 2.54) + "in;\n"
+ "margin-left: " + nf.format(dmargin[0] / 2.54)
+ "in;\n" + "margin-right: "
+ nf.format(dmargin[1] / 2.54) + "in;\n"
+ "margin-bottom: " + nf.format(dmargin[2] / 2.54)
+ "in;\n" + "margin-top: "
+ nf.format(dmargin[3] / 2.54) + "in;\npadding: 0in;\n"
+ "}\n"
);
head.appendChild(style);
if (properties.get(IHtmlToPdfTransformer.FOP_TTF_FONT_PATH) != null) {
final File dir = new File((String) properties
.get(IHtmlToPdfTransformer.FOP_TTF_FONT_PATH));
if (dir.isDirectory()) {
CHtmlToPdfFlyingSaucerTransformer.registerTTF(dir,
renderer);
}
}
((ADocument) mydoc).setInputEncoding("utf-8");
((ADocument) mydoc).setXmlEncoding("utf-8");
renderer.getSharedContext().setBaseURL(urlForBase);
mydoc = parser.parse(new StringReader(mydoc.toString()));
mydoc.getDomConfig().setParameter("entities", Boolean.FALSE );
mydoc.normalizeDocument();
renderer.setDocument(mydoc, urlForBase);
renderer.layout();
final java.io.File f = java.io.File.createTempFile("pdf",