@Test
  public void balloonStyleWriteTest()
      throws KmlException, IOException, XMLStreamException, ParserConfigurationException,
          SAXException, URISyntaxException {
    final KmlFactory kmlFactory = DefaultKmlFactory.getInstance();

    final Coordinate coordinate = kmlFactory.createCoordinate(-122.370533, 37.823842, 0.0);
    final CoordinateSequence coordinates = kmlFactory.createCoordinates(Arrays.asList(coordinate));
    final Point point = kmlFactory.createPoint(coordinates);

    final Feature placemark = kmlFactory.createPlacemark();
    final Collection<Property> placemarkProperties = placemark.getProperties();
    placemarkProperties.add(FF.createAttribute("BalloonStyle", KmlModelConstants.ATT_NAME, null));
    placemarkProperties.add(
        FF.createAttribute("An example of BalloonStyle", KmlModelConstants.ATT_DESCRIPTION, null));
    placemarkProperties.add(
        FF.createAttribute(new URI("#exampleBalloonStyle"), KmlModelConstants.ATT_STYLE_URL, null));
    placemarkProperties.add(
        FF.createAttribute(point, KmlModelConstants.ATT_PLACEMARK_GEOMETRY, null));

    final BalloonStyle balloonStyle = kmlFactory.createBalloonStyle();
    final Cdata text =
        new DefaultCdata(
            "\n      <b><font color=\"#CC0000\" size=\"+3\">$[name]</font></b>\n"
                + "      <br/><br/>\n"
                + "      <font face=\"Courier\">$[description]</font>\n"
                + "      <br/><br/>\n"
                + "      Extra text that will appear in the description balloon\n"
                + "      <br/><br/>\n"
                + "      $[geDirections]\n"
                + "      ");
    balloonStyle.setText(text);
    balloonStyle.setBgColor(new Color(187, 255, 255, 255));

    final IdAttributes idAttributes = kmlFactory.createIdAttributes("exampleBalloonStyle", null);

    final Style style = kmlFactory.createStyle();
    style.setIdAttributes(idAttributes);
    style.setBalloonStyle(balloonStyle);

    final Feature document = kmlFactory.createDocument();
    final Collection<Property> documentProperties = document.getProperties();
    documentProperties.add(
        FF.createAttribute("BalloonStyle.kml", KmlModelConstants.ATT_NAME, null));
    document.getProperty(KmlModelConstants.ATT_OPEN.getName()).setValue(Boolean.TRUE);
    documentProperties.add(FF.createAttribute(style, KmlModelConstants.ATT_STYLE_SELECTOR, null));
    documentProperties.add(
        FeatureUtilities.wrapProperty(placemark, KmlModelConstants.ATT_DOCUMENT_FEATURES));
    final Kml kml = kmlFactory.createKml(null, document, null, null);

    final File temp = File.createTempFile("testBalloonStyle", ".kml");
    temp.deleteOnExit();

    final KmlWriter writer = new KmlWriter();
    writer.setOutput(temp);
    writer.write(kml);
    writer.dispose();

    DomCompare.compare(new File(pathToTestFile), temp);
  }
  @Test
  public void balloonStyleReadTest()
      throws IOException, XMLStreamException, URISyntaxException, KmlException {

    Iterator i;

    final KmlReader reader = new KmlReader();
    reader.setInput(new File(pathToTestFile));
    final Kml kmlObjects = reader.read();
    reader.dispose();

    final Feature document = kmlObjects.getAbstractFeature();
    assertTrue(document.getType().equals(KmlModelConstants.TYPE_DOCUMENT));
    assertEquals(
        "BalloonStyle.kml", document.getProperty(KmlModelConstants.ATT_NAME.getName()).getValue());
    assertTrue((Boolean) document.getProperty(KmlModelConstants.ATT_OPEN.getName()).getValue());
    assertEquals(1, document.getProperties(KmlModelConstants.ATT_STYLE_SELECTOR.getName()).size());
    i = document.getProperties(KmlModelConstants.ATT_STYLE_SELECTOR.getName()).iterator();
    if (i.hasNext()) {
      Object styleSelector = ((Property) i.next()).getValue();
      assertTrue(styleSelector instanceof Style);
      final Style style = (Style) styleSelector;
      assertEquals("exampleBalloonStyle", style.getIdAttributes().getId());
      final BalloonStyle balloonStyle = style.getBalloonStyle();
      assertEquals(new Color(187, 255, 255, 255), balloonStyle.getBgColor());
      final Cdata text =
          new DefaultCdata(
              "\n      <b><font color=\"#CC0000\" size=\"+3\">$[name]</font></b>\n"
                  + "      <br/><br/>\n"
                  + "      <font face=\"Courier\">$[description]</font>\n"
                  + "      <br/><br/>\n"
                  + "      Extra text that will appear in the description balloon\n"
                  + "      <br/><br/>\n"
                  + "      $[geDirections]\n"
                  + "      ");
      assertEquals(text, balloonStyle.getText());
    }

    assertEquals(
        1, document.getProperties(KmlModelConstants.ATT_DOCUMENT_FEATURES.getName()).size());
    i = document.getProperties(KmlModelConstants.ATT_DOCUMENT_FEATURES.getName()).iterator();
    if (i.hasNext()) {
      final Object object = i.next();
      assertTrue(object instanceof Feature);
      Feature placemark = (Feature) object;
      assertTrue(placemark.getType().equals(KmlModelConstants.TYPE_PLACEMARK));
      assertEquals(
          "BalloonStyle", placemark.getProperty(KmlModelConstants.ATT_NAME.getName()).getValue());
      assertEquals(
          "An example of BalloonStyle",
          placemark.getProperty(KmlModelConstants.ATT_DESCRIPTION.getName()).getValue());
      assertEquals(
          new URI("#exampleBalloonStyle"),
          placemark.getProperty(KmlModelConstants.ATT_STYLE_URL.getName()).getValue());
      assertTrue(
          placemark.getProperty(KmlModelConstants.ATT_PLACEMARK_GEOMETRY.getName()).getValue()
              instanceof Point);
      final Point point =
          (Point)
              placemark.getProperty(KmlModelConstants.ATT_PLACEMARK_GEOMETRY.getName()).getValue();
      final CoordinateSequence coordinates = point.getCoordinateSequence();
      assertEquals(1, coordinates.size());
      final Coordinate coordinate = coordinates.getCoordinate(0);
      assertEquals(-122.370533, coordinate.x, DELTA);
      assertEquals(37.823842, coordinate.y, DELTA);
      assertEquals(0, coordinate.z, DELTA);
    }
  }
  @Test
  public void lineStringReadTest()
      throws IOException, XMLStreamException, KmlException, URISyntaxException {

    final KmlReader reader = new KmlReader();
    reader.setInput(new File(pathToTestFile));
    final Kml kmlObjects = reader.read();
    reader.dispose();

    final Feature document = kmlObjects.getAbstractFeature();
    assertTrue(document.getType().equals(KmlModelConstants.TYPE_DOCUMENT));

    assertEquals(
        "LineString.kml", document.getProperty(KmlModelConstants.ATT_NAME.getName()).getValue());
    assertTrue((Boolean) document.getProperty(KmlModelConstants.ATT_OPEN.getName()).getValue());

    assertTrue(
        document.getProperty(KmlModelConstants.ATT_VIEW.getName()).getValue() instanceof LookAt);
    final LookAt lookAt =
        (LookAt) document.getProperty(KmlModelConstants.ATT_VIEW.getName()).getValue();

    assertEquals(-122.36415, lookAt.getLongitude(), DELTA);
    assertEquals(37.824553, lookAt.getLatitude(), DELTA);
    assertEquals(1, lookAt.getAltitude(), DELTA);
    assertEquals(2, lookAt.getHeading(), DELTA);
    assertEquals(50, lookAt.getTilt(), DELTA);
    assertEquals(150, lookAt.getRange(), DELTA);

    assertEquals(
        2, document.getProperties(KmlModelConstants.ATT_DOCUMENT_FEATURES.getName()).size());

    Iterator i =
        document.getProperties(KmlModelConstants.ATT_DOCUMENT_FEATURES.getName()).iterator();

    if (i.hasNext()) {
      Object object = i.next();
      assertTrue(object instanceof Feature);
      Feature placemark0 = (Feature) object;
      assertTrue(placemark0.getType().equals(KmlModelConstants.TYPE_PLACEMARK));

      assertEquals(
          "unextruded", placemark0.getProperty(KmlModelConstants.ATT_NAME.getName()).getValue());
      assertTrue(
          placemark0.getProperty(KmlModelConstants.ATT_PLACEMARK_GEOMETRY.getName()).getValue()
              instanceof LineString);
      final LineString lineString0 =
          (LineString)
              placemark0.getProperty(KmlModelConstants.ATT_PLACEMARK_GEOMETRY.getName()).getValue();
      assertTrue(lineString0.getExtrude());
      assertTrue(lineString0.getTessellate());

      final CoordinateSequence coordinates0 = lineString0.getCoordinateSequence();
      assertEquals(2, coordinates0.size());

      final Coordinate coordinate00 = coordinates0.getCoordinate(0);
      assertEquals(-122.364383, coordinate00.x, DELTA);
      assertEquals(37.824664, coordinate00.y, DELTA);
      assertEquals(0, coordinate00.z, DELTA);

      final Coordinate coordinate01 = coordinates0.getCoordinate(1);
      assertEquals(-122.364152, coordinate01.x, DELTA);
      assertEquals(37.824322, coordinate01.y, DELTA);
      assertEquals(0, coordinate01.z, DELTA);
    }

    if (i.hasNext()) {
      Object object = i.next();
      assertTrue(object instanceof Feature);
      Feature placemark1 = (Feature) object;
      assertTrue(placemark1.getType().equals(KmlModelConstants.TYPE_PLACEMARK));

      assertEquals(
          "extruded", placemark1.getProperty(KmlModelConstants.ATT_NAME.getName()).getValue());
      assertTrue(
          placemark1.getProperty(KmlModelConstants.ATT_PLACEMARK_GEOMETRY.getName()).getValue()
              instanceof LineString);
      final LineString lineString1 =
          (LineString)
              placemark1.getProperty(KmlModelConstants.ATT_PLACEMARK_GEOMETRY.getName()).getValue();
      assertTrue(lineString1.getExtrude());
      assertTrue(lineString1.getTessellate());
      assertEquals(EnumAltitudeMode.RELATIVE_TO_GROUND, lineString1.getAltitudeMode());

      final CoordinateSequence coordinates1 = lineString1.getCoordinateSequence();
      assertEquals(2, coordinates1.size());

      final Coordinate coordinate10 = coordinates1.getCoordinate(0);
      assertEquals(-122.364167, coordinate10.x, DELTA);
      assertEquals(37.824787, coordinate10.y, DELTA);
      assertEquals(50, coordinate10.z, DELTA);

      final Coordinate coordinate11 = coordinates1.getCoordinate(1);
      assertEquals(-122.363917, coordinate11.x, DELTA);
      assertEquals(37.824423, coordinate11.y, DELTA);
      assertEquals(50, coordinate11.z, DELTA);
    }
  }
  @Test
  public void lineStringWriteTest()
      throws KmlException, IOException, XMLStreamException, ParserConfigurationException,
          SAXException {
    final KmlFactory kmlFactory = DefaultKmlFactory.getInstance();

    final Coordinate coordinate00 = kmlFactory.createCoordinate(-122.364383, 37.824664, 0.0);
    final Coordinate coordinate01 = kmlFactory.createCoordinate(-122.364152, 37.824322, 0.0);
    final Coordinate coordinate10 = kmlFactory.createCoordinate(-122.364167, 37.824787, 50.0);
    final Coordinate coordinate11 = kmlFactory.createCoordinate(-122.363917, 37.824423, 50.0);

    final CoordinateSequence coordinates0 =
        kmlFactory.createCoordinates(Arrays.asList(coordinate00, coordinate01));
    final CoordinateSequence coordinates1 =
        kmlFactory.createCoordinates(Arrays.asList(coordinate10, coordinate11));

    final LineString lineString0 = kmlFactory.createLineString(coordinates0);
    lineString0.setExtrude(true);
    lineString0.setTessellate(true);

    final LineString lineString1 = kmlFactory.createLineString(coordinates1);
    lineString1.setExtrude(true);
    lineString1.setTessellate(true);
    lineString1.setAltitudeMode(EnumAltitudeMode.RELATIVE_TO_GROUND);

    final Feature placemark0 = kmlFactory.createPlacemark();
    final Collection<Property> placemark0Properties = placemark0.getProperties();
    placemark0Properties.add(FF.createAttribute("unextruded", KmlModelConstants.ATT_NAME, null));
    placemark0Properties.add(
        FF.createAttribute(lineString0, KmlModelConstants.ATT_PLACEMARK_GEOMETRY, null));

    final Feature placemark1 = kmlFactory.createPlacemark();
    final Collection<Property> placemark1Properties = placemark1.getProperties();
    placemark1Properties.add(FF.createAttribute("extruded", KmlModelConstants.ATT_NAME, null));
    placemark1Properties.add(
        FF.createAttribute(lineString1, KmlModelConstants.ATT_PLACEMARK_GEOMETRY, null));

    final LookAt lookAt = kmlFactory.createLookAt();
    lookAt.setLongitude(-122.36415);
    lookAt.setLatitude(37.824553);
    lookAt.setAltitude(1);
    lookAt.setHeading(2);
    lookAt.setTilt(50);
    lookAt.setRange(150);

    final Feature document = kmlFactory.createDocument();
    final Collection<Property> documentProperties = document.getProperties();
    documentProperties.add(FF.createAttribute("LineString.kml", KmlModelConstants.ATT_NAME, null));
    document.getProperty(KmlModelConstants.ATT_OPEN.getName()).setValue(Boolean.TRUE);
    documentProperties.add(FF.createAttribute(lookAt, KmlModelConstants.ATT_VIEW, null));
    documentProperties.add(
        FeatureUtilities.wrapProperty(placemark0, KmlModelConstants.ATT_DOCUMENT_FEATURES));
    documentProperties.add(
        FeatureUtilities.wrapProperty(placemark1, KmlModelConstants.ATT_DOCUMENT_FEATURES));

    final Kml kml = kmlFactory.createKml(null, document, null, null);

    final File temp = File.createTempFile("testLineString", ".kml");
    temp.deleteOnExit();

    final KmlWriter writer = new KmlWriter();
    writer.setOutput(temp);
    writer.write(kml);
    writer.dispose();

    DomCompare.compare(new File(pathToTestFile), temp);
  }