Search in sources :

Example 1 with DocumentXpp3Reader

use of org.apache.maven.doxia.document.io.xpp3.DocumentXpp3Reader in project maven-plugins by apache.

the class DocumentDescriptorReader method readAndFilterDocumentDescriptor.

/**
 * Read and filter the <code>docDescriptor</code> file.
 *
 * @param docDescriptor not null, corresponding to non-localized descriptor file.
 * @return a DocumentModel instance.
 * @throws XmlPullParserException if an error occurs during parsing.
 * @throws IOException if an error occurs during reading.
 */
public DocumentModel readAndFilterDocumentDescriptor(File docDescriptor) throws XmlPullParserException, IOException {
    if (locale != null) {
        String descriptorFilename = docDescriptor.getName();
        String localized = FileUtils.removeExtension(descriptorFilename) + '_' + locale.getLanguage() + '.' + FileUtils.getExtension(descriptorFilename);
        File localizedDocDescriptor = new File(docDescriptor.getParentFile(), localized);
        if (localizedDocDescriptor.exists()) {
            docDescriptor = localizedDocDescriptor;
        }
    }
    Reader reader = null;
    try {
        // System properties
        final Properties filterProperties = System.getProperties();
        // Project properties
        if (project != null && project.getProperties() != null) {
            filterProperties.putAll(project.getProperties());
        }
        final Interpolator interpolator = new RegexBasedInterpolator();
        interpolator.addValueSource(new MapBasedValueSource(filterProperties));
        interpolator.addValueSource(new EnvarBasedValueSource());
        interpolator.addValueSource(new ObjectBasedValueSource(project) {

            /**
             * {@inheritDoc}
             */
            public Object getValue(final String expression) {
                try {
                    return ReflectionValueExtractor.evaluate(expression, project);
                } catch (Exception e) {
                    addFeedback("Failed to extract \'" + expression + "\' from: " + project, e);
                }
                return null;
            }
        });
        final DateBean bean = new DateBean();
        interpolator.addValueSource(new ObjectBasedValueSource(bean));
        reader = ReaderFactory.newXmlReader(docDescriptor);
        final String interpolatedDoc = interpolator.interpolate(IOUtil.toString(reader));
        reader.close();
        reader = null;
        if (log != null && log.isDebugEnabled()) {
            log.debug("Interpolated document descriptor (" + docDescriptor.getAbsolutePath() + ")\n" + interpolatedDoc);
        }
        // No Strict
        return new DocumentXpp3Reader().read(new StringReader(interpolatedDoc), false);
    } catch (InterpolationException e) {
        final IOException io = new IOException("Error interpolating document descriptor");
        io.initCause(e);
        throw io;
    } finally {
        IOUtil.close(reader);
    }
}
Also used : RegexBasedInterpolator(org.codehaus.plexus.interpolation.RegexBasedInterpolator) DocumentXpp3Reader(org.apache.maven.doxia.document.io.xpp3.DocumentXpp3Reader) Reader(java.io.Reader) StringReader(java.io.StringReader) InterpolationException(org.codehaus.plexus.interpolation.InterpolationException) IOException(java.io.IOException) Properties(java.util.Properties) XmlPullParserException(org.codehaus.plexus.util.xml.pull.XmlPullParserException) IOException(java.io.IOException) InterpolationException(org.codehaus.plexus.interpolation.InterpolationException) DocumentXpp3Reader(org.apache.maven.doxia.document.io.xpp3.DocumentXpp3Reader) StringReader(java.io.StringReader) Interpolator(org.codehaus.plexus.interpolation.Interpolator) RegexBasedInterpolator(org.codehaus.plexus.interpolation.RegexBasedInterpolator) ObjectBasedValueSource(org.codehaus.plexus.interpolation.ObjectBasedValueSource) File(java.io.File) MapBasedValueSource(org.codehaus.plexus.interpolation.MapBasedValueSource) EnvarBasedValueSource(org.codehaus.plexus.interpolation.EnvarBasedValueSource)

Aggregations

File (java.io.File)1 IOException (java.io.IOException)1 Reader (java.io.Reader)1 StringReader (java.io.StringReader)1 Properties (java.util.Properties)1 DocumentXpp3Reader (org.apache.maven.doxia.document.io.xpp3.DocumentXpp3Reader)1 EnvarBasedValueSource (org.codehaus.plexus.interpolation.EnvarBasedValueSource)1 InterpolationException (org.codehaus.plexus.interpolation.InterpolationException)1 Interpolator (org.codehaus.plexus.interpolation.Interpolator)1 MapBasedValueSource (org.codehaus.plexus.interpolation.MapBasedValueSource)1 ObjectBasedValueSource (org.codehaus.plexus.interpolation.ObjectBasedValueSource)1 RegexBasedInterpolator (org.codehaus.plexus.interpolation.RegexBasedInterpolator)1 XmlPullParserException (org.codehaus.plexus.util.xml.pull.XmlPullParserException)1