o Added parser-neutral facades for the Modello generated settings reader/writer

git-svn-id: https://svn.apache.org/repos/asf/maven/maven-3/trunk@824168 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Benjamin Bentmann 2009-10-11 21:22:35 +00:00
parent 4344a147a4
commit 00fa4413d2
5 changed files with 474 additions and 0 deletions

View File

@ -0,0 +1,110 @@
package org.apache.maven.settings.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.settings.Settings;
import org.apache.maven.settings.io.xpp3.SettingsXpp3Reader;
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 settings from some kind of textual format like XML.
*
* @author Benjamin Bentmann
*/
@Component( role = SettingsReader.class )
public class DefaultSettingsReader
implements SettingsReader
{
public Settings read( File input, Map<String, ?> options )
throws IOException
{
if ( input == null )
{
throw new IllegalArgumentException( "input file missing" );
}
Settings settings = read( ReaderFactory.newXmlReader( input ), options );
return settings;
}
public Settings read( Reader input, Map<String, ?> options )
throws IOException
{
if ( input == null )
{
throw new IllegalArgumentException( "input reader missing" );
}
try
{
SettingsXpp3Reader r = new SettingsXpp3Reader();
return r.read( input, isStrict( options ) );
}
catch ( XmlPullParserException e )
{
throw new SettingsParseException( e.getMessage(), e.getLineNumber(), e.getColumnNumber(), e );
}
finally
{
IOUtil.close( input );
}
}
public Settings read( InputStream input, Map<String, ?> options )
throws IOException
{
if ( input == null )
{
throw new IllegalArgumentException( "input stream missing" );
}
try
{
SettingsXpp3Reader r = new SettingsXpp3Reader();
return r.read( input, isStrict( options ) );
}
catch ( XmlPullParserException e )
{
throw new SettingsParseException( e.getMessage(), e.getLineNumber(), e.getColumnNumber(), e );
}
finally
{
IOUtil.close( input );
}
}
private boolean isStrict( Map<String, ?> options )
{
Object value = ( options != null ) ? options.get( IS_STRICT ) : null;
return value == null || Boolean.parseBoolean( value.toString() );
}
}

View File

@ -0,0 +1,115 @@
package org.apache.maven.settings.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.settings.Settings;
import org.apache.maven.settings.io.xpp3.SettingsXpp3Writer;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.WriterFactory;
/**
* Handles serialization of settings into some kind of textual format like XML.
*
* @author Benjamin Bentmann
*/
@Component( role = SettingsWriter.class )
public class DefaultSettingsWriter
implements SettingsWriter
{
public void write( File output, Map<String, Object> options, Settings settings )
throws IOException
{
if ( output == null )
{
throw new IllegalArgumentException( "output file missing" );
}
if ( settings == null )
{
throw new IllegalArgumentException( "settings missing" );
}
output.getParentFile().mkdirs();
write( WriterFactory.newXmlWriter( output ), options, settings );
}
public void write( Writer output, Map<String, Object> options, Settings settings )
throws IOException
{
if ( output == null )
{
throw new IllegalArgumentException( "output writer missing" );
}
if ( settings == null )
{
throw new IllegalArgumentException( "settings missing" );
}
try
{
SettingsXpp3Writer w = new SettingsXpp3Writer();
w.write( output, settings );
}
finally
{
IOUtil.close( output );
}
}
public void write( OutputStream output, Map<String, Object> options, Settings settings )
throws IOException
{
if ( output == null )
{
throw new IllegalArgumentException( "output stream missing" );
}
if ( settings == null )
{
throw new IllegalArgumentException( "settings missing" );
}
try
{
String encoding = settings.getModelEncoding();
if ( encoding == null || encoding.length() <= 0 )
{
encoding = "UTF-8";
}
write( new OutputStreamWriter( output, encoding ), options, settings );
}
finally
{
IOUtil.close( output );
}
}
}

View File

@ -0,0 +1,93 @@
package org.apache.maven.settings.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.IOException;
/**
* Signals a failure to parse the settings due to invalid syntax (e.g. non-wellformed XML or unknown elements).
*
* @author Benjamin Bentmann
*/
public class SettingsParseException
extends IOException
{
/**
* The one-based index of the line containing the error.
*/
private final int lineNumber;
/**
* The one-based index of the column containing the error.
*/
private final int columnNumber;
/**
* Creates a new parser exception with the specified details.
*
* @param message The error message, may be {@code null}.
* @param lineNumber The one-based index of the line containing the error or {@code -1} if unknown.
* @param columnNumber The one-based index of the column containing the error or {@code -1} if unknown.
*/
public SettingsParseException( String message, int lineNumber, int columnNumber )
{
super( message );
this.lineNumber = lineNumber;
this.columnNumber = columnNumber;
}
/**
* Creates a new parser exception with the specified details.
*
* @param message The error message, may be {@code null}.
* @param lineNumber The one-based index of the line containing the error or {@code -1} if unknown.
* @param columnNumber The one-based index of the column containing the error or {@code -1} if unknown.
* @param cause The nested cause of this error, may be {@code null}.
*/
public SettingsParseException( String message, int lineNumber, int columnNumber, Throwable cause )
{
super( message );
initCause( cause );
this.lineNumber = lineNumber;
this.columnNumber = columnNumber;
}
/**
* Gets the one-based index of the line containing the error.
*
* @return The one-based index of the line containing the error or a non-positive value if unknown.
*/
public int getLineNumber()
{
return lineNumber;
}
/**
* Gets the one-based index of the column containing the error.
*
* @return The one-based index of the column containing the error or non-positive value if unknown.
*/
public int getColumnNumber()
{
return columnNumber;
}
}

View File

@ -0,0 +1,82 @@
package org.apache.maven.settings.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.settings.Settings;
/**
* Handles deserialization of settings from some kind of textual format like XML.
*
* @author Benjamin Bentmann
*/
public interface SettingsReader
{
/**
* 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.
*/
String IS_STRICT = "org.apache.maven.settings.io.isStrict";
/**
* Reads the settings from the specified file.
*
* @param input The file to deserialize the settings 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 settings, never {@code null}.
* @throws IOException If the settings could not be deserialized.
* @throws SettingsParseException If the input format could not be parsed.
*/
Settings read( File input, Map<String, ?> options )
throws IOException, SettingsParseException;
/**
* Reads the settings from the specified character reader. The reader will be automatically closed before the method
* returns.
*
* @param input The reader to deserialize the settings 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 settings, never {@code null}.
* @throws IOException If the settings could not be deserialized.
* @throws SettingsParseException If the input format could not be parsed.
*/
Settings read( Reader input, Map<String, ?> options )
throws IOException, SettingsParseException;
/**
* Reads the settings from the specified byte stream. The stream will be automatically closed before the method
* returns.
*
* @param input The stream to deserialize the settings 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 settings, never {@code null}.
* @throws IOException If the settings could not be deserialized.
* @throws SettingsParseException If the input format could not be parsed.
*/
Settings read( InputStream input, Map<String, ?> options )
throws IOException, SettingsParseException;
}

View File

@ -0,0 +1,74 @@
package org.apache.maven.settings.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.settings.Settings;
/**
* Handles serialization of settings into some kind of textual format like XML.
*
* @author Benjamin Bentmann
*/
public interface SettingsWriter
{
/**
* Writes the supplied settings to the specified file. Any non-existing parent directories of the output file will
* be created automatically.
*
* @param output The file to serialize the settings to, must not be {@code null}.
* @param options The options to use for serialization, may be {@code null} to use the default values.
* @param settings The settings to serialize, must not be {@code null}.
* @throws IOException If the settings could not be serialized.
*/
void write( File output, Map<String, Object> options, Settings settings )
throws IOException;
/**
* Writes the supplied settings to the specified character writer. The writer will be automatically closed before
* the method returns.
*
* @param output The writer to serialize the settings to, must not be {@code null}.
* @param options The options to use for serialization, may be {@code null} to use the default values.
* @param settings The settings to serialize, must not be {@code null}.
* @throws IOException If the settings could not be serialized.
*/
void write( Writer output, Map<String, Object> options, Settings settings )
throws IOException;
/**
* Writes the supplied settings to the specified byte stream. The stream will be automatically closed before the
* method returns.
*
* @param output The stream to serialize the settings to, must not be {@code null}.
* @param options The options to use for serialization, may be {@code null} to use the default values.
* @param settings The settings to serialize, must not be {@code null}.
* @throws IOException If the settings could not be serialized.
*/
void write( OutputStream output, Map<String, Object> options, Settings settings )
throws IOException;
}