Example usage for org.apache.poi.xwpf.usermodel XWPFRun getCTR

List of usage examples for org.apache.poi.xwpf.usermodel XWPFRun getCTR

Introduction

In this page you can find the example usage for org.apache.poi.xwpf.usermodel XWPFRun getCTR.

Prototype

@Internal
public CTR getCTR() 

Source Link

Document

Get the currently used CTR object

Usage

From source file:org.obeonetwork.m2doc.generator.M2DocEvaluator.java

License:Open Source License

/**
 * Insert a new run containing the given text.
 * //from   w  ww .  j av  a  2s.c  om
 * @param srcRun
 *            the run to copy the style from.
 * @param fragment
 *            the text fragment to insert
 * @return the generated run.
 */
private XWPFRun insertFragment(XWPFRun srcRun, String fragment) {
    XWPFRun generatedRun = currentGeneratedParagraph.createRun();
    generatedRun.getCTR().set(srcRun.getCTR().copy());
    generatedRun.getCTR().getInstrTextList().clear();
    generatedRun.setText(fragment);
    return generatedRun;
}

From source file:org.obeonetwork.m2doc.generator.M2DocEvaluator.java

License:Open Source License

/**
 * Inserts the given {@link Object} in the given {@link XWPFRun}.
 * /*from   w w w. j  av a2  s.c  o  m*/
 * @param object
 *            the {@link Object} to insert
 * @param run
 *            the {@link XWPFRun}
 */
private void insertObject(Object object, XWPFRun run) {
    if (object instanceof Collection<?>) {
        for (Object child : (Collection<?>) object) {
            insertObject(child, run);
            // TODO insert Run ?
        }
    } else if (object instanceof MHyperLink) {
        final XWPFRun linkRun = insertFieldRunReplacement(run, "");
        insertMHyperLink(linkRun, (MHyperLink) object);
    } else if (object instanceof MBookmark) {
        insertMBookmark(run, (MBookmark) object);
    } else if (object instanceof MImage) {
        final XWPFRun imageRun = insertFieldRunReplacement(run, "");
        insertMImage(imageRun, (MImage) object);
    } else if (object instanceof MTable) {
        XWPFRun tableRun = run;
        tableRun.getCTR().getInstrTextList().clear();
        insertMTable(tableRun, (MTable) object);
    } else if (object instanceof MPagination) {
        insertMPagination(run, (MPagination) object);
    } else if (object == null) {
        insertFieldRunReplacement(run, "");
    } else {
        insertFieldRunReplacement(run, object.toString());
    }
}

From source file:org.obeonetwork.m2doc.generator.M2DocEvaluator.java

License:Open Source License

/**
 * Inserts the given {@link MHyperLink}.
 * // ww  w.  j  av  a  2 s . co m
 * @param run
 *            the {@link XWPFRun}
 * @param hyperLink
 *            the {@link MHyperLink}
 */
private void insertMHyperLink(XWPFRun run, MHyperLink hyperLink) {
    final String id = currentGeneratedParagraph.getDocument().getPackagePart()
            .addExternalRelationship(hyperLink.getUrl(), XWPFRelation.HYPERLINK.getRelation()).getId();
    final CTHyperlink cLink = currentGeneratedParagraph.getCTP().addNewHyperlink();
    cLink.setId(id);
    CTText ctText = CTText.Factory.newInstance();
    ctText.setStringValue(hyperLink.getText());

    CTR ctr = CTR.Factory.newInstance();
    ctr.setRPr((CTRPr) run.getCTR().getRPr().copy());
    ctr.setTArray(new CTText[] { ctText });
    cLink.setRArray(new CTR[] { ctr });
}

From source file:org.obeonetwork.m2doc.generator.M2DocEvaluator.java

License:Open Source License

@Override
public IConstruct caseImage(Image image) {
    XWPFRun imageRun = insertRun(image.getStyleRun());
    imageRun.setText("");
    imageRun.getCTR().getInstrTextList().clear();
    if (image.getFileName() == null) {
        insertQuerySyntaxMessages(image, "");
    } else {/*from   www  .  j  a  v a 2  s .  co m*/
        URI imageURI = URI.createFileURI(image.getFileName());
        imageURI = imageURI.resolve(image.eResource().getURI());
        try {
            int heigth = Units.toEMU(image.getHeight());
            int width = Units.toEMU(image.getWidth());

            try (InputStream imageStream = uriConverter.createInputStream(imageURI)) {
                imageRun.addPicture(imageStream, PictureType.toType(imageURI).getPoiType(), image.getFileName(),
                        width, heigth);
            }
        } catch (InvalidFormatException e) {
            insertMessage(currentGeneratedParagraph, ValidationMessageLevel.ERROR,
                    String.format(PICTURE_INVALID_FORMAT, imageURI.toString(), e.getMessage()));
        } catch (IOException e) {
            insertMessage(currentGeneratedParagraph, ValidationMessageLevel.ERROR,
                    String.format(AN_I_O_PROBLEM_OCCURED_WHILE_READING, imageURI.toString(), e.getMessage()));
        }
    }

    return image;
}

From source file:org.obeonetwork.m2doc.generator.M2DocEvaluator.java

License:Open Source License

@Override
public IConstruct caseRepresentation(Representation representation) {
    XWPFRun imageRun = insertRun(representation.getStyleRun());
    IProvider provider = representation.getProvider();
    if (provider == null) {
        insertMessage(currentGeneratedParagraph, ValidationMessageLevel.ERROR,
                representation.getValidationMessages().get(0).getMessage());
    } else {//from w  w  w. j  a v a  2  s .c o  m
        Map<String, Object> parameters;
        try {
            parameters = setupParametersMapForRepresentation(representation, provider);
            List<String> imagePaths = ((AbstractDiagramProvider) provider)
                    .getRepresentationImagePath(resourceSetForModels, parameters);
            usedProviders.add((AbstractDiagramProvider) provider);
            for (String imagePathStr : imagePaths) {
                URI imageURI = URI.createFileURI(imagePathStr);
                imageURI = imageURI.resolve(representation.eResource().getURI());

                imageRun.setText("");
                imageRun.getCTR().getInstrTextList().clear();

                final MImage image = new MImageImpl(uriConverter, imageURI);
                // get default image size if needed
                image.setConserveRatio(representation.getHeight() == 0 || representation.getWidth() == 0);
                if (representation.getHeight() != 0) {
                    image.setHeight(representation.getHeight());
                }
                if (representation.getWidth() != 0) {
                    image.setWidth(representation.getWidth());
                }
                insertMImage(imageRun, image);
            }
        } catch (IllegalArgumentException e) {
            insertMessage(currentGeneratedParagraph, ValidationMessageLevel.ERROR, e.getMessage());
        } catch (ProviderException e) {
            insertMessage(currentGeneratedParagraph, ValidationMessageLevel.ERROR,
                    "A problem occured while creating image from an diagram provider: " + e.getMessage());
        }

    }

    return representation;

}

From source file:org.obeonetwork.m2doc.generator.M2DocEvaluator.java

License:Open Source License

@Override
public IConstruct caseTableClient(TableClient tableClient) {
    XWPFRun tableRun = insertRun(tableClient.getStyleRun());
    tableRun.getCTR().getInstrTextList().clear();
    AbstractTableProvider provider = (AbstractTableProvider) tableClient.getProvider();
    if (provider == null) {
        insertMessage(currentGeneratedParagraph, ValidationMessageLevel.ERROR,
                tableClient.getValidationMessages().get(0).getMessage());
    } else {/*from  ww w  .ja v  a 2 s .  com*/
        Map<String, Object> parameters;
        try {
            parameters = setupParametersMap(tableClient, provider);
            TableClientProcessor tableProcessor = new TableClientProcessor(generatedDocument, provider,
                    parameters, resourceSetForModels);
            tableProcessor.generate(tableRun);
        } catch (IllegalArgumentException e) {
            insertMessage(currentGeneratedParagraph, ValidationMessageLevel.ERROR, e.getMessage());
        } catch (ProviderException e) {
            insertMessage(currentGeneratedParagraph, ValidationMessageLevel.ERROR,
                    "A problem occured while creating table from a table provider: " + e.getMessage());
        }
    }
    return tableClient;
}

From source file:org.obeonetwork.m2doc.generator.TemplateProcessor.java

License:Open Source License

/**
 * Inserts a run in the generated document. The new run is a copy from the specified run.
 * //from  w w w .  j  a  va  2  s  .com
 * @param srcRun
 *            the run to copy
 * @return the inserted run.
 */
@SuppressWarnings("deprecation")
private XWPFRun insertRun(XWPFRun srcRun) {
    if (srcRun.getParagraph() != currentTemplateParagraph || forceNewParagraph) {
        createNewParagraph(srcRun.getParagraph());
        forceNewParagraph = false;
    }
    XWPFRun generatedRun = currentGeneratedParagraph.createRun();
    generatedRun.getCTR().set(srcRun.getCTR());
    return generatedRun;
}

From source file:org.obeonetwork.m2doc.generator.TemplateProcessor.java

License:Open Source License

/**
 * Insert a new run for a fragment of text following a '\n' character.
 * //from   ww w .  j a v a 2s .c  o m
 * @param fragment
 *            the text fragment to insert
 * @param srcRun
 *            the run to copy the style from.
 * @return the generated run.
 */
private XWPFRun insertFragment(String fragment, XWPFRun srcRun) {
    XWPFRun generatedRun = currentGeneratedParagraph.createRun();
    generatedRun.getCTR().set(srcRun.getCTR().copy());
    generatedRun.getCTR().getInstrTextList().clear();
    generatedRun.setText(fragment);
    return generatedRun;
}

From source file:org.obeonetwork.m2doc.generator.TemplateProcessor.java

License:Open Source License

@Override
public AbstractConstruct caseImage(Image object) {
    XWPFRun imageRun = insertRun(object.getStyleRun());
    imageRun.setText("");
    imageRun.getCTR().getInstrTextList().clear();
    String filePath;//www .j  a v  a 2  s .  c  o m
    if ("".equals(this.rootProjectPath) || this.rootProjectPath == null) {
        filePath = object.getFileName();
    } else {
        filePath = this.rootProjectPath + "/" + object.getFileName();
    }
    try {
        int heigth = Units.toEMU(object.getHeight());
        int width = Units.toEMU(object.getWidth());
        imageRun.addPicture(new FileInputStream(filePath), getPictureType(object.getFileName()),
                object.getFileName(), width, heigth);
    } catch (InvalidFormatException e) {
        imageRun.setText("Picture in " + object.getFileName() + " has an invalid format.");
        imageRun.setBold(true);
        imageRun.setColor(ERROR_COLOR);
    } catch (FileNotFoundException e) {
        imageRun.setText("File " + filePath + " cannot be found.");
        imageRun.setBold(true);
        imageRun.setColor(ERROR_COLOR);
    } catch (IOException e) {
        imageRun.setText("An I/O Problem occured while reading file ");
        imageRun.setBold(true);
        imageRun.setColor(ERROR_COLOR);
    }
    return super.caseImage(object);
}

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.
 * /*from ww  w .j  a  va 2 s . c  om*/
 * @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;
}