diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/io/DefaultModelReader.java b/maven-model-builder/src/main/java/org/apache/maven/model/io/DefaultModelReader.java new file mode 100644 index 0000000000..e6800a327e --- /dev/null +++ b/maven-model-builder/src/main/java/org/apache/maven/model/io/DefaultModelReader.java @@ -0,0 +1,108 @@ +package org.apache.maven.model.io; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.Reader; +import java.util.Map; + +import org.apache.maven.model.Model; +import org.apache.maven.model.io.xpp3.MavenXpp3Reader; +import org.codehaus.plexus.component.annotations.Component; +import org.codehaus.plexus.util.IOUtil; +import org.codehaus.plexus.util.ReaderFactory; +import org.codehaus.plexus.util.xml.pull.XmlPullParserException; + +/** + * Handles deserialization of a model from some kind of textual format like XML. + * + * @author Benjamin Bentmann + */ +@Component( role = ModelReader.class ) +public class DefaultModelReader + implements ModelReader +{ + + public Model read( File input, Map options ) + throws IOException + { + if ( input == null ) + { + throw new IllegalArgumentException( "input file missing" ); + } + + return read( ReaderFactory.newXmlReader( input ), options ); + } + + public Model read( Reader input, Map options ) + throws IOException + { + if ( input == null ) + { + throw new IllegalArgumentException( "input reader missing" ); + } + + try + { + MavenXpp3Reader r = new MavenXpp3Reader(); + return r.read( input, isStrict( options ) ); + } + catch ( XmlPullParserException e ) + { + throw (IOException) new IOException( "Failed to parse POM" ).initCause( e ); + } + finally + { + IOUtil.close( input ); + } + } + + public Model read( InputStream input, Map options ) + throws IOException + { + if ( input == null ) + { + throw new IllegalArgumentException( "input stream missing" ); + } + + try + { + MavenXpp3Reader r = new MavenXpp3Reader(); + return r.read( input, isStrict( options ) ); + } + catch ( XmlPullParserException e ) + { + throw (IOException) new IOException( "Failed to parse POM" ).initCause( e ); + } + finally + { + IOUtil.close( input ); + } + } + + private boolean isStrict( Map options ) + { + Object value = ( options != null ) ? options.get( IS_STRICT ) : null; + return value == null || Boolean.parseBoolean( value.toString() ); + } + +} diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/io/DefaultModelWriter.java b/maven-model-builder/src/main/java/org/apache/maven/model/io/DefaultModelWriter.java new file mode 100644 index 0000000000..7813483cdd --- /dev/null +++ b/maven-model-builder/src/main/java/org/apache/maven/model/io/DefaultModelWriter.java @@ -0,0 +1,115 @@ +package org.apache.maven.model.io; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import java.io.File; +import java.io.IOException; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.io.Writer; +import java.util.Map; + +import org.apache.maven.model.Model; +import org.apache.maven.model.io.xpp3.MavenXpp3Writer; +import org.codehaus.plexus.component.annotations.Component; +import org.codehaus.plexus.util.IOUtil; +import org.codehaus.plexus.util.WriterFactory; + +/** + * Handles serialization of a model into some kind of textual format like XML. + * + * @author Benjamin Bentmann + */ +@Component( role = ModelWriter.class ) +public class DefaultModelWriter + implements ModelWriter +{ + + public void write( File output, Map options, Model model ) + throws IOException + { + if ( output == null ) + { + throw new IllegalArgumentException( "output file missing" ); + } + + if ( model == null ) + { + throw new IllegalArgumentException( "model missing" ); + } + + output.getParentFile().mkdirs(); + + write( WriterFactory.newXmlWriter( output ), options, model ); + } + + public void write( Writer output, Map options, Model model ) + throws IOException + { + if ( output == null ) + { + throw new IllegalArgumentException( "output writer missing" ); + } + + if ( model == null ) + { + throw new IllegalArgumentException( "model missing" ); + } + + try + { + MavenXpp3Writer w = new MavenXpp3Writer(); + w.write( output, model ); + } + finally + { + IOUtil.close( output ); + } + } + + public void write( OutputStream output, Map options, Model model ) + throws IOException + { + if ( output == null ) + { + throw new IllegalArgumentException( "output stream missing" ); + } + + if ( model == null ) + { + throw new IllegalArgumentException( "model missing" ); + } + + try + { + String encoding = model.getModelEncoding(); + if ( encoding == null || encoding.length() <= 0 ) + { + encoding = "UTF-8"; + } + write( new OutputStreamWriter( output, encoding ), options, model ); + } + finally + { + IOUtil.close( output ); + } + } + +} diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/io/ModelReader.java b/maven-model-builder/src/main/java/org/apache/maven/model/io/ModelReader.java new file mode 100644 index 0000000000..76e2418e82 --- /dev/null +++ b/maven-model-builder/src/main/java/org/apache/maven/model/io/ModelReader.java @@ -0,0 +1,77 @@ +package org.apache.maven.model.io; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.Reader; +import java.util.Map; + +import org.apache.maven.model.Model; + +/** + * Handles deserialization of a model from some kind of textual format like XML. + * + * @author Benjamin Bentmann + */ +public interface ModelReader +{ + + /** + * The key for the option to enable strict parsing. This option is of type {@link Boolean} and defaults to {@code + * true}. If {@code false}, unknown elements will be ignored instead of causing a failure. + */ + static final String IS_STRICT = "org.apache.maven.model.io.isStrict"; + + /** + * Reads the model from the specified file. + * + * @param input The file to deserialize the model from, must not be {@code null}. + * @param options The options to use for deserialization, may be {@code null} to use the default values. + * @return The deserialized model, never {@code null}. + * @throws IOException If the model could not be deserialized. + */ + Model read( File input, Map options ) + throws IOException; + + /** + * Reads the model from the specified character reader. + * + * @param input The reader to deserialize the model from, must not be {@code null}. + * @param options The options to use for deserialization, may be {@code null} to use the default values. + * @return The deserialized model, never {@code null}. + * @throws IOException If the model could not be deserialized. + */ + Model read( Reader input, Map options ) + throws IOException; + + /** + * Reads the model from the specified byte stream. + * + * @param input The stream to deserialize the model from, must not be {@code null}. + * @param options The options to use for deserialization, may be {@code null} to use the default values. + * @return The deserialized model, never {@code null}. + * @throws IOException If the model could not be deserialized. + */ + Model read( InputStream input, Map options ) + throws IOException; + +} diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/io/ModelWriter.java b/maven-model-builder/src/main/java/org/apache/maven/model/io/ModelWriter.java new file mode 100644 index 0000000000..207385b062 --- /dev/null +++ b/maven-model-builder/src/main/java/org/apache/maven/model/io/ModelWriter.java @@ -0,0 +1,72 @@ +package org.apache.maven.model.io; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import java.io.File; +import java.io.IOException; +import java.io.OutputStream; +import java.io.Writer; +import java.util.Map; + +import org.apache.maven.model.Model; + +/** + * Handles serialization of a model into some kind of textual format like XML. + * + * @author Benjamin Bentmann + */ +public interface ModelWriter +{ + + /** + * Writes the supplied model to the specified file. Any non-existing parent directories of the output file will be + * created automatically. + * + * @param output The file to serialize the model to, must not be {@code null}. + * @param options The options to use for serialization, may be {@code null} to use the default values. + * @param model The model to serialize, must not be {@code null}. + * @throws IOException If the model could not be serialized. + */ + void write( File output, Map options, Model model ) + throws IOException; + + /** + * Writes the supplied model to the specified character writer. + * + * @param output The writer to serialize the model to, must not be {@code null}. + * @param options The options to use for serialization, may be {@code null} to use the default values. + * @param model The model to serialize, must not be {@code null}. + * @throws IOException If the model could not be serialized. + */ + void write( Writer output, Map options, Model model ) + throws IOException; + + /** + * Writes the supplied model to the specified byte stream. + * + * @param output The stream to serialize the model to, must not be {@code null}. + * @param options The options to use for serialization, may be {@code null} to use the default values. + * @param model The model to serialize, must not be {@code null}. + * @throws IOException If the model could not be serialized. + */ + void write( OutputStream output, Map options, Model model ) + throws IOException; + +}