Esempio n. 1
0
  public static ArrayList open(final String sFilename) {

    ArrayList images = null;
    final ArrayList elements = new ArrayList();
    HelpElement element = null;
    final KXmlParser parser = new KXmlParser();

    try {
      final File file = new File(sFilename);
      parser.setInput(new FileInputStream(file), encoding);
      int tag = parser.nextTag();
      boolean bOut = false;

      if (parser.getEventType() != XmlPullParser.END_DOCUMENT) {
        while ((tag != XmlPullParser.END_DOCUMENT) && !bOut) {
          switch (tag) {
            case XmlPullParser.START_TAG:
              if (parser.getName().compareTo(HELP) == 0) {
              } else if (parser.getName().compareTo(HelpElement.ELEMENT) == 0) {
                images = new ArrayList();
                final String sText = parser.getAttributeValue("", HelpElement.TEXT);
                final String sName = parser.getAttributeValue("", HelpElement.NAME);
                final String sDescription = parser.getAttributeValue("", HelpElement.DESCRIPTION);
                final int iType = Integer.parseInt(parser.getAttributeValue("", HelpElement.TYPE));
                element = new HelpElement();
                element.setText(sText);
                element.setName(sName);
                element.setType(iType);
                element.setDescription(sDescription);
              } else if (parser.getName().compareTo(ImageAndDescription.IMAGE) == 0) {
                final ImageAndDescription iad = new ImageAndDescription();
                final String sImageFilename =
                    parser.getAttributeValue("", ImageAndDescription.FILE);
                final String sDesc = parser.getAttributeValue("", ImageAndDescription.DESCRIPTION);
                iad.setDescription(sDesc);
                iad.setFilename(sImageFilename);
                images.add(iad);
              }
              break;
            case XmlPullParser.END_TAG:
              if (parser.getName().compareTo(HELP) == 0) {
                bOut = true;
              } else if (parser.getName().compareTo(HelpElement.ELEMENT) == 0) {
                element.setImages(images);
                elements.add(element);
              }
              break;
            case XmlPullParser.TEXT:
              break;
          }
          if (!bOut) {
            tag = parser.next();
          }
        }
      }

    } catch (final Exception e) {
      return null;
    }
    return elements;
  }
Esempio n. 2
0
  /**
   * Returns the help associated with a given geoalgorithm as a html-formatted string
   *
   * @param alg the geoalgorithm
   * @param sFilename the filename where help for the passed algorithm is stored
   * @return a html-formatted string with help for the given algorithm
   */
  public static String getHelpAsHTMLCode(final GeoAlgorithm alg, final String sFilename) {

    HelpElement element;
    final ArrayList list = open(sFilename);
    HashMap elements;
    final String sPath =
        "file:///" + sFilename.substring(0, sFilename.lastIndexOf(File.separator)) + File.separator;

    if (list != null) {
      elements = createMap(list);
    } else {
      elements = new HashMap();
    }

    final HTMLDoc doc = new HTMLDoc();

    doc.open(alg.getName());
    doc.addHeader(Sextante.getText(alg.getName()), 1);

    doc.addHeader(Sextante.getText("Description"), 2);

    element = (HelpElement) elements.get("DESCRIPTION");
    if (element != null) {
      doc.addParagraph(element.getTextAsFormattedHTML());
      for (int j = 0; j < element.getImages().size(); j++) {
        final ImageAndDescription iad = (ImageAndDescription) element.getImages().get(j);
        doc.addImageAndDescription(sPath + iad.getFilename(), iad.getDescription());
      }
    }

    doc.addHeader(Sextante.getText("Parameters"), 2);

    final ParametersSet params = alg.getParameters();

    doc.startUnorderedList();
    for (int i = 0; i < params.getNumberOfParameters(); i++) {
      final Parameter param = params.getParameter(i);
      String sParam = param.getParameterDescription();
      sParam = "<b>" + sParam + "[" + getParameterTypeName(param) + "]: </b>";
      element = (HelpElement) elements.get(param.getParameterName());
      if (element != null) {
        sParam = sParam + element.getTextAsFormattedHTML();
      }
      doc.addListElement(sParam);
      if (element != null) {
        for (int j = 0; j < element.getImages().size(); j++) {
          final ImageAndDescription iad = (ImageAndDescription) element.getImages().get(j);
          doc.addImageAndDescription(sPath + iad.getFilename(), iad.getDescription());
        }
      }
    }

    doc.closeUnorderedList();

    doc.addHeader(Sextante.getText("Outputs"), 2);

    element = (HelpElement) elements.get("OUTPUT_DESCRIPTION");
    if (element != null) {
      doc.addParagraph(element.getTextAsFormattedHTML());
      for (int j = 0; j < element.getImages().size(); j++) {
        final ImageAndDescription iad = (ImageAndDescription) element.getImages().get(j);
        doc.addImageAndDescription(sPath + iad.getFilename(), iad.getDescription());
      }
    }

    doc.startUnorderedList();
    final OutputObjectsSet oo = alg.getOutputObjects();
    String sOutputType = "";
    for (int i = 0; i < oo.getOutputObjectsCount(); i++) {
      final Output out = oo.getOutput(i);
      String sOutput = out.getDescription();
      if (out instanceof OutputRasterLayer) {
        sOutputType = Sextante.getText("Raster_Layer");
      } else if (out instanceof Output3DRasterLayer) {
        sOutputType = Sextante.getText("3D_Raster_layer");
      } else if (out instanceof OutputVectorLayer) {
        sOutputType = Sextante.getText("Vector_Layer");
        final OutputVectorLayer ovl = (OutputVectorLayer) out;
        switch (ovl.getShapeType()) {
          case OutputVectorLayer.SHAPE_TYPE_UNDEFINED:
          default:
            sOutputType = sOutputType + " - " + Sextante.getText("Any_type");
            break;
          case OutputVectorLayer.SHAPE_TYPE_LINE:
            sOutputType = sOutputType + " - " + Sextante.getText("Line");
            break;
          case OutputVectorLayer.SHAPE_TYPE_POLYGON:
            sOutputType = sOutputType + " - " + Sextante.getText("Polygon");
            break;
          case OutputVectorLayer.SHAPE_TYPE_POINT:
            sOutputType = sOutputType + " - " + Sextante.getText("Point");
            break;
        }
      } else if (out instanceof OutputTable) {
        sOutputType = Sextante.getText("Table");
      } else if (out instanceof OutputChart) {
        sOutputType = Sextante.getText("graph-chart");
      } else if (out instanceof OutputText) {
        sOutputType = Sextante.getText("Text");
      } else if (out instanceof OutputNumericalValue) {
        sOutputType = Sextante.getText("Numerical_value");
      }
      sOutput = "<b>" + sOutput + "[" + sOutputType + "]: </b>";
      element = (HelpElement) elements.get(out.getName());
      if (element != null) {
        sOutput = sOutput + element.getTextAsFormattedHTML();
      }
      doc.addListElement(sOutput);
      if (element != null) {
        for (int j = 0; j < element.getImages().size(); j++) {
          final ImageAndDescription iad = (ImageAndDescription) element.getImages().get(j);
          doc.addImageAndDescription(sPath + iad.getFilename(), iad.getDescription());
        }
      }
    }
    doc.closeUnorderedList();

    doc.addHeader(Sextante.getText("Additional_information"), 2);

    element = (HelpElement) elements.get("ADDITIONAL_INFO");
    if (element != null) {
      doc.addParagraph(element.getTextAsFormattedHTML());
      for (int j = 0; j < element.getImages().size(); j++) {
        final ImageAndDescription iad = (ImageAndDescription) element.getImages().get(j);
        doc.addImageAndDescription(sPath + iad.getFilename(), iad.getDescription());
      }
    }

    doc.addHeader(Sextante.getText("Command_line"), 2);

    String sText = alg.getCommandLineHelp();
    sText = sText.replaceAll("\n", "<br>");
    sText = sText.replace("   ", " &nbsp ");
    doc.addCourierText(sText);

    doc.addParagraph("");

    element = (HelpElement) elements.get("EXTENSION_AUTHOR");
    if (element != null) {
      doc.addParagraph(
          "<i>" + Sextante.getText("Algorithm_created_by") + " " + element.getText() + "</i>");
      for (int j = 0; j < element.getImages().size(); j++) {
        final ImageAndDescription iad = (ImageAndDescription) element.getImages().get(j);
        doc.addImageAndDescription(sPath + iad.getFilename(), iad.getDescription());
      }
    }

    element = (HelpElement) elements.get("HELP_AUTHOR");
    if (element != null) {
      doc.addParagraph(
          "<i>" + Sextante.getText("Help_file_created_by") + " " + element.getText() + "</i>");
      for (int j = 0; j < element.getImages().size(); j++) {
        final ImageAndDescription iad = (ImageAndDescription) element.getImages().get(j);
        doc.addImageAndDescription(sPath + iad.getFilename(), iad.getDescription());
      }
    }

    doc.close();

    return doc.getHTMLCode();
  }