Пример #1
0
  private void load_snap_from_xml(Element snap, StructureCollection structs, LinkedList tempList)
      throws VisualizerLoadException {
    Iterator iterator = snap.getChildren().iterator();

    if (!iterator.hasNext()) throw new VisualizerLoadException("Ran out of elements");

    structs.loadTitle((Element) (iterator.next()), tempList, this);
    structs.calcDimsAndStartPts(tempList, this);

    if (!iterator.hasNext()) return;
    Element child = (Element) iterator.next();

    if (child.getName().compareTo("doc_url") == 0) {
      // load the doc_url
      add_a_documentation_URL(child.getText().trim());
      if (!iterator.hasNext()) return;
      child = (Element) iterator.next();
    }

    if (child.getName().compareTo("pseudocode_url") == 0) {
      // load the psuedocode_url
      add_a_pseudocode_URL(child.getText().trim());
      if (!iterator.hasNext()) return;
      child = (Element) iterator.next();
    }

    if (child.getName().compareTo("audio_text") == 0) {
      // load the psuedocode_url
      add_audio_text(child.getText().trim());
      if (!iterator.hasNext()) return;
      child = (Element) iterator.next();
    }

    // create & load the individual structures, hooking them into the StructureCollection.
    // linespernode : calculate it at end of loadStructure call
    while (child.getName().compareTo("question_ref") != 0) {
      StructureType child_struct = assignStructureType(child);
      child_struct.loadStructure(child, tempList, this);
      structs.addChild(child_struct);

      if (!iterator.hasNext()) return;
      child = (Element) iterator.next();
    }

    if (child.getName().compareTo("question_ref") == 0) {
      // set up question ref
      add_a_question_xml(child.getAttributeValue("ref"));
    } else
      // should never happen
      throw new VisualizerLoadException(
          "Expected question_ref element, but found " + child.getName());

    if (iterator.hasNext()) {
      child = (Element) iterator.next();
      throw new VisualizerLoadException(
          "Found " + child.getName() + " when expecting end of snap.");
    }
  } // load_snap_from_xml
Пример #2
0
  /** Make a keybinding out of an XML element */
  public static KeyBinding readXML(Element e) {
    try {
      String plugin = e.getAttributeValue("plugin");
      String desc = e.getAttributeValue("desc");
      KeyBinding b = new KeyBinding(plugin, desc, null);

      for (Object neo : e.getChildren()) {
        Element ne = (Element) neo;
        if (ne.getName().equals("char"))
          b.types.add(new TypeChar(ne.getAttributeValue("char").charAt(0)));
        else if (ne.getName().equals("keycode"))
          b.types.add(
              new TypeKeycode(
                  ne.getAttribute("keyCode").getIntValue(),
                  ne.getAttribute("modifier").getIntValue()));
        else if (ne.getName().equals("jinput"))
          b.types.add(
              new TypeJInput(
                  ne.getAttributeValue("ident"), ne.getAttribute("value").getFloatValue()));
      }

      // backwards compatibility
      if (e.getAttribute("key") != null) {
        TypeChar kb = new TypeChar(e.getAttributeValue("key").charAt(0));
        b.types.add(kb);
      } else if (e.getAttribute("keyCode") != null) {
        TypeKeycode kb =
            new TypeKeycode(
                e.getAttribute("keyCode").getIntValue(), e.getAttribute("modifier").getIntValue());
        b.types.add(kb);
      }
      return b;
    } catch (DataConversionException e1) {
      e1.printStackTrace();
      return null;
    }
  }
Пример #3
0
 public void write(Writer out, Document doc, int pos, int len, Map<String, String> copiedImgs)
     throws IOException, BadLocationException {
   Debug.log(9, "SikuliEditorKit.write %d %d", pos, len);
   DefaultStyledDocument sdoc = (DefaultStyledDocument) doc;
   int i = pos;
   String absPath;
   while (i < pos + len) {
     Element e = sdoc.getCharacterElement(i);
     int start = e.getStartOffset(), end = e.getEndOffset();
     if (e.getName().equals(StyleConstants.ComponentElementName)) {
       // A image argument to be filled
       AttributeSet attr = e.getAttributes();
       Component com = StyleConstants.getComponent(attr);
       out.write(com.toString());
       if (copiedImgs != null
           && (com instanceof EditorPatternButton || com instanceof EditorPatternLabel)) {
         if (com instanceof EditorPatternButton) {
           absPath = ((EditorPatternButton) com).getFilename();
         } else {
           absPath = ((EditorPatternLabel) com).getFile();
         }
         String fname = (new File(absPath)).getName();
         copiedImgs.put(fname, absPath);
         Debug.log(3, "save image for copy&paste: " + fname + " -> " + absPath);
       }
     } else {
       if (start < pos) {
         start = pos;
       }
       if (end > pos + len) {
         end = pos + len;
       }
       out.write(doc.getText(start, end - start));
     }
     i = end;
   }
   out.close();
 }
Пример #4
0
 private StructureType assignStructureType(Element structure) throws VisualizerLoadException {
   String name = structure.getName().toLowerCase();
   if (name.compareTo("graph") == 0) {
     if (structure.getAttributeValue("weighted").compareTo("true") == 0)
       return new Graph_Network("NETWORK");
     else return new Graph_Network("GRAPH");
   } else if (name.equals("array")) return new MD_Array();
   else if (name.equals("no3darray")) return new No3darray();
   else if (name.equals("linkedlist")) return new VisLinkedList();
   else if (name.equals("no3dlinkedlist")) return new No3dLinkedList();
   else if (name.equals("linkedlistnonull")) return new VisLinkedListNoNull();
   else if (name.equals("bargraph")) return new BarScat("BAR");
   else if (name.equals("scattergraph"))
     return new BarScat(
         "SCAT"); // not implemented in xml dtd, and does not have a load-from-xml method defined
   else if (name.equals("stack")) return new Stack();
   else if (name.equals("queue")) return new Queue();
   else if (name.equals("tree")) {
     if (structure.getChild("binary_node") != null) return new BinaryTree();
     else return new GeneralTree();
   } else if (name.equals("text")) return new TextStructure();
   // if the XML element name is different from your structure's name, you can do something like
   // this:
   // 	else if( name.equalsIgnoreCase("node"))
   // 	    return new Node();
   else if (name.equals("legend")) return new LegendofColors();
   else {
     // try dynamic typing
     try {
       return assignStructureType(name);
     } catch (Exception e) {
       throw new VisualizerLoadException(
           "Unable to instantiate class \"" + name + "\":\n" + e.getMessage());
     }
   }
 }