List of usage examples for org.apache.poi.xwpf.usermodel XWPFParagraph getRuns
public List<XWPFRun> getRuns()
From source file:org.obeonetwork.m2doc.generator.UserContentRawCopy.java
License:Open Source License
/** * Test if first userContent content begin by a paragraph. * /*from w w w . jav a2s.com*/ * @param userContent * userContent EObject * @return true if userContent content begin by a paragraph */ private boolean userDocContentIsFirstRunOfParagraph(UserContent userContent) { boolean result = true; if (userContent.getBody() != null && userContent.getBody().getStatements().size() > 0 && userContent.getBody().getStatements().get(0).getRuns().size() > 0) { XWPFRun userContentFirstRun = userContent.getBody().getStatements().get(0).getRuns().get(0); XWPFParagraph userContentFirstRunParagraph = (XWPFParagraph) userContentFirstRun.getParent(); if (userContentFirstRunParagraph != null && userContentFirstRunParagraph.getRuns().size() > 0) { XWPFRun paragraphFirstRun = userContentFirstRunParagraph.getRuns().get(0); result = userContentFirstRun == paragraphFirstRun; } } return result; }
From source file:org.obeonetwork.m2doc.parser.BodyGeneratedParser.java
License:Open Source License
@Override protected Block parseBlock(TokenType... endTypes) throws DocumentParserException { final Block res = (Block) EcoreUtil.create(TemplatePackage.Literals.BLOCK); TokenType type = getNextTokenType(); List<TokenType> endTypeList = Lists.newArrayList(endTypes); endBlock: while (!endTypeList.contains(type)) { switch (type) { case USERCONTENT: res.getStatements().add(parseUserContent()); break; case ENDUSERCONTENT: // report the error and ignore the problem so that parsing // continues in other parts of the document. XWPFRun run = runIterator.lookAhead(1).getRun(); if (run == null) { throw new IllegalStateException( "Token of type " + type + " detected. Run shouldn't be null at this place."); }// ww w. j ava 2s . c om res.getValidationMessages().add(new TemplateValidationMessage(ValidationMessageLevel.ERROR, message(ParsingErrorMessage.UNEXPECTEDTAG, type.getValue()), run)); readTag(res, res.getRuns()); break; case EOF: final XWPFParagraph lastParagraph = document.getParagraphs() .get(document.getParagraphs().size() - 1); final XWPFRun lastRun = lastParagraph.getRuns().get(lastParagraph.getRuns().size() - 1); res.getValidationMessages() .add(new TemplateValidationMessage(ValidationMessageLevel.ERROR, message(ParsingErrorMessage.UNEXPECTEDTAGMISSING, type, Arrays.toString(endTypes)), lastRun)); break endBlock; case STATIC: res.getStatements().add(parseStaticFragment()); break; case WTABLE: res.getStatements().add(parseTable(runIterator.next().getTable())); break; default: throw new UnsupportedOperationException( String.format("Developer error: TokenType %s is not supported", type)); } type = getNextTokenType(); } return res; }
From source file:org.obeonetwork.m2doc.parser.BodyTemplateParser.java
License:Open Source License
@Override protected Block parseBlock(TokenType... endTypes) throws DocumentParserException { final Block res = (Block) EcoreUtil.create(TemplatePackage.Literals.BLOCK); TokenType type = getNextTokenType(); Set<TokenType> endTypeList = Sets.newHashSet(endTypes); endBlock: while (!endTypeList.contains(type)) { switch (type) { case AQL: res.getStatements().add(parseQuery()); break; case FOR: res.getStatements().add(parseRepetition()); break; case IF:/*www .j a va2 s .c om*/ res.getStatements().add(parseConditional()); break; case USERDOC: res.getStatements().add(parseUserDoc()); break; case ELSEIF: case ELSE: case ENDFOR: case ENDIF: case ENDLET: case ENDBOOKMARK: case ENDUSERDOC: // report the error and ignore the problem so that parsing // continues in other parts of the document. XWPFRun run = runIterator.lookAhead(1).getRun(); if (run == null) { throw new IllegalStateException( "Token of type " + type + " detected. Run shouldn't be null at this place."); } res.getValidationMessages().add(new TemplateValidationMessage(ValidationMessageLevel.ERROR, message(ParsingErrorMessage.UNEXPECTEDTAG, type.getValue()), run)); readTag(res, res.getRuns()); break; case EOF: final XWPFParagraph lastParagraph = document.getParagraphs() .get(document.getParagraphs().size() - 1); final XWPFRun lastRun = lastParagraph.getRuns().get(lastParagraph.getRuns().size() - 1); res.getValidationMessages() .add(new TemplateValidationMessage(ValidationMessageLevel.ERROR, message(ParsingErrorMessage.UNEXPECTEDTAGMISSING, type, Arrays.toString(endTypes)), lastRun)); break endBlock; case LET: res.getStatements().add(parseLet()); break; case IMAGE: res.getStatements().add(parseImage()); break; case STATIC: res.getStatements().add(parseStaticFragment()); break; case BOOKMARK: res.getStatements().add(parseBookmark()); break; case LINK: res.getStatements().add(parseLink()); break; case WTABLE: res.getStatements().add(parseTable(runIterator.next().getTable())); break; case DIAGRAM: res.getStatements().add(parseRepresentation()); // XXX : change representation into diagram in the template // model. break; case TABLE: res.getStatements().add(parseTableClient()); break; default: throw new UnsupportedOperationException( String.format("Developer error: TokenType %s is not supported", type)); } type = getNextTokenType(); } return res; }
From source file:org.obeonetwork.m2doc.parser.M2DocParser.java
License:Open Source License
@Override protected Block parseBlock(TokenType... endTypes) throws DocumentParserException { final Block res = (Block) EcoreUtil.create(TemplatePackage.Literals.BLOCK); TokenType type = getNextTokenType(); Set<TokenType> endTypeList = Sets.newHashSet(endTypes); endBlock: while (!endTypeList.contains(type)) { switch (type) { case AQL: res.getStatements().add(parseQuery()); break; case FOR: res.getStatements().add(parseRepetition()); break; case IF://from www. ja va 2s.co m res.getStatements().add(parseConditional()); break; case USERDOC: res.getStatements().add(parseUserDoc()); break; case COMMENT: res.getStatements().add(parseComment()); break; case ELSEIF: case ELSE: case ENDFOR: case ENDIF: case ENDLET: case ENDBOOKMARK: case ENDUSERDOC: // report the error and ignore the problem so that parsing // continues in other parts of the document. XWPFRun run = runIterator.lookAhead(1).getRun(); if (run == null) { throw new IllegalStateException( "Token of type " + type + " detected. Run shouldn't be null at this place."); } res.getValidationMessages().add(new TemplateValidationMessage(ValidationMessageLevel.ERROR, message(ParsingErrorMessage.UNEXPECTEDTAG, type.getValue()), run)); readTag(res, res.getRuns()); break; case EOF: final XWPFParagraph lastParagraph = document.getParagraphs() .get(document.getParagraphs().size() - 1); final XWPFRun lastRun = lastParagraph.getRuns().get(lastParagraph.getRuns().size() - 1); res.getValidationMessages() .add(new TemplateValidationMessage(ValidationMessageLevel.ERROR, message(ParsingErrorMessage.UNEXPECTEDTAGMISSING, type, Arrays.toString(endTypes)), lastRun)); break endBlock; case LET: res.getStatements().add(parseLet()); break; case IMAGE: res.getStatements().add(parseImage()); break; case STATIC: res.getStatements().add(parseStaticFragment()); break; case BOOKMARK: res.getStatements().add(parseBookmark()); break; case LINK: res.getStatements().add(parseLink()); break; case WTABLE: res.getStatements().add(parseTable(runIterator.next().getTable())); break; case DIAGRAM: res.getStatements().add(parseRepresentation()); // XXX : change representation into diagram in the template // model. break; case TABLE: res.getStatements().add(parseTableClient()); break; default: throw new UnsupportedOperationException( String.format("Developer error: TokenType %s is not supported", type)); } type = getNextTokenType(); } return res; }
From source file:org.obeonetwork.m2doc.parser.test.OptionParserTest.java
License:Open Source License
/** * Initialize needed elements for testing. * // ww w. java 2 s . c o m * @throws IOException * @throws InvalidFormatException */ @Before public void setUp() throws InvalidFormatException, IOException { optionParser = new OptionParser(); try (FileInputStream is = new FileInputStream("templates/allDiagram.docx")) { OPCPackage oPackage = OPCPackage.open(is); document = new XWPFDocument(oPackage); XWPFParagraph paragraph = document.getParagraphs().get(0); construct = TemplateFactory.eINSTANCE.createRepresentation(); construct.getRuns().add(paragraph.getRuns().get(0)); construct.setStyleRun(paragraph.getRuns().get(0)); } }
From source file:org.obeonetwork.m2doc.parser.TokenIterator.java
License:Open Source License
/** * Put the iterator in a state where it is finished or there's a token to * consumme in the currentIterator.//from w w w.j a va2 s. c om */ private void moveToNextToken() { if (tokenIterator == null || !tokenIterator.hasNext()) { while (elementIterator.hasNext() && (tokenIterator == null || !tokenIterator.hasNext())) { final IBodyElement element = elementIterator.next(); if (element.getElementType().equals(BodyElementType.PARAGRAPH)) { // create an empty run if there's no run in the paragraph. // this eases the processing of documents. The processing is based on runs and a paragraph that has no run in it won't // be seen by the generator and, as a consequence, won't be inserted as a static part in the result. XWPFParagraph paragraph = (XWPFParagraph) element; if (paragraph.getRuns().size() == 0) { paragraph.createRun().setText(""); } tokenIterator = new RunIterator(((XWPFParagraph) element).getRuns()); } else if (element.getElementType().equals(BodyElementType.TABLE)) { tokenIterator = new TableIterator((XWPFTable) element); } else { throw new UnsupportedOperationException( "Unsupported type of body element : " + element.getElementType()); } } } }
From source file:org.obeonetwork.m2doc.test.M2DocTestUtils.java
License:Open Source License
/** * Gets the {@link XWPFRun} containing the given text in the given {@link XWPFDocument}. * // www . ja v a 2 s . co m * @param document * the {@link XWPFDocument} * @param text * the {@link XWPFRun} * @return the {@link XWPFRun} containing the given text in the given {@link XWPFDocument} if any, <code>null</code> otherwise */ public static XWPFRun getRunContaining(XWPFDocument document, String text) { XWPFRun res = null; for (XWPFParagraph paragraph : document.getParagraphs()) { for (XWPFRun run : paragraph.getRuns()) { if (run.text().contains(text)) { res = run; break; } } } return res; }
From source file:org.obeonetwork.m2doc.tests.generator.DocumentGeneratorTests.java
License:Open Source License
@Test public void testBookmarkNominal() throws Exception { try (FileInputStream resIs = new FileInputStream( "resources/bookmark/nominal/nominal-expected-generation.docx"); OPCPackage resOPackage = OPCPackage.open(resIs); XWPFDocument resDocument = new XWPFDocument(resOPackage);) { assertEquals(5, resDocument.getBodyElements().size()); assertTrue(resDocument.getBodyElements().get(1) instanceof XWPFParagraph); XWPFParagraph paragraph = (XWPFParagraph) resDocument.getBodyElements().get(1); assertEquals(6, paragraph.getRuns().size()); assertEquals("Test link before bookmark: ", paragraph.getRuns().get(0).text()); final BigInteger id = new BigInteger(paragraph.getRuns().get(1).getCTR().getRsidR()); assertTrue(id != BigInteger.ZERO); assertEquals(1, paragraph.getRuns().get(1).getCTR().getFldCharList().size()); assertEquals(STFldCharType.BEGIN, paragraph.getRuns().get(1).getCTR().getFldCharList().get(0).getFldCharType()); assertEquals(id, new BigInteger(paragraph.getRuns().get(2).getCTR().getRsidR())); assertEquals(1, paragraph.getRuns().get(2).getCTR().getInstrTextList().size()); assertEquals(Space.PRESERVE, paragraph.getRuns().get(2).getCTR().getInstrTextList().get(0).getSpace()); assertEquals(" REF bookmark1 \\h ", paragraph.getRuns().get(2).getCTR().getInstrTextList().get(0).getStringValue()); assertEquals(id, new BigInteger(paragraph.getRuns().get(3).getCTR().getRsidR())); assertEquals(1, paragraph.getRuns().get(3).getCTR().getFldCharList().size()); assertEquals(STFldCharType.SEPARATE, paragraph.getRuns().get(3).getCTR().getFldCharList().get(0).getFldCharType()); assertEquals(id, new BigInteger(paragraph.getRuns().get(4).getCTR().getRsidR())); assertEquals("a reference to bookmark1", paragraph.getRuns().get(4).text()); assertEquals(id, new BigInteger(paragraph.getRuns().get(5).getCTR().getRsidR())); assertEquals(1, paragraph.getRuns().get(5).getCTR().getFldCharList().size()); assertEquals(STFldCharType.END,/*from w ww . ja va 2s . c om*/ paragraph.getRuns().get(5).getCTR().getFldCharList().get(0).getFldCharType()); assertTrue(resDocument.getBodyElements().get(1) instanceof XWPFParagraph); paragraph = (XWPFParagraph) resDocument.getBodyElements().get(2); assertEquals(1, paragraph.getCTP().getBookmarkStartList().size()); assertEquals("bookmark1", paragraph.getCTP().getBookmarkStartList().get(0).getName()); assertEquals(1, paragraph.getCTP().getBookmarkEndList().size()); assertTrue(resDocument.getBodyElements().get(2) instanceof XWPFParagraph); paragraph = (XWPFParagraph) resDocument.getBodyElements().get(3); assertEquals(7, paragraph.getRuns().size()); assertEquals("Test link after bookmark: ", paragraph.getRuns().get(0).text()); assertEquals(id, new BigInteger(paragraph.getRuns().get(1).getCTR().getRsidR())); assertEquals(1, paragraph.getRuns().get(1).getCTR().getFldCharList().size()); assertEquals(STFldCharType.BEGIN, paragraph.getRuns().get(1).getCTR().getFldCharList().get(0).getFldCharType()); assertEquals(id, new BigInteger(paragraph.getRuns().get(2).getCTR().getRsidR())); assertEquals(1, paragraph.getRuns().get(2).getCTR().getInstrTextList().size()); assertEquals(Space.PRESERVE, paragraph.getRuns().get(2).getCTR().getInstrTextList().get(0).getSpace()); assertEquals(" REF bookmark1 \\h ", paragraph.getRuns().get(2).getCTR().getInstrTextList().get(0).getStringValue()); assertEquals(id, new BigInteger(paragraph.getRuns().get(3).getCTR().getRsidR())); assertEquals(1, paragraph.getRuns().get(3).getCTR().getFldCharList().size()); assertEquals(STFldCharType.SEPARATE, paragraph.getRuns().get(3).getCTR().getFldCharList().get(0).getFldCharType()); assertEquals(id, new BigInteger(paragraph.getRuns().get(4).getCTR().getRsidR())); assertEquals("a reference to bookmark1", paragraph.getRuns().get(4).text()); assertEquals(id, new BigInteger(paragraph.getRuns().get(5).getCTR().getRsidR())); assertEquals(1, paragraph.getRuns().get(5).getCTR().getFldCharList().size()); assertEquals(STFldCharType.END, paragraph.getRuns().get(5).getCTR().getFldCharList().get(0).getFldCharType()); } }
From source file:org.obeonetwork.m2doc.tests.generator.TableClientProcessorWithStyleTests.java
License:Open Source License
@Test @Override//from w ww. j a va 2 s. co m public void testWithoutAnyParameter() throws ProviderException { super.testWithoutAnyParameter(); // Check styles List<XWPFTable> tables = doc.getTables(); XWPFTable table = tables.get(0); // First Row (header) XWPFTableRow row = table.getRow(0); // Header => col 1 has "column style" XWPFTableCell cell = row.getCell(1); XWPFParagraph cellParagraph = cell.getParagraphs().get(0); XWPFRun cellRun = cellParagraph.getRuns().get(0); assertEquals(COL_BG_COLOR, cell.getColor().toUpperCase()); assertEquals(COL_FG_COLOR, cellRun.getColor().toUpperCase()); assertEquals(COL_FONT_SIZE, cellRun.getFontSize()); assertTrue(cellRun.isBold()); assertFalse(cellRun.isItalic()); assertFalse(cellRun.isStrikeThrough()); assertEquals(UnderlinePatterns.NONE, cellRun.getUnderline()); // Header => no style cell = row.getCell(2); cellParagraph = cell.getParagraphs().get(0); cellRun = cellParagraph.getRuns().get(0); assertNull(cell.getColor()); assertNull(cellRun.getColor()); assertFalse(cellRun.isBold()); assertFalse(cellRun.isItalic()); assertFalse(cellRun.isStrikeThrough()); assertEquals(UnderlinePatterns.NONE, cellRun.getUnderline()); // First row row = table.getRow(1); // first row, row header => no style cell = row.getCell(0); cellParagraph = cell.getParagraphs().get(0); cellRun = cellParagraph.getRuns().get(0); assertNull(cell.getColor()); assertNull(cellRun.getColor()); assertFalse(cellRun.isBold()); assertFalse(cellRun.isItalic()); assertFalse(cellRun.isStrikeThrough()); assertEquals(UnderlinePatterns.NONE, cellRun.getUnderline()); // first row, first cell => cell style cell = row.getCell(1); cellParagraph = cell.getParagraphs().get(0); cellRun = cellParagraph.getRuns().get(0); assertEquals(ROW_BG_COLOR, cell.getColor().toUpperCase()); assertEquals(ROW_FG_COLOR, cellRun.getColor().toUpperCase()); assertEquals(ROW_FONT_SIZE, cellRun.getFontSize()); assertFalse(cellRun.isBold()); assertTrue(cellRun.isItalic()); assertFalse(cellRun.isStrikeThrough()); assertEquals(UnderlinePatterns.NONE, cellRun.getUnderline()); // first row, second cell => no style cell = row.getCell(2); cellParagraph = cell.getParagraphs().get(0); assertNull(cell.getColor()); cellRun = cellParagraph.getRuns().get(0); assertFalse(cellRun.isBold()); assertFalse(cellRun.isItalic()); assertFalse(cellRun.isStrikeThrough()); assertEquals(UnderlinePatterns.NONE, cellRun.getUnderline()); // Second row row = table.getRow(2); // second row, row header => no style cell = row.getCell(0); cellParagraph = cell.getParagraphs().get(0); cellRun = cellParagraph.getRuns().get(0); assertNull(cell.getColor()); assertNull(cellRun.getColor()); assertFalse(cellRun.isBold()); assertFalse(cellRun.isItalic()); assertFalse(cellRun.isStrikeThrough()); assertEquals(UnderlinePatterns.NONE, cellRun.getUnderline()); // second row, first cell => no style cell = row.getCell(1); cellParagraph = cell.getParagraphs().get(0); assertNull(cell.getColor()); cellRun = cellParagraph.getRuns().get(0); assertFalse(cellRun.isBold()); assertFalse(cellRun.isItalic()); assertFalse(cellRun.isStrikeThrough()); assertEquals(UnderlinePatterns.NONE, cellRun.getUnderline()); // second row, second cell => cell style cell = row.getCell(2); cellParagraph = cell.getParagraphs().get(0); cellRun = cellParagraph.getRuns().get(0); assertEquals(CELL_BG_COLOR, cell.getColor().toUpperCase()); assertEquals(CELL_FG_COLOR, cellRun.getColor().toUpperCase()); assertEquals(CELL_FONT_SIZE, cellRun.getFontSize()); assertTrue(cellRun.isBold()); assertFalse(cellRun.isItalic()); assertFalse(cellRun.isStrikeThrough()); assertEquals(UnderlinePatterns.SINGLE, cellRun.getUnderline()); }
From source file:org.obeonetwork.m2doc.util.M2DocUtils.java
License:Open Source License
/** * Inserts a new message {@link XWPFRun} after the given {@link XWPFRun}. * //from w ww. j a va 2 s .c om * @param run * the {@link XWPFRun} used as reference. * @param level * the {@link ValidationMessageLevel} * @param message * the message * @return the created {@link XWPFRun} */ public static XWPFRun insertMessageAfter(XWPFRun run, ValidationMessageLevel level, String message) { final XWPFRun res; final IRunBody parent = run.getParent(); if (parent instanceof XWPFParagraph) { final XWPFParagraph paragraph = (XWPFParagraph) parent; res = paragraph.insertNewRun(paragraph.getRuns().indexOf(run)); setRunMessage(res, level, message); } else { throw new IllegalStateException("this should not happend"); } return res; }