001: /*
002: * (c) Copyright 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: * [See end of file]
004: */
005:
006: /**
007: * @author Andy Seaborne
008: * @version $Id: juuid.java,v 1.4 2008/01/02 12:08:16 andy_seaborne Exp $
009: */package jena;
010:
011: import com.hp.hpl.jena.shared.uuid.*;
012:
013: import arq.cmdline.ArgDecl;
014: import arq.cmdline.CmdArgModule;
015: import arq.cmdline.CmdGeneral;
016: import arq.cmdline.ModBase;
017:
018: public class juuid extends CmdGeneral {
019: ModJUUID modJUUID = new ModJUUID();
020: int number = 1;
021: boolean resetEachTime = false;
022: int uuidType = 0;
023: boolean asURN = false;
024: boolean asURI = false;
025: boolean asPlain = false;
026:
027: public static void main(String[] argv) {
028: new juuid(argv).mainAndExit();
029: }
030:
031: private juuid(String argv[]) {
032: super (argv);
033: super .addModule(modJUUID);
034: }
035:
036: protected String getSummary() {
037: return getCommandName() + " [--num=N] [--reset] [--type={1|4}]";
038: }
039:
040: protected void exec() {
041: if (uuidType == UUID_V1.version)
042: JenaUUID.setFactory(new UUID_V1_Gen());
043: if (uuidType == UUID_V4.version)
044: JenaUUID.setFactory(new UUID_V4_Gen());
045:
046: for (int i = 0; i < number; i++) {
047: if (resetEachTime && i != 0)
048: JenaUUID.reset();
049: JenaUUID uuid = JenaUUID.generate();
050: String str = null;
051: if (asURN)
052: str = uuid.asURN();
053: else if (asURN)
054: str = uuid.asURI();
055: else if (asPlain)
056: str = uuid.asString();
057: if (str == null)
058: str = uuid.asString();
059: System.out.println(str);
060: }
061: }
062:
063: protected String getCommandName() {
064: return "uuid";
065: }
066:
067: protected void processModulesAndArgs() {
068:
069: }
070:
071: static ArgDecl argDeclNum = new ArgDecl(ArgDecl.HasValue, "num",
072: "n");
073: static ArgDecl argDeclReset = new ArgDecl(ArgDecl.NoValue, "reset");
074: static ArgDecl argDeclGen = new ArgDecl(ArgDecl.HasValue, "gen",
075: "scheme");
076: static ArgDecl argDeclURN = new ArgDecl(ArgDecl.NoValue, "urn");
077: static ArgDecl argDeclURI = new ArgDecl(ArgDecl.NoValue, "uri");
078: static ArgDecl argDeclPlain = new ArgDecl(ArgDecl.NoValue, "plain");
079:
080: class ModJUUID extends ModBase {
081: public void registerWith(CmdGeneral cmdLine) {
082: cmdLine.add(argDeclNum);
083: cmdLine.add(argDeclReset);
084: cmdLine.add(argDeclGen);
085: cmdLine.add(argDeclURN);
086: cmdLine.add(argDeclURI);
087: cmdLine.add(argDeclPlain);
088: }
089:
090: public void processArgs(CmdArgModule cmdLine) {
091: String numStr = null;
092:
093: if (getNumPositional() > 1)
094: cmdError("Too many positional arguments");
095:
096: if (cmdLine.contains(argDeclNum)) {
097: if (getNumPositional() != 0)
098: cmdError("--num and positional arguments don't go together");
099: numStr = getValue(argDeclNum);
100: }
101:
102: if (numStr == null && cmdLine.getNumPositional() == 1)
103: numStr = cmdLine.getPositionalArg(0);
104:
105: if (numStr != null) {
106: try {
107: number = Integer.parseInt(numStr);
108: if (number < 0 || number > 10000)
109: cmdLine.cmdError("Number out of range:"
110: + numStr);
111: } catch (NumberFormatException e) {
112: cmdLine.cmdError("Bad argument: " + numStr);
113: }
114: }
115:
116: resetEachTime = cmdLine.contains(argDeclReset);
117:
118: if (contains(argDeclGen)) {
119: String s = getValue(argDeclGen);
120: if (s.equalsIgnoreCase("time")
121: || s.equalsIgnoreCase("1"))
122: uuidType = UUID_V1.version;
123: else if (s.equalsIgnoreCase("random")
124: || s.equalsIgnoreCase("rand")
125: || s.equalsIgnoreCase("4"))
126: uuidType = UUID_V4.version;
127: else
128: cmdError("Unrecognized UUID scheme: " + s);
129: }
130:
131: if (contains(argDeclURN) || contains(argDeclURI)
132: || contains(argDeclPlain)) {
133: asURN = contains(argDeclURN);
134: asURI = contains(argDeclURI);
135: asPlain = contains(argDeclPlain);
136: } else {
137: // Defaults
138: asURN = true;
139: asURI = false;
140: asPlain = false;
141: }
142: }
143: }
144: }
145:
146: /*
147: * (c) Copyright 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
148: * All rights reserved.
149: *
150: * Redistribution and use in source and binary forms, with or without
151: * modification, are permitted provided that the following conditions
152: * are met:
153: * 1. Redistributions of source code must retain the above copyright
154: * notice, this list of conditions and the following disclaimer.
155: * 2. Redistributions in binary form must reproduce the above copyright
156: * notice, this list of conditions and the following disclaimer in the
157: * documentation and/or other materials provided with the distribution.
158: * 3. The name of the author may not be used to endorse or promote products
159: * derived from this software without specific prior written permission.
160: *
161: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
162: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
163: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
164: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
165: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
166: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
167: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
168: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
169: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
170: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
171: */
|