Example usage for org.apache.maven.model.io DefaultModelWriter write

List of usage examples for org.apache.maven.model.io DefaultModelWriter write

Introduction

In this page you can find the example usage for org.apache.maven.model.io DefaultModelWriter write.

Prototype

@Override
    public void write(OutputStream output, Map<String, Object> options, Model model) throws IOException 

Source Link

Usage

From source file:com.francetelecom.clara.cloud.mvn.consumer.maven.PomGenerator.java

License:Apache License

/**
 * Generate a pom.xml file from a MavenProject description
 * @return//from  w  ww.jav  a2 s.  c  om
 */
private String modelToStringXml(Model model) {

    DefaultModelWriter modelWriter = new DefaultModelWriter();

    StringWriter output = new StringWriter();
    String result = "";
    try {
        modelWriter.write(output, null, model);
        result = output.getBuffer().toString();
    } catch (IOException e) {
        logger.error("Cannot convert model to pom: " + e.getMessage());
        throw new TechnicalException("Cannot convert model to pom", e);
    }
    return result;
}

From source file:org.sonatype.maven.polyglot.groovy.GroovyModelReader.java

License:Open Source License

@Override
public Model read(final Reader input, final Map<String, ?> options) throws IOException {
    assert input != null;

    Model model;// www  .  j a  v  a  2  s .  c om

    try {
        model = doRead(input, options);
    } catch (Throwable t) {
        t = StackTraceUtils.sanitize(t);

        if (t instanceof IOException) {
            throw (IOException) t;
        }
        if (t instanceof RuntimeException) {
            throw (RuntimeException) t;
        }
        if (t instanceof Error) {
            throw (Error) t;
        }

        throw new RuntimeException(t);
    }

    // FIXME: Looks like there are cases where the model is loaded more than once
    executeManager.install(model, options);

    if (log.isDebugEnabled()) {
        DefaultModelWriter writer = new DefaultModelWriter();
        StringWriter buff = new StringWriter();
        writer.write(buff, null, model);
        log.debug("Read groovy model: \n" + buff);
    }

    return model;
}

From source file:org.sonatype.maven.polyglot.groovy.GroovyModelWriter.java

License:Open Source License

@Override
public void write(final Writer output, final Map<String, Object> options, final Model model)
        throws IOException {
    assert output != null;
    assert model != null;

    StringWriter buff = new StringWriter();
    DefaultModelWriter writer = new DefaultModelWriter();
    writer.write(buff, options, model);

    Dom2Groovy converter = new Dom2Groovy(new IndentPrinter(new PrintWriter(output), "  "));

    try {//from w  w w  .ja  v a 2s.  co  m
        DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        Document doc = builder.parse(new InputSource(new StringReader(buff.toString())));

        Element root = doc.getDocumentElement();
        NamedNodeMap attrs = root.getAttributes();
        for (int i = 0; i < attrs.getLength(); i++) {
            Attr attr = (Attr) attrs.item(i);
            root.removeAttribute(attr.getName());
        }
        // Not sure where this comes from but the above will not nuke it
        root.removeAttribute("xmlns:xsi");

        converter.print(doc);
        output.flush();
    } catch (ParserConfigurationException e) {
        throw (IOException) new IOException().initCause(e);
    } catch (SAXException e) {
        throw (IOException) new IOException().initCause(e);
    }
}