WordExtractionHandler.java 26.5 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.dam.api.Asset
 *  com.day.cq.dam.api.Rendition
 *  com.day.cq.dam.api.RenditionPicker
 *  com.day.cq.dam.indd.AbstractPageExtractionHandler
 *  com.day.cq.dam.indd.PageBuilder
 *  com.day.cq.dam.indd.PageComponent
 *  com.day.cq.dam.indd.PageExtractionException
 *  com.day.cq.dam.indd.PageExtractionHandler
 *  com.day.cq.wcm.api.Page
 *  com.day.cq.wcm.api.WCMException
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Properties
 *  org.apache.felix.scr.annotations.Property
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.poi.hwpf.HWPFDocument
 *  org.apache.poi.hwpf.model.PicturesTable
 *  org.apache.poi.hwpf.usermodel.CharacterRun
 *  org.apache.poi.hwpf.usermodel.Paragraph
 *  org.apache.poi.hwpf.usermodel.Picture
 *  org.apache.poi.hwpf.usermodel.Range
 *  org.apache.poi.xwpf.usermodel.BodyElementType
 *  org.apache.poi.xwpf.usermodel.VerticalAlign
 *  org.apache.poi.xwpf.usermodel.XWPFDocument
 *  org.apache.poi.xwpf.usermodel.XWPFParagraph
 *  org.apache.poi.xwpf.usermodel.XWPFPicture
 *  org.apache.poi.xwpf.usermodel.XWPFPictureData
 *  org.apache.poi.xwpf.usermodel.XWPFRun
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ValueMap
 *  org.apache.sling.api.wrappers.ValueMapDecorator
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.dam.word.extraction;

import com.day.cq.dam.api.Asset;
import com.day.cq.dam.api.Rendition;
import com.day.cq.dam.api.RenditionPicker;
import com.day.cq.dam.indd.AbstractPageExtractionHandler;
import com.day.cq.dam.indd.PageBuilder;
import com.day.cq.dam.indd.PageComponent;
import com.day.cq.dam.indd.PageExtractionException;
import com.day.cq.dam.indd.PageExtractionHandler;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.WCMException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Service;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.model.PicturesTable;
import org.apache.poi.hwpf.usermodel.CharacterRun;
import org.apache.poi.hwpf.usermodel.Paragraph;
import org.apache.poi.hwpf.usermodel.Picture;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.xwpf.usermodel.BodyElementType;
import org.apache.poi.xwpf.usermodel.VerticalAlign;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFPicture;
import org.apache.poi.xwpf.usermodel.XWPFPictureData;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.api.wrappers.ValueMapDecorator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component
@Service(value={PageExtractionHandler.class})
@Properties(value={@Property(name="extractionhandler.label", value={"Word Extraction Handler"}, propertyPrivate=1), @Property(name="extractionhandler.description", value={"Extraction Handler for a MS Word files."}, propertyPrivate=1)})
public class WordExtractionHandler
extends AbstractPageExtractionHandler {
    private static final Logger log = LoggerFactory.getLogger(WordExtractionHandler.class);
    private static final String TEXT_COMPONENT = "foundation/components/text";
    private static final String IMAGE_COMPONENT = "foundation/components/image";
    private static final String TEXT_IMAGE_COMPONENT = "foundation/components/textimage";
    private static final int JUSTIFICATION_RIGHT = 2;
    private static final int JUSTIFICATION_CENTER = 1;
    private static final short TEXT_TYPE = 0;
    private static final short IMAGE_TYPE = 1;
    private static final short TEXT_IMAGE_TYPE = 2;

    public RenditionPicker getRenditionPicker() {
        return new RenditionPicker(){

            public Rendition getRendition(Asset asset) {
                if (asset == null) {
                    return null;
                }
                String assetMime = asset.getMimeType();
                if (assetMime.matches("application.*msword") || assetMime.equals("application/vnd.openxmlformats-officedocument.wordprocessingml.document")) {
                    return asset.getRendition("original");
                }
                return null;
            }
        };
    }

    public Page extractPage(Rendition rend, String pageRoot, String pageName, String pageTitle, String pageTemplate, String pageDesign) throws PageExtractionException {
        String rendMime = rend.getAsset().getMimeType();
        if (rendMime.equals("application/vnd.openxmlformats-officedocument.wordprocessingml.document")) {
            return this.extractDocx(rend, pageRoot, pageName, pageTitle, pageTemplate, pageDesign);
        }
        if (rendMime.matches("application.*msword")) {
            return this.extractDoc(rend, pageRoot, pageName, pageTitle, pageTemplate, pageDesign);
        }
        String msg = "No appropriate extractor found for: " + rend.getAsset().getName();
        log.info(msg);
        throw new PageExtractionException(msg);
    }

    private Page extractDoc(Rendition rend, String pageRoot, String pageName, String pageTitle, String pageTemplate, String pageDesign) throws PageExtractionException {
        log.info("Beginning page extraction from: " + rend.getAsset().getPath());
        int textIndex = 0;
        int textImageIndex = 0;
        int imageIndex = 0;
        ResourceResolver resourceResolver = rend.getResourceResolver();
        PageBuilder pageBuilder = this.getPageBuilder(resourceResolver);
        PageComponent par = null;
        String subAssetPath = rend.getAsset().getPath() + "/subassets";
        log.debug("Images in the page will point to the appropriate subassets located at: " + subAssetPath);
        try {
            HWPFDocument doc = new HWPFDocument(rend.getAsset().getOriginal().getStream());
            if (pageBuilder != null) {
                par = this.createParComponent(pageBuilder);
                Paragraph[] paragraphs = this.getParagraphs(doc.getRange());
                log.debug("Found " + paragraphs.length + " paragraphs in the document.");
                block7 : for (int i = 0; i < paragraphs.length; ++i) {
                    short type = this.getParagraphType(paragraphs[i], doc);
                    String nodeName = null;
                    switch (type) {
                        default: {
                            log.debug("Unknown paragraph type, will treat it as text only");
                        }
                        case 0: {
                            log.debug("Paragraph " + i + " contains only text.");
                            nodeName = "text_" + textIndex++;
                            PageComponent comp = this.createTextNode(paragraphs[i], pageBuilder, nodeName);
                            if (comp == null) continue block7;
                            par.getChildComponents().add(comp);
                            continue block7;
                        }
                        case 1: {
                            log.debug("Paragraph " + i + " contains only images.");
                            nodeName = "image_" + imageIndex++;
                            List<PageComponent> imageComps = this.createImageNode(paragraphs[i], pageBuilder, nodeName, subAssetPath, doc.getPicturesTable());
                            if (imageComps == null || imageComps.isEmpty()) continue block7;
                            par.getChildComponents().addAll(imageComps);
                            continue block7;
                        }
                        case 2: {
                            log.debug("Paragraph " + i + " contains both text and images.");
                            nodeName = "textImage_" + textImageIndex++;
                            List<PageComponent> textImageComps = this.createTextImageNode(paragraphs[i], pageBuilder, nodeName, subAssetPath, doc.getPicturesTable());
                            if (textImageComps == null || textImageComps.isEmpty()) continue block7;
                            par.getChildComponents().addAll(textImageComps);
                        }
                    }
                }
            }
            return this.buildPage(rend, pageRoot, pageName, pageTitle, pageTemplate, pageDesign, pageBuilder, par);
        }
        catch (Throwable e) {
            throw new PageExtractionException(e.getMessage(), e);
        }
    }

    private Page extractDocx(Rendition rend, String pageRoot, String pageName, String pageTitle, String pageTemplate, String pageDesign) throws PageExtractionException {
        log.info("Beginning page extraction from: " + rend.getAsset().getPath());
        int textIndex = 0;
        int textImageIndex = 0;
        int imageIndex = 0;
        ResourceResolver resourceResolver = rend.getResourceResolver();
        PageBuilder pageBuilder = this.getPageBuilder(resourceResolver);
        PageComponent par = null;
        String subAssetPath = rend.getAsset().getPath() + "/subassets";
        log.debug("Images in the page will point to the appropriate subassets located at: " + subAssetPath);
        try {
            XWPFDocument docx = new XWPFDocument(rend.getAsset().getOriginal().getStream());
            if (pageBuilder != null) {
                par = this.createParComponent(pageBuilder);
                Iterator paraIter = docx.getParagraphsIterator();
                while (paraIter.hasNext()) {
                    XWPFParagraph para = (XWPFParagraph)paraIter.next();
                    if (para.isEmpty() || para.isPageBreak()) {
                        log.debug("Empty paragraph found, ignoring.");
                        continue;
                    }
                    short paraType = this.getParagraphType(para);
                    String nodeName = null;
                    switch (paraType) {
                        case 0: {
                            log.debug("Current paragraph contains only text.");
                            nodeName = "text_" + textIndex++;
                            PageComponent comp = this.createTextNode(para, pageBuilder, nodeName);
                            if (comp == null) break;
                            par.getChildComponents().add(comp);
                            break;
                        }
                        case 1: {
                            log.debug("Current paragraph contains only images.");
                            nodeName = "image_" + imageIndex++;
                            List<PageComponent> imageComps = this.createImageNode(para, pageBuilder, nodeName, subAssetPath);
                            if (imageComps == null || imageComps.isEmpty()) break;
                            par.getChildComponents().addAll(imageComps);
                            break;
                        }
                        case 2: {
                            log.debug("Current paragraph contains both text and images.");
                            nodeName = "textImage_" + textImageIndex++;
                            List<PageComponent> textImageComps = this.createTextImageNode(para, pageBuilder, nodeName, subAssetPath);
                            if (textImageComps == null || textImageComps.isEmpty()) break;
                            par.getChildComponents().addAll(textImageComps);
                        }
                    }
                }
            }
            return this.buildPage(rend, pageRoot, pageName, pageTitle, pageTemplate, pageDesign, pageBuilder, par);
        }
        catch (Throwable e) {
            throw new PageExtractionException(e.getMessage(), e);
        }
    }

    private Page buildPage(Rendition rend, String pageRoot, String pageName, String pageTitle, String pageTemplate, String pageDesign, PageBuilder pageBuilder, PageComponent par) throws WCMException {
        ArrayList<PageComponent> pageComponents = new ArrayList<PageComponent>();
        pageComponents.add(par);
        Asset asset = rend.getAsset();
        String title = asset.getMetadataValue("dc:title");
        if (title != null && !title.trim().equalsIgnoreCase("")) {
            log.debug("Title found in the document metadata: " + title);
            pageTitle = title;
        } else {
            pageTitle = pageTitle + " (created: " + new Date() + ")";
            log.debug("No title found in the document metadata.  Using: " + pageTitle);
        }
        pageComponents.add(this.createTitleComponent(pageBuilder, pageTitle));
        Page page = pageBuilder.recreatePage(pageRoot, pageName, pageTitle, pageTemplate, pageDesign, pageComponents);
        log.info("Page extraction from: " + rend.getAsset().getPath() + " has completed successfully.");
        return page;
    }

    protected PageComponent createTitleComponent(PageBuilder pageBuilder, String pageTitle) {
        HashMap<String, String> properties = new HashMap<String, String>();
        properties.put("jcr:title", pageTitle);
        properties.put("type", "extralarge");
        return pageBuilder.createComponent("foundation/components/title", (ValueMap)new ValueMapDecorator(properties));
    }

    private List<PageComponent> createTextImageNode(Paragraph para, PageBuilder pageBuilder, String name, String subAssetPath, PicturesTable picTable) {
        int imageCount = 1;
        ArrayList<PageComponent> imageList = new ArrayList<PageComponent>();
        for (int i = 0; i < para.numCharacterRuns(); ++i) {
            CharacterRun run = para.getCharacterRun(i);
            if (!picTable.hasPicture(run)) continue;
            Picture pic = picTable.extractPicture(run, false);
            String path = subAssetPath + "/" + pic.suggestFullFileName();
            HashMap<String, String> props = new HashMap<String, String>();
            props.put("fileReference", path);
            if (imageList.isEmpty()) {
                imageList.add(pageBuilder.createComponent("foundation/components/image", (ValueMap)new ValueMapDecorator(props), "image"));
                continue;
            }
            imageList.add(pageBuilder.createComponent("foundation/components/image", (ValueMap)new ValueMapDecorator(props), "image_" + imageCount++));
        }
        log.debug("Found " + imageList.size() + " images.");
        HashMap<String, Object> props = new HashMap<String, Object>();
        props.put("textIsRich", true);
        props.put("text", this.renderParagraph(para));
        ArrayList<PageComponent> comps = new ArrayList<PageComponent>();
        if (!imageList.isEmpty()) {
            PageComponent textImageComp = pageBuilder.createComponent("foundation/components/textimage", (ValueMap)new ValueMapDecorator(props), name);
            textImageComp.getChildComponents().add(imageList.get(0));
            comps.add(textImageComp);
            for (int i2 = 1; i2 < imageList.size(); ++i2) {
                comps.add((PageComponent)imageList.get(i2));
            }
        } else {
            comps.add(pageBuilder.createComponent("foundation/components/text", (ValueMap)new ValueMapDecorator(props), name));
        }
        return comps;
    }

    private List<PageComponent> createTextImageNode(XWPFParagraph para, PageBuilder pageBuilder, String name, String subAssetPath) {
        int imageCount = 1;
        ArrayList<PageComponent> imageList = new ArrayList<PageComponent>();
        for (XWPFRun run : para.getRuns()) {
            if (run.getEmbeddedPictures().size() <= 0) continue;
            HashMap<String, String> propsImage = new HashMap<String, String>();
            Iterator pics = run.getEmbeddedPictures().iterator();
            while (pics.hasNext()) {
                XWPFPictureData pic = ((XWPFPicture)pics.next()).getPictureData();
                String path = subAssetPath + "/" + pic.getFileName();
                propsImage.put("fileReference", path);
                if (imageList.size() == 0) {
                    imageList.add(pageBuilder.createComponent("foundation/components/image", (ValueMap)new ValueMapDecorator(propsImage), "image"));
                    continue;
                }
                imageList.add(pageBuilder.createComponent("foundation/components/image", (ValueMap)new ValueMapDecorator(propsImage), "image_" + imageCount++));
            }
        }
        String text = this.renderParagraph(para);
        log.debug("Found " + imageList.size() + " images.");
        HashMap<String, Object> props = new HashMap<String, Object>();
        props.put("textIsRich", true);
        if (text != null && !text.trim().equalsIgnoreCase("") && !text.trim().equalsIgnoreCase("null")) {
            props.put("text", text);
        }
        ArrayList<PageComponent> components = new ArrayList<PageComponent>();
        PageComponent textComp = pageBuilder.createComponent("foundation/components/textimage", (ValueMap)new ValueMapDecorator(props), name);
        if (imageList.size() > 0) {
            PageComponent img = (PageComponent)imageList.get(0);
            textComp.getChildComponents().add(img);
            components.add(textComp);
            for (int i = 1; i < imageList.size(); ++i) {
                components.add((PageComponent)imageList.get(i));
            }
        } else {
            components.add(textComp);
        }
        return components;
    }

    private List<PageComponent> createImageNode(Paragraph para, PageBuilder pageBuilder, String name, String subAssetPath, PicturesTable picTable) {
        int count = 0;
        ArrayList<PageComponent> comps = new ArrayList<PageComponent>();
        for (int i = 0; i < para.numCharacterRuns(); ++i) {
            CharacterRun chars = para.getCharacterRun(i);
            if (!picTable.hasPicture(chars)) continue;
            Picture pic = picTable.extractPicture(chars, false);
            String path = subAssetPath + "/" + pic.suggestFullFileName();
            HashMap<String, String> props = new HashMap<String, String>();
            props.put("fileReference", path);
            comps.add(pageBuilder.createComponent("foundation/components/image", (ValueMap)new ValueMapDecorator(props), name + "_" + count++));
        }
        log.debug("Created " + comps.size() + "image components.");
        return comps;
    }

    private List<PageComponent> createImageNode(XWPFParagraph para, PageBuilder pageBuilder, String name, String subAssetPath) {
        int count = 0;
        ArrayList<PageComponent> comps = new ArrayList<PageComponent>();
        for (XWPFRun run : para.getRuns()) {
            if (run.getEmbeddedPictures().size() <= 0) continue;
            Iterator pics = run.getEmbeddedPictures().iterator();
            while (pics.hasNext()) {
                PageComponent comp = null;
                XWPFPictureData pic = ((XWPFPicture)pics.next()).getPictureData();
                String path = subAssetPath + "/" + pic.getFileName();
                HashMap<String, String> properties = new HashMap<String, String>();
                properties.put("fileReference", path);
                comp = pageBuilder.createComponent("foundation/components/image", (ValueMap)new ValueMapDecorator(properties), name + "_" + count++);
                comps.add(comp);
            }
        }
        log.debug("Created " + comps.size() + "image components.");
        return comps;
    }

    private PageComponent createTextNode(Paragraph para, PageBuilder pageBuilder, String name) {
        HashMap<String, Object> props = new HashMap<String, Object>();
        props.put("textIsRich", true);
        props.put("text", this.renderParagraph(para));
        return pageBuilder.createComponent("foundation/components/text", (ValueMap)new ValueMapDecorator(props), name);
    }

    private PageComponent createTextNode(XWPFParagraph para, PageBuilder pageBuilder, String name) {
        PageComponent comp = null;
        if (para.getElementType() == BodyElementType.PARAGRAPH) {
            String text = this.renderParagraph(para);
            if (text != null && !text.trim().equalsIgnoreCase("") && !text.trim().equalsIgnoreCase("null")) {
                log.debug("Paragraph text is: " + text);
                HashMap<String, Object> properties = new HashMap<String, Object>();
                properties.put("textIsRich", true);
                properties.put("text", text);
                comp = pageBuilder.createComponent("foundation/components/text", (ValueMap)new ValueMapDecorator(properties), name);
            }
        } else {
            log.warn("Non-PARAGRAPH type paragraph was found.  The type is: " + (Object)para.getElementType() + ". Skipping.");
        }
        return comp;
    }

    private void addOpenStyleTags(XWPFRun run, StringBuilder builder) {
        if (run.isBold()) {
            builder.append("<b>");
        }
        if (run.isItalic()) {
            builder.append("<i>");
        }
        if (run.isStrike()) {
            builder.append("<del>");
        }
        if (run.getSubscript() == VerticalAlign.SUBSCRIPT) {
            builder.append("<sub>");
        }
        if (run.getSubscript() == VerticalAlign.SUPERSCRIPT) {
            builder.append("<sup>");
        }
    }

    private void addCloseStyleTags(XWPFRun run, StringBuilder builder) {
        if (run.isBold()) {
            builder.append("</b>");
        }
        if (run.isItalic()) {
            builder.append("</i>");
        }
        if (run.isStrike()) {
            builder.append("</del>");
        }
        if (run.getSubscript() == VerticalAlign.SUBSCRIPT) {
            builder.append("</sub>");
        }
        if (run.getSubscript() == VerticalAlign.SUPERSCRIPT) {
            builder.append("</sup>");
        }
    }

    private int checkStyle(XWPFParagraph para) {
        String style = para.getStyle();
        if (style != null) {
            if (style.toLowerCase().startsWith("heading")) {
                int headingNum = -1;
                try {
                    headingNum = Integer.parseInt(style.substring(style.length() - 1));
                }
                catch (NumberFormatException e) {
                    // empty catch block
                }
                return headingNum;
            }
            return -1;
        }
        return -1;
    }

    private Paragraph[] getParagraphs(Range range) {
        Paragraph[] paragraphs = new Paragraph[range.numParagraphs()];
        for (int i = 0; i < paragraphs.length; ++i) {
            paragraphs[i] = range.getParagraph(i);
        }
        return paragraphs;
    }

    private short getParagraphType(XWPFParagraph para) {
        String paraText;
        boolean hasPictures = false;
        boolean hasText = false;
        for (XWPFRun run : para.getRuns()) {
            if (run.getEmbeddedPictures().size() <= 0) continue;
            hasPictures = true;
            break;
        }
        if ((paraText = para.getParagraphText()) != null && !paraText.trim().equals("") && !paraText.trim().startsWith("null")) {
            hasText = true;
        }
        if (hasPictures && hasText) {
            return 2;
        }
        if (hasPictures) {
            return 1;
        }
        return 0;
    }

    private short getParagraphType(Paragraph paragraph, HWPFDocument doc) {
        boolean hasPic = false;
        boolean hasText = false;
        for (int i = 0; i < paragraph.numCharacterRuns(); ++i) {
            CharacterRun characters = paragraph.getCharacterRun(i);
            if (doc.getPicturesTable().hasPicture(characters)) {
                hasPic = true;
                continue;
            }
            hasText |= paragraph.getCharacterRun(i).text() != null && paragraph.getCharacterRun(i).text().trim().length() > 0;
        }
        if (hasPic && !hasText) {
            return 1;
        }
        if (hasPic && hasText) {
            return 2;
        }
        return 0;
    }

    private String renderParagraph(Paragraph paragraph) {
        StringBuilder builder = new StringBuilder();
        if (paragraph.getJustification() == 2) {
            builder.append("<p style=\"text-align: right;\">");
        } else if (paragraph.getJustification() == 1) {
            builder.append("<p style=\"text-align: center;\">");
        } else {
            builder.append("<p>");
        }
        for (int i = 0; i < paragraph.numCharacterRuns(); ++i) {
            CharacterRun characters = paragraph.getCharacterRun(i);
            if (characters.isBold()) {
                builder.append("<b>");
            }
            if (characters.isItalic()) {
                builder.append("<i>");
            }
            for (char c : characters.text().toCharArray()) {
                if (c == '<') {
                    builder.append("&lt;");
                    continue;
                }
                if (c == '>') {
                    builder.append("&gt;");
                    continue;
                }
                if (c == '&') {
                    builder.append("&amp;");
                    continue;
                }
                builder.append(c);
            }
            if (characters.isItalic()) {
                builder.append("</i>");
            }
            if (!characters.isBold()) continue;
            builder.append("</b>");
        }
        builder.append("</p>");
        return builder.toString();
    }

    private String renderParagraph(XWPFParagraph para) {
        StringBuilder builder = new StringBuilder();
        if (log.isDebugEnabled()) {
            if (para.getText() == null) {
                log.debug("Para text is a null object");
            } else {
                log.debug("Para text is " + para.getText());
            }
        }
        if (para.getText() != null && !para.getText().trim().equalsIgnoreCase("") && !para.getText().trim().equalsIgnoreCase("null")) {
            builder.append("<p>");
            int style = this.checkStyle(para);
            if (style == -1 || style > 6) {
                for (XWPFRun run : para.getRuns()) {
                    if (run.getText(0) == null || run.getText(0).trim().equalsIgnoreCase("null")) continue;
                    this.addOpenStyleTags(run, builder);
                    builder.append(run.getText(0));
                    this.addCloseStyleTags(run, builder);
                }
            } else {
                builder.append("<H");
                builder.append(style);
                builder.append(">");
                builder.append(para.getText());
                builder.append("</H");
                builder.append(style);
                builder.append(">");
            }
            builder.append("</p>");
        }
        return builder.toString();
    }

}