PageRenderTime 21ms CodeModel.GetById 9ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

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