OpenLRXMLDecoder implements PhysicalDecoder { @Override public final RawLocationReference decodeData(final LocationReference lr) throws PhysicalFormatException { Object data = lr.getLocationReferenceData(); if (!(data instanceof OpenLR)) { throw new OpenLRXMLException(XMLErrorType.DATA_ERROR, "incorrect data class"); } OpenLR xmlData = (OpenLR) data; String id = xmlData.getLocationID(); XMLLocationReference xmlLoc = xmlData.getXMLLocationReference(); if (xmlLoc == null) { throw new OpenLRXMLException(XMLErrorType.BINARY_DATA_ONLY, "cannot decode binary string using xml package"); } LineLocationReference lineLoc = xmlLoc.getLineLocationReference(); PointLocationReference pointLoc = xmlLoc.getPointLocationReference(); AreaLocationReference areaLoc = xmlLoc.getAreaLocationReference(); if (lineLoc == null && pointLoc == null && areaLoc == null) { throw new OpenLRXMLException(XMLErrorType.DATA_ERROR, "no location reference"); } RawLocationReference rawLocRef = null; if (lineLoc != null) { LineDecoder decoder = new LineDecoder(); rawLocRef = decoder.decodeData(id, lineLoc); } else if (pointLoc != null) { GeoCoordinate gCoord = pointLoc.getGeoCoordinate(); PointAlongLine pal = pointLoc.getPointAlongLine(); PoiWithAccessPoint palwap = pointLoc.getPoiWithAccessPoint(); if (gCoord == null && pal == null && palwap == null) { throw new OpenLRXMLException(XMLErrorType.DATA_ERROR, "no point location found"); } if (gCoord != null) { GeoCoordDecoder decoder = new GeoCoordDecoder(); rawLocRef = decoder.decodeData(id, gCoord); } else if (pal != null) { PointAlongDecoder decoder = new PointAlongDecoder(); rawLocRef = decoder.decodeData(id, pal); } else if (palwap != null) { PoiAccessDecoder decoder = new PoiAccessDecoder(); rawLocRef = decoder.decodeData(id, palwap); } } else if (areaLoc != null) { CircleLocationReference circleLoc = areaLoc .getCircleLocationReference(); RectangleLocationReference rectangleLoc = areaLoc .getRectangleLocationReference(); GridLocationReference gridLoc = areaLoc.getGridLocationReference(); PolygonLocationReference polygonLoc = areaLoc .getPolygonLocationReference(); ClosedLineLocationReference closedLineLoc = areaLoc .getClosedLineLocationReference(); if (circleLoc == null && polygonLoc == null && rectangleLoc == null && gridLoc == null && closedLineLoc == null) { throw new OpenLRXMLException(XMLErrorType.DATA_ERROR, "no area location found"); } if (circleLoc != null) { CircleDecoder circleDecoder = new CircleDecoder(); rawLocRef = circleDecoder.decodeData(id, circleLoc); } else if (rectangleLoc != null) { RectangleDecoder rectangleDecoder = new RectangleDecoder(); rawLocRef = rectangleDecoder.decodeData(id, rectangleLoc); } else if (gridLoc != null) { GridDecoder gridDecoder = new GridDecoder(); rawLocRef = gridDecoder.decodeData(id, gridLoc); } else if (polygonLoc != null) { PolygonDecoder polygonDecoder = new PolygonDecoder(); rawLocRef = polygonDecoder.decodeData(id, polygonLoc); } else if (closedLineLoc != null) { ClosedLineDecoder closedLineDecoder = new ClosedLineDecoder(); rawLocRef = closedLineDecoder.decodeData(id, closedLineLoc); } } return rawLocRef; }  @Override final RawLocationReference decodeData(final LocationReference lr); final Class<?> getDataClass(); final String getDataFormatIdentifier();  }
@Test public final void testWhitePaperLineLocation() { OpenLRXMLDecoder dec = new OpenLRXMLDecoder(); try { LocationReference lr = new LocationReferenceXmlImpl("", whitePaperLineLocation, 1); RawLocationReference decodedData = dec .decodeData(lr); assertSame(decodedData.getLocationType(), LocationType.LINE_LOCATION); assertTrue(decodedData.isValid()); assertNull(decodedData.getReturnCode()); Lrp[] expectedLrps = new Lrp[]{Lrp.LINE_ENC_LRP1, Lrp.LINE_ENC_LRP2, Lrp.LINE_ENC_LRP3}; Utils.checkDecodedLrps(decodedData, expectedLrps, true); checkOffsets(decodedData.getOffsets(), POSITIVE_OFFSET_WP_LINE_LOCATION, null); assertNull(decodedData.getGeoCoordinates()); assertNull(decodedData.getOrientation()); assertNull(decodedData.getSideOfRoad()); } catch (PhysicalFormatException e) { fail("failed on valid input", e); } }
@Test public final void testInvalidLocation() { OpenLRXMLDecoder dec = new OpenLRXMLDecoder(); try { LocationReference lr = new LocationReferenceXmlImpl("", invalidXML, 1); dec.decodeData(lr); fail("exception expected"); } catch (PhysicalFormatException e) { assertNotNull(e); } }
@Test public final void testMissingLastLrpLocation() { OpenLRXMLDecoder dec = new OpenLRXMLDecoder(); try { LocationReference lr = new LocationReferenceXmlImpl("", missingLastLrp, 1); RawLocationReference ref = dec.decodeData(lr); assertFalse(ref.isValid()); assertEquals(ref.getLocationType(), LocationType.LINE_LOCATION); assertSame(ref.getReturnCode(), XmlReturnCode.NO_LAST_LRP_FOUND); assertNull(ref.getGeoCoordinates()); assertNull(ref.getLocationReferencePoints()); assertNull(ref.getOffsets()); assertNull(ref.getOrientation()); assertNull(ref.getSideOfRoad()); } catch (PhysicalFormatException e) { fail("Unexpected exception!", e); } }
@Test public final void testWrongDataClassDecoding() { OpenLR wpPaperLineLocation = Utils.readLocationFromFile( "whitePointAlongLocation.xml", true); try { new LineDecoder().decodeData("", wpPaperLineLocation); fail("Exception expected!"); } catch (PhysicalFormatException e) { assertSame(e.getErrorCode(), XMLErrorType.DATA_ERROR); } }