gt:                         String imgName = imgFile.getName();
context: /*
   * Geopaparazzi - Digital field mapping on Android based devices
   * Copyright (C) 2010  HydroloGIS (www.hydrologis.com)
   *
   * This program is free software: you can redistribute it and/or modify
   * it under the terms of the GNU General Public License as published by
   * the Free Software Foundation, either version 3 of the License, or
   * (at your option) any later version.
   *
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU General Public License for more details.
   *
   * You should have received a copy of the GNU General Public License
   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
   */
  package eu.hydrologis.geopaparazzi.util;
  
  import org.json.JSONArray;
  import org.json.JSONObject;
  
  import java.io.File;
  import java.util.ArrayList;
  import java.util.List;
  
  import eu.geopaparazzi.library.database.INote;
  import eu.geopaparazzi.library.database.Image;
  import eu.geopaparazzi.library.forms.FormUtilities;
  import eu.geopaparazzi.library.forms.TagsManager;
  import eu.geopaparazzi.library.gpx.GpxRepresenter;
  import eu.geopaparazzi.library.gpx.GpxUtilities;
  import eu.geopaparazzi.library.kml.KmlRepresenter;
  import eu.geopaparazzi.library.util.Utilities;
  import eu.hydrologis.geopaparazzi.database.DaoImages;
  
  /**
   * Represents a note (log or map).
   *
   * @author Andrea Antonello (www.hydrologis.com)
   */
  public class Note implements INote, KmlRepresenter, GpxRepresenter {
      public static final String IMAGES_SEPARATOR = ";";
      private final String simpleText;
      private final String description;
      private final long timeStamp;
      private final long id;
      private String style;
      private final double lon;
      private final double lat;
      private final double altim;
      private final String form;
      private final int isDirty;
      private List<String> images = null;
  
      /**
       * A wrapper for a note.
       *
       * @param id          the note's id.
       * @param text        the simple text of the note.
       * @param description a description for the note
       * @param timeStamp   the timestamp.
       * @param lon         lon
       * @param lat         lat
       * @param altim       elevation
       * @param form        the form data .
       * @param isDirty     isDirty flag.
       */
      public Note(long id, String text, String description, long timeStamp, double lon, double lat, double altim,
                  String form, int isDirty, String style) {
          this.id = id;
          this.style = style;
          if (text != null) {
              this.simpleText = text;
          } else {
              this.simpleText = ""; //$NON-NLS-1$
          }
          if (description != null) {
              this.description = description;
          } else {
              this.description = ""; //$NON-NLS-1$
          }
          this.timeStamp = timeStamp;
          this.lon = lon;
          this.lat = lat;
          this.altim = altim;
          this.form = form;
          this.isDirty = isDirty;
      }
  
      public long getId() {
          return id;
      }
  
      public double getLat() {
          return lat;
      }
  
      public double getLon() {
          return lon;
      }
  
      /**
       * @return the elevation.
       */
      public double getAltim() {
          return altim;
      }
  
      public String getName() {
          return simpleText;
      }
  
      /**
       * @return description.
       */
      public String getDescription() {
          return description;
      }
  
      /**
       * @return form json.
       */
      public String getForm() {
          return form;
      }
  
      public String getStyle() {
          return style;
      }
  
      /**
       * @return the isDirty.
       */
      public int getIsDirty() {
          return isDirty;
      }
  
      @SuppressWarnings("nls")
      public String toKmlString() throws Exception {
          images = new ArrayList<String>();
          String name = Utilities.makeXmlSafe(this.simpleText);
          StringBuilder sB = new StringBuilder();
          sB.append("<Placemark>\n");
          // sB.append("<styleUrl>#red-pushpin</styleUrl>\n");
          sB.append("<styleUrl>#info-icon</styleUrl>\n");
          sB.append("<simpleText>").append(name).append("</simpleText>\n");
          sB.append("<description>\n");
  
          if (form != null && form.length() > 0) {
              DaoImages daoImages = new DaoImages();
  
  
              sB.append("<![CDATA[\n");
              JSONObject sectionObject = new JSONObject(form);
              if (sectionObject.has(FormUtilities.ATTR_SECTIONNAME)) {
                  String sectionName = sectionObject.getString(FormUtilities.ATTR_SECTIONNAME);
                  sB.append("<h1>").append(sectionName).append("</h1>\n");
              }
  
              List<String> formsNames = TagsManager.getFormNames4Section(sectionObject);
              for (String formName : formsNames) {
                  sB.append("<h2>").append(formName).append("</h2>\n");
  
                  sB.append("<table style=\"text-align: left; width: 100%;\" border=\"1\" cellpadding=\"5\" cellspacing=\"2\">");
                  sB.append("<tbody>");
  
                  JSONObject form4Name = TagsManager.getForm4Name(formName, sectionObject);
                  JSONArray formItems = TagsManager.getFormItems(form4Name);
                  for (int i = 0; i < formItems.length(); i++) {
                      JSONObject formItem = formItems.getJSONObject(i);
                      if (!formItem.has(FormUtilities.TAG_KEY)) {
                          continue;
                      }
  
                      String type = formItem.getString(FormUtilities.TAG_TYPE);
                      String key = formItem.getString(FormUtilities.TAG_KEY);
                      String value = formItem.getString(FormUtilities.TAG_VALUE);
  
                      if (type.equals(FormUtilities.TYPE_PICTURES)) {
                          if (value.trim().length() == 0) {
                              continue;
                          }
                          String[] imageIdsSplit = value.split(IMAGES_SEPARATOR);
                          for (String imageId : imageIdsSplit) {
                              Image image = daoImages.getImage(Long.parseLong(imageId));
                              String imgName = image.getName();
                              sB.append("<tr>");
                              sB.append("<td colspan=\"2\" style=\"text-align: left; vertical-align: top; width: 100%;\">");
                              sB.append("<img src=\"" + imgName + "\" width=\"300\">");
                              sB.append("</td>");
                              sB.append("</tr>");
  
                              images.add(imageId);
                          }
                      } else if (type.equals(FormUtilities.TYPE_MAP)) {
                          if (value.trim().length() == 0) {
                              continue;
                          }
                          sB.append("<tr>");
                          // FIXME
                          String image = value.trim();
                          File imgFile = new File(image);
  