Example #1
0
  public static void main(String[] args) {

    try {

      // Find the implementation
      DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
      factory.setNamespaceAware(true);
      DocumentBuilder builder = factory.newDocumentBuilder();
      DOMImplementation impl = builder.getDOMImplementation();

      // Create the document
      DocumentType svgDOCTYPE =
          impl.createDocumentType(
              "svg",
              "-//W3C//DTD SVG 1.0//EN",
              "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd");
      Document doc = impl.createDocument("http://www.w3.org/2000/svg", "svg", svgDOCTYPE);

      // Fill the document
      Node rootElement = doc.getDocumentElement();
      ProcessingInstruction xmlstylesheet =
          doc.createProcessingInstruction(
              "xml-stylesheet", "type=\"text/css\" href=\"standard.css\"");
      Comment comment = doc.createComment("An example from Chapter 10 of Processing XML with Java");
      doc.insertBefore(comment, rootElement);
      doc.insertBefore(xmlstylesheet, rootElement);
      Node desc = doc.createElementNS("http://www.w3.org/2000/svg", "desc");
      rootElement.appendChild(desc);
      Text descText = doc.createTextNode("An example from Processing XML with Java");
      desc.appendChild(descText);

      // Serialize the document onto System.out
      TransformerFactory xformFactory = TransformerFactory.newInstance();
      Transformer idTransform = xformFactory.newTransformer();
      Source input = new DOMSource(doc);
      Result output = new StreamResult(System.out);
      idTransform.transform(input, output);

    } catch (FactoryConfigurationError e) {
      System.out.println("Could not locate a JAXP factory class");
    } catch (ParserConfigurationException e) {
      System.out.println("Could not locate a JAXP DocumentBuilder class");
    } catch (DOMException e) {
      System.err.println(e);
    } catch (TransformerConfigurationException e) {
      System.err.println(e);
    } catch (TransformerException e) {
      System.err.println(e);
    }
  }
Example #2
0
    /**
     * Draws a concreteDiagram as an SVG.
     *
     * <p>This approach is wholly declarative. It currently knows nothing about the on screen
     * rendering of the diagram. To make decisions based on the on screen rendering (such as better
     * label placement) we will, in future, have to build a GVT (from the Batik library) of the
     * SVGDocument.
     *
     * @returns An SVGDocument DOM structure representing the SVG.
     */
    @Override
    public SVGDocument toSVG(ConcreteDiagram cd) {
      // Get a DOMImplementation.
      DOMImplementation domImpl = SVGDOMImplementation.getDOMImplementation();

      // Create an instance of org.w3c.dom.Document.
      String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
      SVGDocument document = (SVGDocument) domImpl.createDocument(svgNS, "svg", null);

      // Get the root element (the 'svg' element).
      Element svgRoot = document.getDocumentElement();

      // Set the width and height attributes on the root 'svg' element.
      svgRoot.setAttributeNS(null, "width", Integer.toString(cd.getSize()));
      svgRoot.setAttributeNS(null, "height", Integer.toString(cd.getSize()));

      // Draw the shaded zones
      for (ConcreteZone z : cd.getShadedZones()) {
        Element path = document.createElementNS(svgNS, "path");
        path.setAttributeNS(null, "d", toSVGPath(z.getShape(cd.getBox())));
        path.setAttributeNS(null, "fill", "#cccccc"); // grey
        path.setAttributeNS(null, "z-index", Integer.toString(zOrder.SHADING.ordinal()));

        svgRoot.appendChild(path);
      }

      // TODO: Concrete* should return themselves as DocumentFragments
      for (CircleContour c : cd.getCircles()) {
        // Draw the circle
        Element circle = document.createElementNS(svgNS, "circle");
        circle.setAttributeNS(null, "cx", Double.toString(c.get_cx()));
        circle.setAttributeNS(null, "cy", Double.toString(c.get_cy()));
        circle.setAttributeNS(null, "r", Double.toString(c.get_radius()));
        circle.setAttributeNS(null, "z-index", Integer.toString(zOrder.CONTOUR.ordinal()));
        // Not pretty, but it works.
        Color strokeColor = c.color();
        circle.setAttributeNS(
            null, "stroke", (null == strokeColor) ? "black" : "#" + toHexString(c.color()));
        circle.setAttributeNS(null, "stroke-width", "2");
        circle.setAttributeNS(null, "fill", "none");
        svgRoot.appendChild(circle);

        // TODO: Put this text in a path around the circle
        // alternatively come up with some better label placement
        // algorithm
        Element text = document.createElementNS(svgNS, "text");
        text.setAttributeNS(null, "x", Double.toString(c.get_cx()));
        text.setAttributeNS(null, "y", Double.toString(c.get_cy() + c.get_radius()));
        text.setAttributeNS(null, "text-anchor", "middle");
        text.setAttributeNS(
            null, "fill", (null == strokeColor) ? "black" : "#" + toHexString(c.color()));
        text.setAttributeNS(null, "z-index", Integer.toString(zOrder.LABEL.ordinal()));

        Text textNode = document.createTextNode(c.ac.getLabel().getLabel());
        text.appendChild(textNode);
        svgRoot.appendChild(text);
      }

      for (ConcreteSpider cs : cd.getSpiders()) {
        for (ConcreteSpiderFoot f : cs.feet) {
          // Draw the foot
          Element circle = document.createElementNS(svgNS, "circle");
          circle.setAttributeNS(null, "cx", Double.toString(f.getX()));
          circle.setAttributeNS(null, "cy", Double.toString(f.getY()));
          circle.setAttributeNS(null, "r", Double.toString(ConcreteSpiderFoot.FOOT_RADIUS));
          circle.setAttributeNS(null, "z-index", Integer.toString(zOrder.SPIDER.ordinal()));

          circle.setAttributeNS(null, "stroke", "black");
          circle.setAttributeNS(null, "stroke-width", "2");
          circle.setAttributeNS(null, "fill", "black");
          svgRoot.appendChild(circle);
        }

        for (ConcreteSpiderLeg l : cs.legs) {
          Element line = document.createElementNS(svgNS, "line");
          line.setAttributeNS(null, "x1", Double.toString(l.from.getX()));
          line.setAttributeNS(null, "y1", Double.toString(l.from.getY()));
          line.setAttributeNS(null, "x2", Double.toString(l.to.getX()));
          line.setAttributeNS(null, "y2", Double.toString(l.to.getY()));
          line.setAttributeNS(null, "z-index", Integer.toString(zOrder.SPIDER.ordinal()));

          line.setAttributeNS(null, "stroke", "black");
          line.setAttributeNS(null, "stroke-width", "2");
          line.setAttributeNS(null, "fill", "black");
          svgRoot.appendChild(line);
        }
      }
      return document;
    }
Example #3
0
  public String saveShow(String fn, ShowFileData showData) {
    _calendar = new GregorianCalendar();
    String showName =
        showData.getSettings().get(_title) + "_" + _format.format(_calendar.getTime()) + ".show";

    try {
      new File(fn).mkdirs();
      FileWriter _fileWriter = new FileWriter(fn + "/" + showName);
      BufferedWriter _outputFile = new BufferedWriter(_fileWriter);

      // Create the document
      Document doc = _impl.createDocument(null, _fileType, null);

      Element root = doc.getDocumentElement();

      Element settings = doc.createElement(_settings);
      Element patch = doc.createElement(_patch);
      Element cues = doc.createElement(_cues);
      Element magic = doc.createElement(_magicSheet);

      addText(doc, settings, _recordMode, showData.getSettings().get(_recordMode));
      addText(doc, settings, _totalChannels, showData.getSettings().get(_totalChannels));
      addText(doc, settings, _totalDimmers, showData.getSettings().get(_totalDimmers));
      addText(doc, settings, _dUpTime, showData.getSettings().get(_dUpTime));
      addText(doc, settings, _dDownTime, showData.getSettings().get(_dDownTime));
      addText(doc, settings, _gotoCueTime, showData.getSettings().get(_gotoCueTime));
      addText(doc, settings, _title, showData.getSettings().get(_title));
      addText(doc, settings, _comment, showData.getSettings().get(_comment));

      addText(doc, settings, _channelPerLine, showData.getSettings().get(_channelPerLine));
      addText(doc, settings, _channelHGroup, showData.getSettings().get(_channelHGroup));
      addText(doc, settings, _channelVGroup, showData.getSettings().get(_channelVGroup));

      for (Integer chan : showData.getPatch().keySet())
        if (showData.getPatch().get(chan).size() > 0)
          addChannel(doc, patch, chan, showData.getPatch().get(chan));

      for (FadeData fade : showData.getCueList()) addFade(doc, cues, fade);

      Position p;
      for (Integer chan : showData.getMagicSheet().keySet()) {
        MagicChannelData mChan = showData.getMagicSheet().get(chan);

        if (!(mChan._x == 0 && mChan._x == 0))
          addMagicChannel(doc, magic, chan, mChan._x, mChan._y);
      }

      root.appendChild(settings);
      root.appendChild(patch);
      root.appendChild(cues);
      root.appendChild(magic);

      // Serialize the document onto System.out
      TransformerFactory xformFactory = TransformerFactory.newInstance();
      Transformer idTransform = xformFactory.newTransformer();
      Source input = new DOMSource(doc);
      // Result output = new StreamResult(System.out);
      // idTransform.transform(input, output);
      Result output = new StreamResult(_outputFile);
      idTransform.transform(input, output);

      _outputFile.close();
      _fileWriter.close();
    } catch (FactoryConfigurationError e) {
      System.out.println("Could not locate a JAXP factory class");
    } catch (DOMException e) {
      System.err.println(e);
    } catch (TransformerConfigurationException e) {
      System.err.println(e);
    } catch (TransformerException e) {
      System.err.println(e);
    } catch (IOException e) {
      System.err.println(e);
    }

    return showName;
  }