Kleine Änderung beim RelationAttributeImporter: Einzelne RelationAttributes können jetzt auch per Kommandozeile, ohne XML Datei, angelegt werden.

git-svn-id: https://svn.libreccm.org/ccm/trunk@573 8810af33-2d31-482b-a856-94f89814c4df
master
jensp 2010-10-15 08:50:14 +00:00
parent e994fe7bda
commit cb6af4338c
1 changed files with 86 additions and 51 deletions

View File

@ -34,7 +34,8 @@ import org.xml.sax.SAXException;
/**
* <p>
* A simple import tool for {@link RelationAttribute}s. This tool imports the
* relation attributes from a simple XML file, which looks like this:
* relation attributes from a simple XML file or from parameters at the
* commandline. The XML file format:
* </p>
* <pre>
* &lt;?xml version="1.0"?&gt;
@ -46,6 +47,7 @@ import org.xml.sax.SAXException;
* &lt;name&gt;...&lt;/name&gt;
* &lt;description&gt;...&lt;/description&gt;
* &lt;/relationAttribute&gt;
* ...
* &lt;/relationAttributes&gt;
* </pre>
* <p>
@ -53,10 +55,22 @@ import org.xml.sax.SAXException;
* to import is needed. With tools-ng and ECDC, the line for calling the
* <code>RelationAttributeImporter</code> would like the following:
* </p>
* <p>
* With a XML file:
* </p>
* <pre>
* ant -Dccm.classname="com.arsdigita.cms.relationattributeimporter.RelationAttributeImporter" -Dccm.parameters="/path/to/relation/attribute/file.xml" ccm-run
* </pre>
* <p>
* With command line parameters:
* </p>
* <pre>
* ant -Dccm.classname="com.arsdigita.cms.relationattributeimporter.RelationAttributeImporter" -Dccm.parameters="attribute key lang name [description]" ccm-run
* </pre>
* <p>
* The description parameter is optional.
* </p>
* <p>
* You have to add the <code>RelationAttributeImporter</code> to add to your
* environment, of course.
* </p>
@ -68,11 +82,15 @@ import org.xml.sax.SAXException;
public class RelationAttributeImporter extends Program {
public RelationAttributeImporter() {
super("Relation Attribute Importer", "0.1.0", "FILE");
super("Relation Attribute Importer",
"0.1.0",
"FILE | ATTRIBUTE KEY LANG NAME [DESCRIPTION]");
}
public RelationAttributeImporter(boolean startup) {
super("Relation Attribute Importer", "0.1.0", "FILE", startup);
super("Relation Attribute Importer",
"0.1.0",
"FILE | ATTRIBUTE KEY LANG NAME [DESCRIPTION]", startup);
}
@Override
@ -84,56 +102,72 @@ public class RelationAttributeImporter extends Program {
SAXParser saxParser = null;
RelationAttributeParser parser;
args = cmdLine.getArgs();
if (args.length != 1) {
if (args.length == 1) {
System.out.printf("Using file %s.", args[0]);
relAttrFile = new File(args[0]);
if (!relAttrFile.exists()) {
System.err.printf("ERROR: File %s does not exist.", args[0]);
System.exit(-1);
}
if (!relAttrFile.isFile()) {
System.err.printf("ERROR: Path %s does not point to a file.",
args[0]);
System.exit(-1);
}
saxFactory = SAXParserFactory.newInstance();
try {
saxParser = saxFactory.newSAXParser();
} catch (ParserConfigurationException ex) {
System.err.printf("Error creating SAXParser: %s",
ex.getMessage());
ex.printStackTrace();
System.exit(-1);
} catch (SAXException ex) {
System.err.printf("Error creating SAXParser: %s",
ex.getMessage());
ex.printStackTrace();
System.exit(-1);
}
parser = new RelationAttributeParser();
try {
saxParser.parse(relAttrFile, parser);
} catch (SAXException ex) {
System.err.printf("Error parsing file %s: %s",
args[0],
ex.getMessage());
ex.printStackTrace();
System.exit(-1);
} catch (IOException ex) {
System.err.printf("Error parsing file %s: %s",
args[0], ex.getMessage());
ex.printStackTrace();
System.exit(-1);
}
System.out.println(
"Parsed XML file successfully. Creating ACSObjects...");
for (int i = 0; i < parser.getRelAttrs().size(); i++) {
System.out.printf("%d of %d...", (i + 1), parser.getRelAttrs().
size());
createACSObject(parser.getRelAttrs().get(i));
}
} else if ((args.length == 4) || (args.length == 5)) {
RelAttrBean relAttr;
relAttr = new RelAttrBean();
relAttr.setAttribute(args[0]);
relAttr.setKey(args[1]);
relAttr.setLang(args[2]);
relAttr.setName(args[3]);
if (args.length == 5) {
relAttr.setDescription(args[4]);
}
createACSObject(relAttr);
} else {
help(System.err);
System.exit(-1);
}
System.out.printf("Using file %s.", args[0]);
relAttrFile = new File(args[0]);
if (!relAttrFile.exists()) {
System.err.printf("ERROR: File %s does not exist.", args[0]);
System.exit(-1);
}
if (!relAttrFile.isFile()) {
System.err.printf("ERROR: Path %s does not point to a file.",
args[0]);
System.exit(-1);
}
saxFactory = SAXParserFactory.newInstance();
try {
saxParser = saxFactory.newSAXParser();
} catch (ParserConfigurationException ex) {
System.err.printf("Error creating SAXParser: %s", ex.getMessage());
ex.printStackTrace();
System.exit(-1);
} catch (SAXException ex) {
System.err.printf("Error creating SAXParser: %s", ex.getMessage());
ex.printStackTrace();
System.exit(-1);
}
parser = new RelationAttributeParser();
try {
saxParser.parse(relAttrFile, parser);
} catch (SAXException ex) {
System.err.printf("Error parsing file %s: %s",
args[0],
ex.getMessage());
ex.printStackTrace();
System.exit(-1);
} catch (IOException ex) {
System.err.printf("Error parsing file %s: %s",
args[0], ex.getMessage());
ex.printStackTrace();
System.exit(-1);
}
System.out.println(
"Parsed XML file successfully. Creating ACSObjects...");
for (int i = 0; i < parser.getRelAttrs().size(); i++) {
System.out.printf("%d of %d...", (i + 1), parser.getRelAttrs().size());
createACSObject(parser.getRelAttrs().get(i), i);
}
}
/**
@ -143,7 +177,7 @@ public class RelationAttributeImporter extends Program {
* @param relAttr
* @param index
*/
private void createACSObject(final RelAttrBean relAttr, int index) {
private void createACSObject(final RelAttrBean relAttr) {
/*System.out.printf("\tattribute = %s\n", relAttr.getAttribute());
System.out.printf("\tkey = %s\n", relAttr.getKey());
@ -152,6 +186,7 @@ public class RelationAttributeImporter extends Program {
System.out.printf("\tdescription = %s\n", relAttr.getDescription());*/
Transaction transaction = new Transaction() {
@Override
public void doRun() {
RelationAttribute attr;