
redis.redisgen.Main Maven / Gradle / Ivy
package redis.redisgen;
import com.github.mustachejava.DefaultMustacheFactory;
import com.github.mustachejava.Mustache;
import com.github.mustachejava.MustacheException;
import com.github.mustachejava.MustacheFactory;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableListMultimap;
import com.google.common.collect.Multimaps;
import com.sampullara.cli.Args;
import com.sampullara.cli.Argument;
import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.map.MappingJsonFactory;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import java.io.*;
import java.net.URL;
import java.net.URLEncoder;
import java.util.*;
/**
* Generate client code for redis based on the protocol.
*
* User: sam
* Date: 11/5/11
* Time: 9:10 PM
*/
@SuppressWarnings({"UnusedDeclaration", "FieldCanBeLocal"})
public class Main {
@Argument(alias = "l")
private static String language = "java";
@Argument(alias = "p")
private static String pkg = "redis.client";
@Argument(alias = "d", required = true)
private static File dest;
@Argument(alias = "t")
private static String template = "client";
@Argument(alias = "n")
private static String className = "RedisClient";
private static Set keywords = new HashSet() {{
add("type");
add("object");
}};
@SuppressWarnings("ResultOfMethodCallIgnored")
public static void main(String[] args) throws IOException, ParserConfigurationException, SAXException, XPathExpressionException, MustacheException {
try {
Args.parse(Main.class, args);
} catch (IllegalArgumentException e) {
Args.usage(Main.class);
System.exit(1);
}
MustacheFactory mb = new DefaultMustacheFactory("templates/" + language) {
@Override
public void encode(String value, Writer writer) {
try {
writer.write(value);
} catch (IOException e) {
throw new MustacheException();
}
}
};
Mustache mustache = mb.compile(template + ".txt");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
XPathFactory xpf = XPathFactory.newInstance();
final DocumentBuilder db = dbf.newDocumentBuilder();
final XPathExpression replyX = xpf.newXPath().compile("//a");
final Properties cache = new Properties();
File cacheFile = new File("cache");
if (cacheFile.exists()) {
cache.load(new FileInputStream(cacheFile));
}
Set ungenerated = new HashSet(
Arrays.asList(
"MULTI", "EXEC", "DISCARD", // Transactions
"PSUBSCRIBE", "SUBSCRIBE", "UNSUBSCRIBE", "PUNSUBSCRIBE", // subscriptions
"AUTH" // authentication
)
);
final Set genericReply = new HashSet(Arrays.asList(
"SORT", // Can return an integer reply
"ZRANK", "ZREVRANK", // Two different return values
"SRANDMEMBER" // Can return a bulk or multibulk reply depending on count
));
final Set multiples = new HashSet(Arrays.asList(
"ZADD"
));
JsonFactory jf = new MappingJsonFactory();
JsonParser jsonParser = jf.createJsonParser(new URL("https://raw.github.com/antirez/redis-doc/master/commands.json"));
final JsonNode commandNodes = jsonParser.readValueAsTree();
Iterator fieldNames = commandNodes.getFieldNames();
ImmutableListMultimap group = Multimaps.index(fieldNames,
new Function() {
@Override
public String apply(String s) {
return commandNodes.get(s).get("group").asText();
}
});
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy