/farmR/src/java/src/jfm/xml/TextParser.java

https://code.google.com/p/javawfm/ · Java · 77 lines · 67 code · 7 blank · 3 comment · 10 complexity · 2be8654aa1e55ef579dee0e0b45671c8 MD5 · raw file

  1. package jfm.xml;
  2. import jfm.model.Types;
  3. import jfm.model.Types.*;
  4. /** Parses generic comma separated value text fields */
  5. public class TextParser extends JFMObjectParser {
  6. private int length = -1;
  7. public TextDataType dataType=null;
  8. public OperationType opType=null;
  9. public WorkerType wkType=null;
  10. public TextParser(ObjectParser parent_){
  11. // System.out.println("new csvparser with parent "+parent_);
  12. parent=parent_;
  13. registerAttribute("length",JFMObjectParser.optionalAttribute);
  14. registerAttribute("data",JFMObjectParser.mandatoryAttribute);
  15. registerAttribute("wktype",JFMObjectParser.optionalAttribute);
  16. registerAttribute("optype",JFMObjectParser.optionalAttribute);
  17. }
  18. @Override
  19. public void initializeObject() throws XMLObjectException, XMLSyntaxException {
  20. try {
  21. if (textContent==null){ throw new XMLSyntaxException("CSV elements must contain text");};
  22. if ( attributeHasValue("length")){
  23. length=Integer.parseInt(getNamedAttribute("length"));
  24. }
  25. if ( length > -1 & textContent.split(",").length != length){
  26. throw new XMLSyntaxException(this.printNameAndAttributes()+" specifies "+length+
  27. " entries but "+textContent.split(",").length+" supplied at "
  28. +parent.printNameAndAttributes());
  29. }
  30. // obj = textContent.split(",");
  31. obj=textContent;
  32. if ( attributeHasValue("data")){
  33. dataType=Types.xmlToTextDataType(getNamedAttribute("data"));
  34. } else {
  35. throw new XMLSyntaxException("data is a mandatory attribute for csv fields");
  36. }
  37. if (attributeHasValue("optype")){
  38. opType=Types.xmlToOperationType(getNamedAttribute("optype"));
  39. }
  40. if ( attributeHasValue("wktype")){
  41. wkType=Types.xmlToWorkerType(getNamedAttribute("wktype"));
  42. }
  43. } catch (NumberFormatException ex){
  44. throw new XMLSyntaxException("Bad number format for attribute");
  45. } catch (XMLSyntaxException tex){
  46. tex.printStackTrace();
  47. throw new XMLSyntaxException(tex.getMessage());
  48. }
  49. }
  50. public String name(){
  51. if ( dataType!=null ){
  52. return dataType.xmlname;
  53. } else {
  54. return "textparser";
  55. }
  56. }
  57. public String[] getCSV(){
  58. String objalias=(String)obj;
  59. return objalias.split(",");
  60. }
  61. @Override
  62. public String parsesNode() {
  63. return "csv";
  64. }
  65. @Override
  66. public String toString() {
  67. return "CSVParser";
  68. }
  69. protected boolean isPrimitive(){ return true;};
  70. }