Пример #1
0
  /** Auxiliary method for dispatchSVGLoad. */
  protected void dispatchSVGLoad(Element elt, boolean checkCanRun, String lang) {
    for (Node n = elt.getFirstChild(); n != null; n = n.getNextSibling()) {
      if (n.getNodeType() == Node.ELEMENT_NODE) {
        dispatchSVGLoad((Element) n, checkCanRun, lang);
      }
    }

    DocumentEvent de = (DocumentEvent) elt.getOwnerDocument();
    AbstractEvent ev = (AbstractEvent) de.createEvent("SVGEvents");
    String type;
    if (bridgeContext.isSVG12()) {
      type = "load";
    } else {
      type = "SVGLoad";
    }
    ev.initEventNS(XMLConstants.XML_EVENTS_NAMESPACE_URI, type, false, false);
    NodeEventTarget t = (NodeEventTarget) elt;

    final String s = elt.getAttributeNS(null, SVGConstants.SVG_ONLOAD_ATTRIBUTE);
    if (s.length() == 0) {
      // No script to run so just dispatch the event to DOM
      // (For java presumably).
      t.dispatchEvent(ev);
      return;
    }

    final Interpreter interp = getInterpreter();
    if (interp == null) {
      // Can't load interpreter so just dispatch normal event
      // to the DOM (for java presumably).
      t.dispatchEvent(ev);
      return;
    }

    if (checkCanRun) {
      // Check that it is ok to run embeded scripts
      checkCompatibleScriptURL(lang, docPURL);
      checkCanRun = false; // we only check once for onload handlers
    }

    DocumentLoader dl = bridgeContext.getDocumentLoader();
    SVGDocument d = (SVGDocument) elt.getOwnerDocument();
    int line = dl.getLineNumber(elt);
    final String desc =
        Messages.formatMessage(
            EVENT_SCRIPT_DESCRIPTION,
            new Object[] {d.getURL(), SVGConstants.SVG_ONLOAD_ATTRIBUTE, new Integer(line)});

    EventListener l =
        new EventListener() {
          public void handleEvent(Event evt) {
            try {
              Object event;
              if (evt instanceof ScriptEventWrapper) {
                event = ((ScriptEventWrapper) evt).getEventObject();
              } else {
                event = evt;
              }
              interp.bindObject(EVENT_NAME, event);
              interp.bindObject(ALTERNATE_EVENT_NAME, event);
              interp.evaluate(new StringReader(s), desc);
            } catch (IOException io) {
            } catch (InterpreterException e) {
              handleInterpreterException(e);
            }
          }
        };
    t.addEventListenerNS(XMLConstants.XML_EVENTS_NAMESPACE_URI, type, l, false, null);
    t.dispatchEvent(ev);
    t.removeEventListenerNS(XMLConstants.XML_EVENTS_NAMESPACE_URI, type, l, false);
  }
  /** Fills the given AttributedStringBuffer. */
  protected void fillAttributedStringBuffer(
      BridgeContext ctx,
      Element element,
      boolean top,
      Integer bidiLevel,
      Map initialAttributes,
      AttributedStringBuffer asb,
      List lnLocs) {
    // 'requiredFeatures', 'requiredExtensions', 'systemLanguage' &
    // 'display="none".
    if ((!SVGUtilities.matchUserAgent(element, ctx.getUserAgent()))
        || (!CSSUtilities.convertDisplay(element))) {
      return;
    }

    String s = XMLSupport.getXMLSpace(element);
    boolean preserve = s.equals(SVG_PRESERVE_VALUE);
    boolean prevEndsWithSpace;
    Element nodeElement = element;
    int elementStartChar = asb.length();

    if (top) {
      endLimit = startLen = asb.length();
    }
    if (preserve) {
      endLimit = startLen;
    }

    Map map = initialAttributes == null ? new HashMap() : new HashMap(initialAttributes);
    initialAttributes = getAttributeMap(ctx, element, null, bidiLevel, map);
    Object o = map.get(TextAttribute.BIDI_EMBEDDING);
    Integer subBidiLevel = bidiLevel;
    if (o != null) {
      subBidiLevel = (Integer) o;
    }

    int lineBreak = -1;
    if (lnLocs.size() != 0) {
      lineBreak = ((Integer) lnLocs.get(lnLocs.size() - 1)).intValue();
    }

    for (Node n = getFirstChild(element); n != null; n = getNextSibling(n)) {

      if (preserve) {
        prevEndsWithSpace = false;
      } else {
        int len = asb.length();
        if (len == startLen) {
          prevEndsWithSpace = true;
        } else {
          prevEndsWithSpace = (asb.getLastChar() == ' ');
          int idx = lnLocs.size() - 1;
          if (!prevEndsWithSpace && (idx >= 0)) {
            Integer i = (Integer) lnLocs.get(idx);
            if (i.intValue() == len) {
              prevEndsWithSpace = true;
            }
          }
        }
      }

      switch (n.getNodeType()) {
        case Node.ELEMENT_NODE:
          // System.out.println("Element: " + n);
          if (!SVG_NAMESPACE_URI.equals(n.getNamespaceURI())) break;

          nodeElement = (Element) n;

          String ln = n.getLocalName();

          if (ln.equals(SVG12Constants.SVG_FLOW_LINE_TAG)) {
            int before = asb.length();
            fillAttributedStringBuffer(
                ctx, nodeElement, false, subBidiLevel, initialAttributes, asb, lnLocs);
            // System.out.println("Line: " + asb.length() +
            //                    " - '" +  asb + "'");
            lineBreak = asb.length();
            lnLocs.add(new Integer(lineBreak));
            if (before != lineBreak) {
              initialAttributes = null;
            }
          } else if (ln.equals(SVG12Constants.SVG_FLOW_SPAN_TAG)
              || ln.equals(SVG12Constants.SVG_ALT_GLYPH_TAG)) {
            int before = asb.length();
            fillAttributedStringBuffer(
                ctx, nodeElement, false, subBidiLevel, initialAttributes, asb, lnLocs);
            if (asb.length() != before) {
              initialAttributes = null;
            }
          } else if (ln.equals(SVG_A_TAG)) {
            if (ctx.isInteractive()) {
              NodeEventTarget target = (NodeEventTarget) nodeElement;
              UserAgent ua = ctx.getUserAgent();
              SVGAElementBridge.CursorHolder ch;
              ch = new SVGAElementBridge.CursorHolder(CursorManager.DEFAULT_CURSOR);
              target.addEventListenerNS(
                  XMLConstants.XML_EVENTS_NAMESPACE_URI,
                  SVG_EVENT_CLICK,
                  new SVGAElementBridge.AnchorListener(ua, ch),
                  false,
                  null);

              target.addEventListenerNS(
                  XMLConstants.XML_EVENTS_NAMESPACE_URI,
                  SVG_EVENT_MOUSEOVER,
                  new SVGAElementBridge.CursorMouseOverListener(ua, ch),
                  false,
                  null);

              target.addEventListenerNS(
                  XMLConstants.XML_EVENTS_NAMESPACE_URI,
                  SVG_EVENT_MOUSEOUT,
                  new SVGAElementBridge.CursorMouseOutListener(ua, ch),
                  false,
                  null);
            }
            int before = asb.length();
            fillAttributedStringBuffer(
                ctx, nodeElement, false, subBidiLevel, initialAttributes, asb, lnLocs);
            if (asb.length() != before) {
              initialAttributes = null;
            }
          } else if (ln.equals(SVG_TREF_TAG)) {
            String uriStr = XLinkSupport.getXLinkHref((Element) n);
            Element ref = ctx.getReferencedElement((Element) n, uriStr);
            s = TextUtilities.getElementContent(ref);
            s = normalizeString(s, preserve, prevEndsWithSpace);
            if (s.length() != 0) {
              int trefStart = asb.length();
              Map m = new HashMap();
              getAttributeMap(ctx, nodeElement, null, bidiLevel, m);
              asb.append(s, m);
              int trefEnd = asb.length() - 1;
              TextPaintInfo tpi;
              tpi = (TextPaintInfo) elemTPI.get(nodeElement);
              tpi.startChar = trefStart;
              tpi.endChar = trefEnd;
            }
          }
          break;

        case Node.TEXT_NODE:
        case Node.CDATA_SECTION_NODE:
          s = n.getNodeValue();
          s = normalizeString(s, preserve, prevEndsWithSpace);
          if (s.length() != 0) {
            asb.append(s, map);
            if (preserve) {
              endLimit = asb.length();
            }
            initialAttributes = null;
          }
      }
    }

    if (top) {
      boolean strippedSome = false;
      while ((endLimit < asb.length()) && (asb.getLastChar() == ' ')) {
        int idx = lnLocs.size() - 1;
        int len = asb.length();
        if (idx >= 0) {
          Integer i = (Integer) lnLocs.get(idx);
          if (i.intValue() >= len) {
            i = new Integer(len - 1);
            lnLocs.set(idx, i);
            idx--;
            while (idx >= 0) {
              i = (Integer) lnLocs.get(idx);
              if (i.intValue() < len - 1) break;
              lnLocs.remove(idx);
              idx--;
            }
          }
        }
        asb.stripLast();
        strippedSome = true;
      }
      if (strippedSome) {
        Iterator iter = elemTPI.values().iterator();
        while (iter.hasNext()) {
          TextPaintInfo tpi = (TextPaintInfo) iter.next();
          if (tpi.endChar >= asb.length()) {
            tpi.endChar = asb.length() - 1;
            if (tpi.startChar > tpi.endChar) tpi.startChar = tpi.endChar;
          }
        }
      }
    }

    int elementEndChar = asb.length() - 1;
    TextPaintInfo tpi = (TextPaintInfo) elemTPI.get(element);
    tpi.startChar = elementStartChar;
    tpi.endChar = elementEndChar;
  }