Update FileServices with new operation "replace in file"

git-svn-id: https://brutex.net/svn/xservices/trunk@63 e7e49efb-446e-492e-b9ec-fcafc1997a86
tag-20130205r
Brian Rosenberger 2011-05-03 14:10:04 +00:00
parent 2e99ff8f80
commit 10a2ca711d
3 changed files with 152 additions and 1 deletions

View File

@ -0,0 +1,75 @@
/*
* Copyright 2010 Brian Rosenberger (Brutex Network)
*
* 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.brutex.xservices.types;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
/**
* Generic key/ value pairs.
*
* @author Brian Rosenberger
*/
@XmlRootElement
public class ReplacePattern {
/**
* Search string.
*/
@XmlElement(required=true)
public String search ="";
/**
* Replace string.
*/
@XmlElement(required=true)
public String replace="";
/**
* Converts a Map<String, String> into a list of
* ReplacePattern.
* @param map The map to convert
* @return A list of key/value pairs
*/
public static List<ReplacePattern> createAntPropertyList(Map<String, String> map) {
List<ReplacePattern> list = new ArrayList<ReplacePattern>();
for(Map.Entry<String, String> e : map.entrySet()) {
list.add(new ReplacePattern(e.getKey(), e.getValue()));
}
return list;
}
/**
* Creates a new ReplacePattern.
* @param name
* @param value
*/
public ReplacePattern(String search, String replace) {
this.search = search;
this.replace = replace;
}
/**
* Creates a new ReplacePattern.
*/
public ReplacePattern() {
}
}

View File

@ -1,5 +1,7 @@
package net.brutex.xservices.ws; package net.brutex.xservices.ws;
import java.util.List;
import javax.activation.DataHandler; import javax.activation.DataHandler;
import javax.jws.WebMethod; import javax.jws.WebMethod;
import javax.jws.WebParam; import javax.jws.WebParam;
@ -9,6 +11,7 @@ import net.brutex.xservices.types.ArchiveResource;
import net.brutex.xservices.types.AttachmentType; import net.brutex.xservices.types.AttachmentType;
import net.brutex.xservices.types.FileResource; import net.brutex.xservices.types.FileResource;
import net.brutex.xservices.types.FileSetResource; import net.brutex.xservices.types.FileSetResource;
import net.brutex.xservices.types.ReplacePattern;
import net.brutex.xservices.types.ReturnCode; import net.brutex.xservices.types.ReturnCode;
import net.brutex.xservices.util.BrutexNamespaces; import net.brutex.xservices.util.BrutexNamespaces;
@WebService(targetNamespace = BrutexNamespaces.WS_XSERVICES) @WebService(targetNamespace = BrutexNamespaces.WS_XSERVICES)
@ -67,4 +70,21 @@ public interface FileService {
@WebParam(name = "fileset") FileSetResource res, @WebParam(name = "fileset") FileSetResource res,
@WebParam(name = "permissions") String perm); @WebParam(name = "permissions") String perm);
@WebMethod(operationName = "replaceInFile")
public abstract ReturnCode replaceInFile(
@WebParam(name = "file") FileResource res,
@WebParam(name = "search") String search,
@WebParam(name = "replace") String replace);
@WebMethod(operationName = "replaceInFile2")
public abstract ReturnCode replaceInFile2(
@WebParam(name = "file") FileResource res,
@WebParam(name = "patternList") List<ReplacePattern> patternList);
@WebMethod(operationName = "replaceInFileRegEx")
public abstract ReturnCode replaceInFileRegEx(
@WebParam(name = "file") FileResource res,
@WebParam(name = "search") String search,
@WebParam(name = "replace") String replace,
@WebParam(name = "flags") String flags);
} }

View File

@ -15,6 +15,7 @@
*/ */
package net.brutex.xservices.ws.impl; package net.brutex.xservices.ws.impl;
import java.io.BufferedInputStream;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
@ -34,6 +35,7 @@ import net.brutex.xservices.types.ArchiveResource;
import net.brutex.xservices.types.AttachmentType; import net.brutex.xservices.types.AttachmentType;
import net.brutex.xservices.types.FileResource; import net.brutex.xservices.types.FileResource;
import net.brutex.xservices.types.FileSetResource; import net.brutex.xservices.types.FileSetResource;
import net.brutex.xservices.types.ReplacePattern;
import net.brutex.xservices.types.ResourceInterface; import net.brutex.xservices.types.ResourceInterface;
import net.brutex.xservices.types.ReturnCode; import net.brutex.xservices.types.ReturnCode;
import net.brutex.xservices.util.BrutexNamespaces; import net.brutex.xservices.util.BrutexNamespaces;
@ -50,6 +52,8 @@ import org.apache.tools.ant.taskdefs.Chmod;
import org.apache.tools.ant.taskdefs.Copy; import org.apache.tools.ant.taskdefs.Copy;
import org.apache.tools.ant.taskdefs.Echo; import org.apache.tools.ant.taskdefs.Echo;
import org.apache.tools.ant.taskdefs.LoadResource; import org.apache.tools.ant.taskdefs.LoadResource;
import org.apache.tools.ant.taskdefs.Replace;
import org.apache.tools.ant.taskdefs.optional.ReplaceRegExp;
import org.apache.tools.ant.taskdefs.optional.unix.Chgrp; import org.apache.tools.ant.taskdefs.optional.unix.Chgrp;
import org.apache.tools.ant.taskdefs.optional.unix.Chown; import org.apache.tools.ant.taskdefs.optional.unix.Chown;
import org.apache.tools.ant.types.FileSet; import org.apache.tools.ant.types.FileSet;
@ -74,6 +78,58 @@ public class FileServiceImpl implements FileService {
return basename(new File(filename), suffix); return basename(new File(filename), suffix);
} }
@WebMethod(operationName = "replaceInFile")
public ReturnCode replaceInFile(
@WebParam(name = "file") FileResource res,
@WebParam(name = "search") String search,
@WebParam(name = "replace") String replace) {
ReturnCode r = null;
Replace rep = new Replace();
rep.setTaskName("Replace");
RunTask runner = new RunTask(rep);
rep.addConfigured(res.getAntResource(rep.getProject()));
rep.setToken(search);
rep.setValue(replace);
r = runner.postTask();
return r;
}
@WebMethod(operationName = "replaceInFile2")
public ReturnCode replaceInFile2(
@WebParam(name = "file") FileResource res,
@WebParam(name = "patternList") List<ReplacePattern> patternList) {
ReturnCode r = null;
for (ReplacePattern pat : patternList) {
Replace rep = new Replace();
rep.setTaskName("Replace");
RunTask runner = new RunTask(rep);
rep.addConfigured(res.getAntResource(rep.getProject()));
rep.setToken(pat.search);
rep.setValue(pat.replace);
r = runner.postTask();
}
return r;
}
@WebMethod(operationName = "replaceInFileRegEx")
public ReturnCode replaceInFileRegEx(
@WebParam(name = "file") FileResource res,
@WebParam(name = "search") String search,
@WebParam(name = "replace") String replace,
@WebParam(name = "flags") String flags) {
ReplaceRegExp rep = new ReplaceRegExp();
rep.setTaskName("ReplaceRegExp");
RunTask runner = new RunTask(rep);
File infile = new File(res.uri);
rep.setFile(infile);
rep.setMatch(search);
rep.setReplace(replace);
rep.setFlags(flags);
ReturnCode r = runner.postTask();
return r;
}
/* /*
* (non-Javadoc) * (non-Javadoc)
* *