List of usage examples for org.apache.poi.xwpf.usermodel XWPFParagraph insertNewRun
public XWPFRun insertNewRun(int pos)
From source file:com.deepoove.poi.resolver.TemplateResolver.java
License:Apache License
/** * running string Algorithm//from w w w . ja v a 2 s .co m * * @param paragraph * @return */ public static List<RunTemplate> parseRun(XWPFParagraph paragraph) { List<XWPFRun> runs = paragraph.getRuns(); if (null == runs || runs.isEmpty()) return null; String text = paragraph.getText(); logger.debug("Paragrah's text is:" + text); List<Pair<RunEdge, RunEdge>> pairs = new ArrayList<Pair<RunEdge, RunEdge>>(); List<String> tags = new ArrayList<String>(); calcTagPosInParagraph(text, pairs, tags); List<RunTemplate> rts = new ArrayList<RunTemplate>(); if (pairs.isEmpty()) return rts; RunTemplate runTemplate; calcRunPosInParagraph(runs, pairs); for (Pair<RunEdge, RunEdge> pai : pairs) { logger.debug(pai.getLeft().toString()); logger.debug(pai.getRight().toString()); } // split and merge Pair<RunEdge, RunEdge> pair2 = pairs.get(0); int length = pairs.size(); int tagIndex = length; for (int n = length - 1; n >= 0; n--) { pair2 = pairs.get(n); RunEdge left2 = pair2.getLeft(); RunEdge right2 = pair2.getRight(); int left_r = left2.getRunPos(); int right_r = right2.getRunPos(); int runEdge = left2.getRunEdge(); int runEdge2 = right2.getRunEdge(); String text1 = runs.get(left_r).getText(0); String text2 = runs.get(right_r).getText(0); if (runEdge2 + 1 >= text2.length()) { if (left_r != right_r) paragraph.removeRun(right_r); } else { String substring = text2.substring(runEdge2 + 1, text2.length()); if (left_r == right_r) { XWPFRun insertNewRun = paragraph.insertNewRun(right_r + 1); styleRun(insertNewRun, runs.get(right_r)); insertNewRun.setText(substring, 0); } else runs.get(right_r).setText(substring, 0); } for (int m = right_r - 1; m > left_r; m--) { paragraph.removeRun(m); } if (runEdge <= 0) { runs.get(left_r).setText(tags.get(--tagIndex), 0); runTemplate = parseRun(runs.get(left_r)); } else { String substring = text1.substring(0, runEdge); XWPFRun xwpfRun = runs.get(left_r); runs.get(left_r).setText(substring, 0); XWPFRun insertNewRun = paragraph.insertNewRun(left_r + 1); styleRun(insertNewRun, xwpfRun); insertNewRun.setText(tags.get(--tagIndex), 0); runTemplate = parseRun(runs.get(left_r + 1)); } if (null != runTemplate) { rts.add(runTemplate); } } return rts; }
From source file:org.obeonetwork.m2doc.generator.TemplateValidationGenerator.java
License:Open Source License
/** * Inserts the given {@link TemplateValidationMessage} in a run next to the one of the given construct subject to the message. * // ww w . j ava 2 s. co m * @param message * the {@link TemplateValidationMessage} to insert * the error message to insert in a run * @param offset * the offset in number of {@link XWPFRun} to insert after {@link TemplateValidationMessage#getLocation() message location} * @return the number of inserted {@link XWPFRun} */ private int addRunError(TemplateValidationMessage message, int offset) { int res = 0; if (message.getLocation().getParent() instanceof XWPFParagraph) { XWPFParagraph paragraph = (XWPFParagraph) message.getLocation().getParent(); final int basePosition = paragraph.getRuns().indexOf(message.getLocation()) + offset; // separation run. res++; final String color = M2DocUtils.getColor(message.getLevel()); XWPFRun newBlankRun = paragraph.insertNewRun(basePosition + res); newBlankRun.setText(BLANK_SEPARATOR); res++; final XWPFRun separationRun = paragraph.insertNewRun(basePosition + res); separationRun.setText(LOCATION_SEPARATOR); separationRun.setColor(color); separationRun.setFontSize(ERROR_MESSAGE_FONT_SIZE); final CTHighlight separationHighlight = separationRun.getCTR().getRPr().addNewHighlight(); separationHighlight.setVal(STHighlightColor.LIGHT_GRAY); res++; final XWPFRun messageRun = paragraph.insertNewRun(basePosition + res); messageRun.setText(message.getMessage()); messageRun.setColor(color); messageRun.setFontSize(ERROR_MESSAGE_FONT_SIZE); final CTHighlight messageHighlight = messageRun.getCTR().getRPr().addNewHighlight(); messageHighlight.setVal(STHighlightColor.LIGHT_GRAY); } return res; }
From source file:org.obeonetwork.m2doc.util.M2DocUtils.java
License:Open Source License
/** * Inserts a new message {@link XWPFRun} after the given {@link XWPFRun}. * //w w w . j a v a2 s. co m * @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; }