private static Namespace officeNs = Namespace.getNamespace("office", "urn:oasis:names:tc:opendocument:xmlns:office:1.0");
private static Namespace textNs = Namespace.getNamespace("text", "urn:oasis:names:tc:opendocument:xmlns:text:1.0");
public ConvertedDocumentBean convert(HttpServletRequest request, File aDocFile, String aTitle, String aMenuTextLength, List aCssList, String rewrite, String keepMenuExpanded)
{
ConvertedDocumentBean convertedDocument = new ConvertedDocumentBean();
int menuMaxLength = 20;
logger.info("-----------------------------------------------");
logger.info("Doc file: " + aDocFile.getAbsolutePath());
logger.info("Title: " + aTitle);
logger.info("Menu text length: " + aMenuTextLength);
logger.info("CSS list: " + aCssList);
logger.info("Rewrite: " + rewrite);
logger.info("-----------------------------------------------");
try
{
if (aTitle == null || aTitle.trim().equals(""))
{
aTitle = "Inneh�llsf�rteckning";
}
if (aMenuTextLength == null || aMenuTextLength.trim().equals(""))
{
aMenuTextLength = "20";
}
try
{
menuMaxLength = new Integer(aMenuTextLength).intValue();
}
catch(NumberFormatException nfe)
{
// Do nothing. Use the default value of 20 instead.
}
OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
//------------------------
// Setup the output files
//------------------------
String fileName = aDocFile.getName().substring(0, aDocFile.getName().indexOf("."));
int idIndex = fileName.indexOf("_");
String digitalAssetPath = CmsPropertyHandler.getDigitalAssetPath();
String folderName = "";
if(idIndex > -1)
{
String fileIdString = fileName.substring(0, idIndex);
int fileId = Integer.parseInt(fileIdString);
folderName = "" + (fileId / 1000);
digitalAssetPath = digitalAssetPath + File.separator + folderName;
}
//----------------------------------------
// Add the name of the folder to the path
//----------------------------------------
digitalAssetPath = digitalAssetPath + File.separator + fileName;
logger.info("Directory to write files to: " + digitalAssetPath);
//-------------------------------------------------
// Add a folder where the files are to be written.
//-------------------------------------------------
String newFilePath = digitalAssetPath + File.separator + fileName;
File pdfFile = new File(newFilePath + ".pdf");
File odtFile = new File(newFilePath + ".odt");
File htmlFile = new File(newFilePath + ".html");
File contentXmlFile = new File(digitalAssetPath + File.separator + "content.xml");;
//----------------------------------------------
// Check if the doc has already been converted.
// If it has, we don't need another conversion.
//----------------------------------------------
File documentDir = new File(digitalAssetPath);
if (!documentDir.exists() || rewrite.equals("true"))
{
logger.info("The directory " + digitalAssetPath + " does not exist. Creating it.");
documentDir.mkdir();
logger.info("The directory " + digitalAssetPath + " was successfully created.");
logger.info("Connecting to server...");
connection.connect();
logger.info("Connection ok");
logger.info("Conversion START");
convertDocument(aDocFile, htmlFile, connection);
if(!aDocFile.getName().substring(aDocFile.getName().indexOf(".") + 1).equalsIgnoreCase("odt"))
{
convertDocument(aDocFile, odtFile, connection);
}
else
{
odtFile = aDocFile;
}
convertDocument(aDocFile, pdfFile, connection);
connection.disconnect();
logger.info("Conversion END");
//------------------------------------------------
// Extract the content.xml file from the ODT file
// so we can parse the XML and generate the TOC
//------------------------------------------------
logger.info("Extracting content.xml...");
logger.info("odtFile: " + odtFile.getPath());
logger.info("target path: : " + digitalAssetPath);
contentXmlFile = extractContentXml(odtFile, digitalAssetPath);
logger.info("Done extracting content.xml");
//--------------------------------------------------------
// Insert the anchors, remove the TOC, remove CSS styles
// and add a link to the CMS-CSS to the the HTML handbook
// (Since we've just generated a new nav above)
//--------------------------------------------------------
logger.info("Updating handbook with extra info");
adaptHandbook(htmlFile, aCssList);
logger.info("Done updating handbook with extra info");
}
//--------------------------------
// Get the URL:s to the resources
//--------------------------------
logger.info("Extracting URL:s to resources.");
String digitalAssetUrl = CmsPropertyHandler.getDigitalAssetBaseUrl();
//----------------------------------------------------------------
// Add the contextPath to the URL to avoid problems with niceURIs.
//-----------------------------------------------------------------
String contextPath = request.getContextPath();
digitalAssetUrl = contextPath + "/" + digitalAssetUrl;
//--------------------------------
// Add the folder name to the URL
//--------------------------------
if (!folderName.equals(""))
{
digitalAssetUrl = digitalAssetUrl + "/" + folderName;
}
String htmlFileUrl = digitalAssetUrl + "/" + fileName + "/" + htmlFile.getName();
String pdfFileUrl = digitalAssetUrl + "/" + fileName + "/" + pdfFile.getName();
String odtFileUrl = digitalAssetUrl + "/" + fileName + "/" + odtFile.getName();
logger.info("htmlFileUrl: " + htmlFileUrl);
logger.info("pdfFileUrl: " + pdfFileUrl);
logger.info("odtFileUrl: " + odtFileUrl);
//--------------------------
// Generate HTML TOC string
//--------------------------
logger.info("Generating TOC...");
String tocString = generateHtmlToc(contentXmlFile, htmlFileUrl, aTitle, menuMaxLength, keepMenuExpanded);
logger.info("Done generating TOC");
convertedDocument.setHtmlFileUrl(htmlFileUrl);
convertedDocument.setPdfFileUrl(pdfFileUrl);
convertedDocument.setOdtFileUrl(odtFileUrl);
convertedDocument.setTocString(tocString);
}
catch(Exception e)
{
logger.error("An error occurred when converting document:" + e.getMessage(), e);
}