From: zapata Date: Thu, 3 Apr 2003 02:57:38 +0000 (+0000) Subject: Bundle tool added X-Git-Tag: BEFORE_MERGE_1_1~192 X-Git-Url: http://erislabs.org.uk/gitweb/?a=commitdiff_plain;h=c0ff1fd91bbf9600e8ece25bcc621b7d8a9ae41d;p=mir.git Bundle tool added --- diff --git a/source/mir/producer/RSSProducerNode.java b/source/mir/producer/RSSProducerNode.java index fc1c442b..c3d17e7e 100755 --- a/source/mir/producer/RSSProducerNode.java +++ b/source/mir/producer/RSSProducerNode.java @@ -1,33 +1,36 @@ -package mir.producer; - +package mir.producer; + import java.util.Map; import mir.log.LoggerWrapper; import mir.rss.RSSData; import mir.rss.RSSReader; import mir.rss.RSSToMapConverter; -import mir.util.ParameterExpander; - -public class RSSProducerNode implements ProducerNode { - private String key; - private String url; - - public RSSProducerNode(String aKey, String anURL) { - key = aKey; - url = anURL; - } - - public void produce(Map aValueMap, String aVerb, LoggerWrapper aLogger) throws ProducerFailure { - try { - String expandedKey = ParameterExpander.expandExpression( aValueMap, key ); - String expandedUrl = ParameterExpander.expandExpression( aValueMap, url ); - - RSSReader reader = new RSSReader(); - RSSData rssData = reader.parseUrl(url); - ParameterExpander.setValueForKey(aValueMap, expandedKey, RSSToMapConverter.convertRSSData(rssData)); - } - catch (Throwable t) { - aLogger.error("Error while processing RSS data: " + t.toString()); - } - }; +import mir.util.ParameterExpander; +import mir.util.ExceptionFunctions; + +public class RSSProducerNode implements ProducerNode { + private String key; + private String url; + + public RSSProducerNode(String aKey, String anURL) { + key = aKey; + url = anURL; + } + + public void produce(Map aValueMap, String aVerb, LoggerWrapper aLogger) throws ProducerFailure { + try { + String expandedKey = ParameterExpander.expandExpression( aValueMap, key ); + String expandedUrl = ParameterExpander.expandExpression( aValueMap, url ); + + ParameterExpander.setValueForKey(aValueMap, expandedKey, null); + RSSReader reader = new RSSReader(); + RSSData rssData = reader.parseUrl(expandedUrl); + ParameterExpander.setValueForKey(aValueMap, expandedKey, RSSToMapConverter.convertRSSData(rssData)); + } + catch (Throwable t) { + Throwable s = ExceptionFunctions.traceCauseException(t); + aLogger.error("Error while processing RSS data: " + s.getClass().getName()+","+ s.getMessage()); + } + }; } \ No newline at end of file diff --git a/source/mir/util/PropertiesManipulator.java b/source/mir/util/PropertiesManipulator.java new file mode 100755 index 00000000..72c67a47 --- /dev/null +++ b/source/mir/util/PropertiesManipulator.java @@ -0,0 +1,277 @@ +package mir.util; + +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.LineNumberReader; +import java.io.OutputStream; +import java.io.*; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Vector; + +import multex.Exc; +import multex.Failure; + +public class PropertiesManipulator { + private List entries; + private Map values; + + public PropertiesManipulator() { + entries = new Vector(); + values = new HashMap(); + } + + public void addEmptyLine() { + entries.add(new EmptyLine()); + } + + public void addComment(String aComment) { + entries.add(new Comment(aComment)); + } + + public void addEntry(String aKey, String aValue) { + entries.add(new Entry(aKey, aValue)); + values.put(aKey, aValue); + } + + public Iterator getEntries() { + return entries.iterator(); + } + + public String get(String aKey) { + return (String) values.get(aKey); + } + + public static class Comment { + private String comment; + + public Comment(String aComment) { + comment = aComment; + } + + public String getComment() { + return comment; + } + } + + public static class EmptyLine { + public EmptyLine() { + } + } + + public static class Entry { + private String key; + private String value; + + public Entry(String aKey, String aValue) { + key = aKey; + value = aValue; + } + + public String getKey() { + return key; + } + + public String getValue() { + return value; + } + } + + private final static String PLAIN= "[^\\\\]*"; + private final static String ESCAPE= "\\\\[ tn]"; + private final static String UNICODE= "\\\\u[a-fA-F0-9][a-fA-F0-9][a-fA-F0-9][a-fA-F0-9]"; + + + private static String decode(String aValue) { + try { + SimpleParser parser = new SimpleParser(aValue); + StringBuffer result = new StringBuffer(); + + while (!parser.isAtEnd()) { + result.append(parser.parse(PLAIN)); + + if (!parser.isAtEnd()) { + if (parser.parses(UNICODE)) { + String unicode = parser.parse(UNICODE); + + result.append((char) Integer.parseInt(unicode.substring(2,6), 16)); + } + else if (parser.parses(ESCAPE)) { + String escape = parser.parse(ESCAPE); + result.append(escape.substring(1)); + } + else + throw new PropertiesManipulatorExc("Invalid escape code: " + parser.remainingData()); + } + } + + return result.toString(); + } + catch (Throwable t) { + throw new PropertiesManipulatorFailure(t); + } + } + + private static String encode(String aValue) { + try { + StringBuffer result = new StringBuffer(); + boolean leadingspace=true; + + for (int i = 0; i0x7e) { + String code=Integer.toHexString(c); + result.append("\\u"); + for (int j=0; j<4-code.length(); j++) + result.append("0"); + result.append(code); + } + else if (c=='\\') + { + result.append("\\\\"); + } + else if (c=='\n') + { + result.append("\\n"); + } + else if (c=='\r') + { + result.append("\\r"); + } + else if (c=='\t') + { + result.append("\\t"); + } + else if (c==' ' && leadingspace) { + result.append("\\ "); + } + else { + result.append(c); + } + + leadingspace = leadingspace && c ==' '; + } + + return result.toString(); + } + catch (Throwable t) { + throw new PropertiesManipulatorFailure(t); + } + } + + // ML: to be fixed + private final static String SPACE = "[\t\n\r ]*"; + private final static String KEY = "(([\\\\].)|([^\\\\=: \t\n\r]))*"; + private final static String SEPARATOR = "[\t\n\r ]*[:=]?[\t\n\r ]*"; + private final static String VALUE = "(([\\\\].)|([^\\\\]))*"; + + public static PropertiesManipulator readProperties(InputStream anInputStream) throws PropertiesManipulatorExc, PropertiesManipulatorFailure { + try { + PropertiesManipulator result = new PropertiesManipulator(); + LineNumberReader reader = new LineNumberReader(new InputStreamReader(anInputStream, "ISO-8859-1")); + + String line = reader.readLine(); + + while (line != null) { + String trimmedLine = line.trim(); + + if (trimmedLine.length() == 0) { + result.addEmptyLine(); + } + else if (trimmedLine.startsWith("!") || trimmedLine.startsWith("#")) { + result.addComment(line); + } + else { + SimpleParser parser = new SimpleParser(line); + parser.skip(SPACE); + String key = parser.parse(KEY); + parser.skip(SEPARATOR); + String value = parser.parse(VALUE); + while (parser.remainingData().length()>0) { + if (!parser.remainingData().equals("\\")) + throw new PropertiesManipulatorExc("internal error: remainingData = " + parser.remainingData()); + + line = reader.readLine(); + if (line==null) { + throw new PropertiesManipulatorExc("Unexpected end of file"); + } + parser = new SimpleParser(line); + parser.skip(SPACE); + value = value + parser.parse(VALUE); + } + + result.addEntry(decode(key), decode(value)); + } + line = reader.readLine(); + } + + reader.close(); + + return result; + } + catch (PropertiesManipulatorExc t) { + throw t; + } + catch (Throwable t) { + throw new PropertiesManipulatorFailure(t); + } + } + + public static void writeProperties(PropertiesManipulator aProperties, OutputStream anOutputStream) throws PropertiesManipulatorExc, PropertiesManipulatorFailure { + try { + PrintWriter p = new PrintWriter(new OutputStreamWriter(anOutputStream, "ISO-8859-1")); + + try { + Iterator i = aProperties.getEntries(); + + while (i.hasNext()) { + Object entry = i.next(); + + if (entry instanceof EmptyLine) { + p.println(); + } + else if (entry instanceof Comment) { + p.println(((Comment) entry).getComment()); + } + else if (entry instanceof Entry) { + String key = encode( ( (Entry) entry).getKey()); + String value = ""; + if ( ( (Entry) entry).getValue() != null) + value = encode( ( (Entry) entry).getValue()); + + String line = key + " = " + value; + + p.println(line); + + } + else throw new PropertiesManipulatorExc("Unknown entry class: " +entry.getClass().getName()); + } + } + finally { + p.close(); + } + } + catch (Throwable t) { + throw new PropertiesManipulatorFailure(t); + } + } + + public static class PropertiesManipulatorFailure extends Failure { + public PropertiesManipulatorFailure(Throwable aThrowable) { + super(aThrowable.getMessage(), aThrowable); + } + + public PropertiesManipulatorFailure(String aMessage, Throwable aThrowable) { + super(aMessage, aThrowable); + } + } + + public static class PropertiesManipulatorExc extends Exc { + public PropertiesManipulatorExc(String aMessage) { + super(aMessage); + } + } +} \ No newline at end of file diff --git a/source/mir/util/SimpleParser.java b/source/mir/util/SimpleParser.java index e95d8ff2..1810466e 100755 --- a/source/mir/util/SimpleParser.java +++ b/source/mir/util/SimpleParser.java @@ -1,155 +1,254 @@ -/* - * Copyright (C) 2001, 2002 The Mir-coders group - * - * This file is part of Mir. - * - * Mir is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * Mir is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Mir; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * In addition, as a special exception, The Mir-coders gives permission to link - * the code of this program with the com.oreilly.servlet library, any library - * licensed under the Apache Software License, The Sun (tm) Java Advanced - * Imaging library (JAI), The Sun JIMI library (or with modified versions of - * the above that use the same license as the above), and distribute linked - * combinations including the two. You must obey the GNU General Public - * License in all respects for all of the code used other than the above - * mentioned libraries. If you modify this file, you may extend this exception - * to your version of the file, but you are not obligated to do so. If you do - * not wish to do so, delete this exception statement from your version. - */ - -package mir.util; - -import gnu.regexp.RE; -import gnu.regexp.REException; -import gnu.regexp.REMatch; -import multex.Exc; -import multex.Failure; - -public class SimpleParser { - private String data; - private int position; - - public SimpleParser(String aData) { - data=aData; - position=0; - } - - public boolean parses(RE aRegularExpression) throws SimpleParserExc { - REMatch match = aRegularExpression.getMatch(data, position); - - return (match!=null && match.getStartIndex()==position) ; - } - - public String parse(RE aRegularExpression, String aMessage) throws SimpleParserExc { - REMatch match = aRegularExpression.getMatch(data, position); - - if (match==null || match.getStartIndex()!=position) - throw new SimpleParserExc(aMessage+" at position "+position+" in '"+data+"'"); - - position=match.getEndIndex(); - - return match.toString(); - } - - public String parse(RE aRegularExpression) throws SimpleParserExc { - return parse( aRegularExpression, "No match found for '"+aRegularExpression.toString()+"'"); - } - - public void skip(RE aRegularExpression) throws SimpleParserExc { - REMatch match = aRegularExpression.getMatch(data, position); - - if (match!=null && match.getStartIndex()==position) - position=match.getEndIndex(); - } - - public boolean parses(String anExpression) throws SimpleParserExc { - try { - return parses(new RE(anExpression)); - } - catch (SimpleParserExc e) { - throw e; - } - catch (REException e) { - throw new SimpleParserFailure( "Error compiling regular expression '" + anExpression + "': " + e.getMessage(), e); - } - catch (Throwable t) { - throw new SimpleParserFailure( t ); - } - } - - public String parse(String anExpression) throws SimpleParserExc, SimpleParserFailure { - try { - return parse(new RE(anExpression)); - } - catch (SimpleParserExc e) { - throw e; - } - catch (REException e) { - throw new SimpleParserFailure( "Error compiling regular expression '" + anExpression + "': " + e.getMessage(), e); - } - catch (Throwable t) { - throw new SimpleParserFailure( t ); - } - } - - public String parse(String anExpression, String aMessage) throws SimpleParserExc, SimpleParserFailure { - try { - return parse(new RE(anExpression), aMessage); - } - catch (SimpleParserExc e) { - throw e; - } - catch (REException e) { - throw new SimpleParserFailure( "Error compiling regular expression '" + anExpression + "': " + e.getMessage(), e); - } - catch (Throwable t) { - throw new SimpleParserFailure( t ); - } - } - - public void skip(String anExpression) throws SimpleParserExc, SimpleParserFailure { - try { - skip(new RE(anExpression)); - } - catch (SimpleParserExc e) { - throw e; - } - catch (REException e) { - throw new SimpleParserFailure( "Error compiling regular expression '" + anExpression + "': " + e.getMessage(), e); - } - catch (Throwable t) { - throw new SimpleParserFailure( t ); - } - } - public boolean isAtEnd() { - return position>=data.length(); - } - - public static class SimpleParserFailure extends Failure { - public SimpleParserFailure(Throwable aThrowable) { - super(aThrowable.getMessage(), aThrowable); - } - - public SimpleParserFailure(String aMessage, Throwable aThrowable) { - super(aMessage, aThrowable); - } - } - - public static class SimpleParserExc extends Exc { - public SimpleParserExc(String aMessage) { - super(aMessage); - } - } +/* + * Copyright (C) 2001, 2002 The Mir-coders group + * + * This file is part of Mir. + * + * Mir is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * Mir is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Mir; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * In addition, as a special exception, The Mir-coders gives permission to link + * the code of this program with the com.oreilly.servlet library, any library + * licensed under the Apache Software License, The Sun (tm) Java Advanced + * Imaging library (JAI), The Sun JIMI library (or with modified versions of + * the above that use the same license as the above), and distribute linked + * combinations including the two. You must obey the GNU General Public + * License in all respects for all of the code used other than the above + * mentioned libraries. If you modify this file, you may extend this exception + * to your version of the file, but you are not obligated to do so. If you do + * not wish to do so, delete this exception statement from your version. + */ + +package mir.util; + +import gnu.regexp.RE; +import gnu.regexp.REException; +import gnu.regexp.REMatch; +import multex.Exc; +import multex.Failure; + +/** + * a class to do some basic, regexp based parsing of character data + * + *

Title:

+ *

Description:

+ *

Copyright: Copyright (c) 2003

+ *

Company:

+ * @author not attributable + * @version 1.0 + */ + +public class SimpleParser { + private String data; + private int position; + + /** + * + * @param aData + */ + + public SimpleParser(String aData) { + data=aData; + position=0; + } + + /** + * + * @param aRegularExpression + * @return + * @throws SimpleParserExc + */ + + public boolean parses(RE aRegularExpression) throws SimpleParserExc { + REMatch match = aRegularExpression.getMatch(data, position); + + return (match!=null && match.getStartIndex()==position) ; + } + + /** + * + * @param aRegularExpression + * @param aMessage + * @return + * @throws SimpleParserExc + */ + + public String parse(RE aRegularExpression, String aMessage) throws SimpleParserExc { + REMatch match = aRegularExpression.getMatch(data, position); + + if (match==null || match.getStartIndex()!=position) + throw new SimpleParserExc(aMessage+" at position "+position+" in '"+data+"'"); + + position=match.getEndIndex(); + + return match.toString(); + } + + /** + * + * @param aRegularExpression + * @return + * @throws SimpleParserExc + */ + + public String parse(RE aRegularExpression) throws SimpleParserExc { + return parse( aRegularExpression, "No match found for '"+aRegularExpression.toString()+"'"); + } + + /** + * + * @param aRegularExpression + * @throws SimpleParserExc + */ + + public void skip(RE aRegularExpression) throws SimpleParserExc { + REMatch match = aRegularExpression.getMatch(data, position); + + if (match!=null && match.getStartIndex()==position) + position=match.getEndIndex(); + } + + /** + * + * @param anExpression + * @return + * @throws SimpleParserExc + */ + + public boolean parses(String anExpression) throws SimpleParserExc { + try { + return parses(new RE(anExpression)); + } + catch (SimpleParserExc e) { + throw e; + } + catch (REException e) { + throw new SimpleParserFailure( "Error compiling regular expression '" + anExpression + "': " + e.getMessage(), e); + } + catch (Throwable t) { + throw new SimpleParserFailure( t ); + } + } + + /** + * + * @param anExpression + * @return + * @throws SimpleParserExc + * @throws SimpleParserFailure + */ + + public String parse(String anExpression) throws SimpleParserExc, SimpleParserFailure { + try { + return parse(new RE(anExpression)); + } + catch (SimpleParserExc e) { + throw e; + } + catch (REException e) { + throw new SimpleParserFailure( "Error compiling regular expression '" + anExpression + "': " + e.getMessage(), e); + } + catch (Throwable t) { + throw new SimpleParserFailure( t ); + } + } + + /** + * + * @param anExpression + * @param aMessage + * @return + * @throws SimpleParserExc + * @throws SimpleParserFailure + */ + + public String parse(String anExpression, String aMessage) throws SimpleParserExc, SimpleParserFailure { + try { + return parse(new RE(anExpression), aMessage); + } + catch (SimpleParserExc e) { + throw e; + } + catch (REException e) { + throw new SimpleParserFailure( "Error compiling regular expression '" + anExpression + "': " + e.getMessage(), e); + } + catch (Throwable t) { + throw new SimpleParserFailure( t ); + } + } + + /** + * + * @param anExpression + * @throws SimpleParserExc + * @throws SimpleParserFailure + */ + + public void skip(String anExpression) throws SimpleParserExc, SimpleParserFailure { + try { + skip(new RE(anExpression)); + } + catch (SimpleParserExc e) { + throw e; + } + catch (REException e) { + throw new SimpleParserFailure( "Error compiling regular expression '" + anExpression + "': " + e.getMessage(), e); + } + catch (Throwable t) { + throw new SimpleParserFailure( t ); + } + } + + /** + * + * @return true if the parser is at the end of the data + */ + + public boolean isAtEnd() { + return position>=data.length(); + } + + /** + * + * @return + */ + public String remainingData() { + return data.substring(position); + } + + /** + * + *

Title:

+ *

Description:

+ *

Copyright: Copyright (c) 2003

+ *

Company:

+ * @author not attributable + * @version 1.0 + */ + + public static class SimpleParserFailure extends Failure { + public SimpleParserFailure(Throwable aThrowable) { + super(aThrowable.getMessage(), aThrowable); + } + + public SimpleParserFailure(String aMessage, Throwable aThrowable) { + super(aMessage, aThrowable); + } + } + + public static class SimpleParserExc extends Exc { + public SimpleParserExc(String aMessage) { + super(aMessage); + } + } } \ No newline at end of file diff --git a/source/mircoders/global/Abuse.java b/source/mircoders/global/Abuse.java index dd1a4ede..7ef49c30 100755 --- a/source/mircoders/global/Abuse.java +++ b/source/mircoders/global/Abuse.java @@ -1,26 +1,29 @@ package mircoders.global; +import java.io.File; import java.io.FileNotFoundException; +import java.io.FileOutputStream; import java.util.Arrays; import java.util.Date; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; -import java.util.Vector; import java.util.Random; -import java.io.*; -import javax.servlet.http.*; +import java.util.Vector; +import javax.servlet.http.Cookie; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.apache.commons.collections.ExtendedProperties; -import org.apache.commons.collections.*; import gnu.regexp.RE; +import mir.entity.Entity; import mir.log.LoggerWrapper; import mir.util.DateToMapAdapter; +import mir.util.InternetFunctions; import mir.util.StringRoutines; -import mir.util.*; -import mir.entity.*; - import mircoders.entity.EntityComment; import mircoders.entity.EntityContent; import mircoders.localizer.MirAdminInterfaceLocalizer; diff --git a/source/tool/BundleTool.java b/source/tool/BundleTool.java new file mode 100755 index 00000000..1b9602c1 --- /dev/null +++ b/source/tool/BundleTool.java @@ -0,0 +1,89 @@ +package tool; + +import java.util.*; +import java.io.*; +import org.apache.commons.collections.*; + +import mir.util.*; + +public class BundleTool { + public static void main(String[] anArguments) { + + if (anArguments.length!=3) { + System.out.println("Usage:"); + + System.out.println(" BundleTool "); + System.out.println(""); + System.out.println("Description:"); + System.out.println(" Reorders keys/values from a slave bundle according to a master bundle."); + + return; + } + + PropertiesManipulator master; + PropertiesManipulator slave; + PropertiesManipulator result; + + try { + master = PropertiesManipulator.readProperties(new FileInputStream(new File(anArguments[0] + "_" + anArguments[1] + ".properties"))); + } + catch (Throwable t) { + System.out.println("Unable to read master properties: " + t.getMessage()); + return; + } + + try { + slave = PropertiesManipulator.readProperties(new FileInputStream(new File(anArguments[0] + "_" + anArguments[2] + ".properties"))); + } + catch (FileNotFoundException t) { + slave = new PropertiesManipulator(); + } + catch (Throwable t) { + System.out.println("Unable to read slave properties: " + t.getMessage()); + return; + } + result = new PropertiesManipulator(); + + Iterator i = slave.getEntries(); + while (i.hasNext()) { + Object e = i.next(); + + if (e instanceof PropertiesManipulator.EmptyLine) { + result.addEmptyLine(); + } + else if (e instanceof PropertiesManipulator.Comment) { + result.addComment(((PropertiesManipulator.Comment) e).getComment()); + } + + if (!(e instanceof PropertiesManipulator.Comment)) + break; + } + + boolean insideHeader=true; + i = master.getEntries(); + while (i.hasNext()) { + Object e = i.next(); + + if (!insideHeader && (e instanceof PropertiesManipulator.EmptyLine)) { + result.addEmptyLine(); + } + else if (!insideHeader && e instanceof PropertiesManipulator.Comment) { + result.addComment(((PropertiesManipulator.Comment) e).getComment()); + } + else if (e instanceof PropertiesManipulator.Entry) { + String key = ((PropertiesManipulator.Entry) e).getKey(); + String value = slave.get(key); + result.addEntry(key,value); + } + + insideHeader = insideHeader && (e instanceof PropertiesManipulator.Comment); + } + try { + PropertiesManipulator.writeProperties(result, new FileOutputStream(new File(anArguments[0] + "_" + anArguments[2] + ".properties"))); + } + catch (Throwable t) { + System.out.println("Unable to write slave properties: " + t.getMessage()); + return; + } + } +} \ No newline at end of file