View Javadoc
1   package au.gov.amsa.ais;
2   
3   import au.gov.amsa.ais.message.AbstractAisBStaticDataReport;
4   import au.gov.amsa.ais.message.AisAidToNavigation;
5   import au.gov.amsa.ais.message.AisBStaticDataReportPartA;
6   import au.gov.amsa.ais.message.AisBStaticDataReportPartB;
7   import au.gov.amsa.ais.message.AisBaseStation;
8   import au.gov.amsa.ais.message.AisMessageOther;
9   import au.gov.amsa.ais.message.AisPositionA;
10  import au.gov.amsa.ais.message.AisPositionB;
11  import au.gov.amsa.ais.message.AisPositionBExtended;
12  import au.gov.amsa.ais.message.AisPositionGPS;
13  import au.gov.amsa.ais.message.AisShipStaticA;
14  
15  /**
16   * Parses AIS messages (as they are taken from the 5th column in the NMEA
17   * message).
18   * 
19   * @author dxm
20   * 
21   */
22  public class AisMessageParser {
23  
24  	private final AisExtractorFactory factory;
25  
26  	/**
27  	 * Constructor.
28  	 */
29  	public AisMessageParser() {
30  		this(Util.getAisExtractorFactory());
31  	}
32  
33  	/**
34  	 * Constructor.
35  	 * 
36  	 * @param factory
37  	 */
38  	public AisMessageParser(AisExtractorFactory factory) {
39  		this.factory = factory;
40  	}
41  
42  	/**
43  	 * Returns an {@link AisMessage} from the string representation of the
44  	 * message as per 1371-4 IMO specification (as per the appropriate column in
45  	 * the NMEA message). Sets source to null.
46  	 * 
47  	 * @param message
48  	 * @return
49  	 */
50  	public AisMessage parse(String message, int padBits) {
51  		return parse(message, null, padBits);
52  	}
53  
54  	/**
55  	 * Returns an {@link AisMessage} from the string representation of the
56  	 * message as per 1371-4 IMO specification (as per the appropriate column in
57  	 * the NMEA message).
58  	 * 
59  	 * @param message
60  	 * @param source
61  	 * @return
62  	 */
63  	public AisMessage parse(String message, String source, int padBits) {
64  		AisExtractor extractor = factory.create(message, 0, padBits);
65  		int id = extractor.getMessageId();
66  		
67  		if (Util.isClassAPositionReport(id)) {
68  			return new AisPositionA(message, source, padBits);
69  		} else if (id == 4)
70  			return new AisBaseStation(message, source, padBits);
71  		else if (id == 5)
72  			return new AisShipStaticA(message, source, padBits);
73  		else if (id == 18)
74  			return new AisPositionB(message, source, padBits);
75  		else if (id == 19)
76  			return new AisPositionBExtended(message, source, padBits);
77  		else if (id == 21)
78  			return new AisAidToNavigation(message, source, padBits);
79  		else if (id == AisMessageType.STATIC_DATA_REPORT.getId()) {
80  			int partNumber = AbstractAisBStaticDataReport.extractPartNumber(factory, message, padBits);
81  			return parseStaticDataReport(partNumber, id, message, source, padBits);
82  		}
83  		else if (id == 27)
84  			return new AisPositionGPS(message, source, padBits);
85  		else
86  			return new AisMessageOther(id, source, padBits);
87  	}
88  	
89  	AisMessage parseStaticDataReport(int partNumber, int id, String message, String source, int padBits) {
90  		
91          if (partNumber == AbstractAisBStaticDataReport.PART_NUMBER_A) {
92              return new AisBStaticDataReportPartA(message, source, padBits);
93          } else if (partNumber == AbstractAisBStaticDataReport.PART_NUMBER_B) {
94              return new AisBStaticDataReportPartB(message, source, padBits);
95          } else {
96              throw new AisParseException("Unsupported part number [" + partNumber + "]");
97          }
98  	}
99  }