public String buildWordDoc()

in opennlp-similarity/src/main/java/opennlp/tools/similarity/apps/solr/WordDocBuilderEndNotes.java [37:146]


	public String buildWordDoc(List<HitBase> content, String title){
		
		String outputDocFilename =  absPath+"written/"+ title.replace(' ','_').replace('\"', ' ').trim()+ ".docx";
		
		WordprocessingMLPackage wordMLPackage;
		
       
		List<String> imageURLs = getAllImageSearchResults(title);
		int count=0;
		BigInteger refId = BigInteger.ONE;
		try {
			wordMLPackage = WordprocessingMLPackage.createPackage();

			CTEndnotes endnotes = null;
			try {
				EndnotesPart ep = new EndnotesPart();
				endnotes = Context.getWmlObjectFactory().createCTEndnotes();
				ep.setJaxbElement(endnotes);
				wordMLPackage.getMainDocumentPart().addTargetPart(ep);
			} catch (InvalidFormatException e1) {
				e1.printStackTrace();
			}
			
			wordMLPackage.getMainDocumentPart().addStyledParagraphOfText("Title", title.toUpperCase());
			for(HitBase para: content){
				if (para.getFragments()==null || para.getFragments().size()<1) // no found content in this hit
						continue;
				try {
					String processedParaTitle = processParagraphTitle(para.getTitle());
					
					if (processedParaTitle!=null && 
							!processedParaTitle.endsWith("..") || processedParaTitle.chars().allMatch(this::isAlphanumeric)){
						wordMLPackage.getMainDocumentPart().addStyledParagraphOfText("Subtitle",processedParaTitle);
					}
					String paraText = processParagraphText(para.getFragments().toString());
					wordMLPackage.getMainDocumentPart().addParagraphOfText(paraText);
					
					 CTFtnEdn endnote = Context.getWmlObjectFactory().createCTFtnEdn();
			         endnotes.getEndnote().add(endnote);
			        
			         endnote.setId(refId);
			         refId.add(BigInteger.ONE);
			         String url = para.getUrl();
			         String endnoteBody = "<w:p xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" ><w:pPr><w:pStyle w:val=\"EndnoteText\"/></w:pPr><w:r><w:rPr>" +
			         		"<w:rStyle w:val=\"EndnoteReference\"/></w:rPr><w:endnoteRef/></w:r><w:r><w:t xml:space=\"preserve\"> "+ url + "</w:t></w:r></w:p>";
			         try {
						endnote.getEGBlockLevelElts().add( XmlUtils.unmarshalString(endnoteBody));
					} catch (Exception e) {
						e.printStackTrace();
					}
			         
			         // Add the body text referencing it
			         String docBody = "<w:p xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" ><w:r><w:t>"//+ paraText
			         /*+ refId.toString()*/ +"</w:t></w:r><w:r><w:rPr><w:rStyle w:val=\"EndnoteReference\"/></w:rPr><w:endnoteReference w:id=\""+refId.toString()+"\"/></w:r></w:p>";
			         
			         try {
			        	 wordMLPackage.getMainDocumentPart().addParagraph(docBody);
					} catch (Exception e) {
						e.printStackTrace();
					}
					
					try {
						addImageByImageURLToPackage(count, wordMLPackage, imageURLs);
					} catch (Exception e) {
						// no need to report issues
						//e.printStackTrace();
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
				count++;
			}
			// now add URLs
			wordMLPackage.getMainDocumentPart().addStyledParagraphOfText("Subtitle", "REFERENCES");
			for(HitBase para: content){
				if (para.getFragments()==null || para.getFragments().size()<1) // no found content in this hit
						continue;
				try {
					wordMLPackage.getMainDocumentPart().addStyledParagraphOfText("Subtitle",
							para.getTitle());
					String paraText = para.getUrl();
					wordMLPackage.getMainDocumentPart().addParagraphOfText(paraText);
					
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
	
	        
			try {
				wordMLPackage.save(new File(outputDocFilename));
				System.out.println("Finished creating docx ="+outputDocFilename);
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			try {
				String fileNameToDownload = "/var/www/wrt_latest/"+title.replace(' ','_').replace('\"', ' ').trim()+ ".docx";
				wordMLPackage.save(new File(fileNameToDownload));
				System.out.println("Wrote a doc for download :"+fileNameToDownload);
			} catch (Exception e) {
				e.printStackTrace();
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return outputDocFilename;
	}