diff options
author | mattinger <[email protected]> | 2006-07-06 21:53:00 +0000 |
---|---|---|
committer | mattinger <[email protected]> | 2006-07-06 21:53:00 +0000 |
commit | 1159111b7a71b72eb04326df33211e1733f7e742 (patch) | |
tree | f0a80c384f633e521649654ab78e6239cf5e0d6f /src/java/net/sf/antcontrib/inifile/IniFileTask.java |
Initial addition into subversion with build script changes
git-svn-id: file:///home/sven/projects/JOGL/temp/ant-contrib/svn/ant-contrib-code/trunk/ant-contrib@5 32d7a393-a5a9-423c-abd3-5d954feb1f2f
Diffstat (limited to 'src/java/net/sf/antcontrib/inifile/IniFileTask.java')
-rw-r--r-- | src/java/net/sf/antcontrib/inifile/IniFileTask.java | 326 |
1 files changed, 326 insertions, 0 deletions
diff --git a/src/java/net/sf/antcontrib/inifile/IniFileTask.java b/src/java/net/sf/antcontrib/inifile/IniFileTask.java new file mode 100644 index 0000000..d7e44aa --- /dev/null +++ b/src/java/net/sf/antcontrib/inifile/IniFileTask.java @@ -0,0 +1,326 @@ +/* + * Copyright (c) 2001-2004 Ant-Contrib project. All rights reserved. + * + * Licensed 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. + */ + package net.sf.antcontrib.inifile; + +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.util.Iterator; +import java.util.Vector; + +import org.apache.tools.ant.BuildException; +import org.apache.tools.ant.Project; +import org.apache.tools.ant.Task; + + +/**************************************************************************** + * Place class description here. + * + * @author <a href='mailto:[email protected]'>Matthew Inger</a> + * @author <additional author> + * + * @since + * + ****************************************************************************/ + + +public class IniFileTask + extends Task +{ + public static abstract class IniOperation + { + private String section; + private String property; + private String ifCond; + private String unlessCond; + + public IniOperation() + { + super(); + } + + public String getSection() + { + return section; + } + + + public void setSection(String section) + { + this.section = section; + } + + + public String getProperty() + { + return property; + } + + + public void setProperty(String property) + { + this.property = property; + } + + + public void setIf(String ifCond) + { + this.ifCond = ifCond; + } + + public void setUnless(String unlessCond) + { + this.unlessCond = unlessCond; + } + + /** + * Returns true if the define's if and unless conditions + * (if any) are satisfied. + */ + public boolean isActive(org.apache.tools.ant.Project p) + { + if (ifCond != null && p.getProperty(ifCond) == null) + { + return false; + } + else if (unlessCond != null && p.getProperty(unlessCond) != null) + { + return false; + } + + return true; + } + + public void execute(Project project, IniFile iniFile) + { + if (isActive(project)) + operate(iniFile); + } + + protected abstract void operate(IniFile file); + } + + public static final class Remove + extends IniOperation + { + public Remove() + { + super(); + } + + protected void operate(IniFile file) + { + String secName = getSection(); + String propName = getProperty(); + + if (propName == null) + { + file.removeSection(secName); + } + else + { + IniSection section = file.getSection(secName); + if (section != null) + section.removeProperty(propName); + } + } + } + + + public final class Set + extends IniOperation + { + private String value; + private String operation; + + public Set() + { + super(); + } + + + public void setValue(String value) + { + this.value = value; + } + + + public void setOperation(String operation) + { + this.operation = operation; + } + + + protected void operate(IniFile file) + { + String secName = getSection(); + String propName = getProperty(); + + IniSection section = file.getSection(secName); + if (section == null) + { + section = new IniSection(secName); + file.setSection(section); + } + + if (propName != null) + { + if (operation != null) + { + if ("+".equals(operation)) + { + IniProperty prop = section.getProperty(propName); + value = prop.getValue(); + int intVal = Integer.parseInt(value) + 1; + value = String.valueOf(intVal); + } + else if ("-".equals(operation)) + { + IniProperty prop = section.getProperty(propName); + value = prop.getValue(); + int intVal = Integer.parseInt(value) - 1; + value = String.valueOf(intVal); + } + } + section.setProperty(new IniProperty(propName, value)); + } + } + + } + + private File source; + private File dest; + private Vector operations; + + public IniFileTask() + { + super(); + this.operations = new Vector(); + } + + public Set createSet() + { + Set set = new Set(); + operations.add(set); + return set; + } + + public Remove createRemove() + { + Remove remove = new Remove(); + operations.add(remove); + return remove; + } + + + public void setSource(File source) + { + this.source = source; + } + + + public void setDest(File dest) + { + this.dest = dest; + } + + + public void execute() + throws BuildException + { + if (dest == null) + throw new BuildException("You must supply a dest file to write to."); + + IniFile iniFile = null; + + try + { + iniFile = readIniFile(source); + } + catch (IOException e) + { + throw new BuildException(e); + } + + Iterator it = operations.iterator(); + IniOperation operation = null; + while (it.hasNext()) + { + operation = (IniOperation)it.next(); + operation.execute(getProject(), iniFile); + } + + FileWriter writer = null; + + try + { + try + { + writer = new FileWriter(dest); + iniFile.write(writer); + } + finally + { + try + { + if (writer != null) + writer.close(); + } + catch (IOException e) + { + ; // gulp + } + } + } + catch (IOException e) + { + throw new BuildException(e); + } + + } + + + private IniFile readIniFile(File source) + throws IOException + { + FileReader reader = null; + IniFile iniFile = new IniFile(); + + if (source == null) + return iniFile; + + try + { + reader = new FileReader(source); + iniFile.read(reader); + } + finally + { + try + { + if (reader != null) + reader.close(); + } + catch (IOException e) + { + ; // gulp + } + } + + return iniFile; + } +} |