public void startElement(String uri, String localname, String qName, Attributes attributes)
      throws SAXException {

    level++;
    final boolean topLevelElement = level == 1;

    if (!gotElements) {
      // Override default as we just go an element

      assert topLevelElement;

      delimiterNamespaceURI = uri;
      delimiterPrefix = XMLUtils.prefixFromQName(qName);
      delimiterLocalName = XMLUtils.localNameFromQName(qName);

      gotElements = true;
    }

    flushCharacters(false, topLevelElement);
    generateFirstDelimitersIfNeeded();

    // Add or update classes on element if needed
    super.startElement(
        uri, localname, qName, topLevelElement ? getAttributesWithClass(attributes) : attributes);
  }
Esempio n. 2
0
  private void checkDelimiters(String uri, String qName, boolean topLevel) throws SAXException {

    if (topLevel && delimiterNamespaceURI == null) {
      delimiterNamespaceURI = uri;
      delimiterPrefix = XMLUtils.prefixFromQName(qName);
      delimiterLocalName = XMLUtils.localNameFromQName(qName);
    }

    if (mustGenerateFirstDelimiters) {
      // Generate first delimiter
      beginDelimiterListener.generateFirstDelimiter(this);
      mustGenerateFirstDelimiters = false;
    }
  }
  public OutputInterceptor(
      XMLReceiver output,
      String spanQName,
      Listener beginDelimiterListener,
      boolean isAroundTableOrListElement) {
    super(output);
    this.spanQName = spanQName;
    this.beginDelimiterListener = beginDelimiterListener;
    this.isAroundTableOrListElement = isAroundTableOrListElement;

    // Default to <xhtml:span>
    delimiterNamespaceURI = XMLConstants.XHTML_NAMESPACE_URI;
    delimiterPrefix = XMLUtils.prefixFromQName(spanQName);
    delimiterLocalName = XMLUtils.localNameFromQName(spanQName);
  }
Esempio n. 4
0
 public static XMLReader newXMLReader(XMLUtils.ParserConfiguration parserConfiguration) {
   final SAXParser saxParser = XMLUtils.newSAXParser(parserConfiguration);
   try {
     final XMLReader xmlReader = saxParser.getXMLReader();
     xmlReader.setEntityResolver(XMLUtils.ENTITY_RESOLVER);
     xmlReader.setErrorHandler(XMLUtils.ERROR_HANDLER);
     return xmlReader;
   } catch (Exception e) {
     throw new OXFException(e);
   }
 }
Esempio n. 5
0
  private static void mapPrefixIfNeeded(
      Set<String> declaredPrefixes, String uri, String qName, StringBuilder sb) {
    final String prefix = XMLUtils.prefixFromQName(qName);
    if (prefix.length() > 0 && !declaredPrefixes.contains(prefix)) {
      sb.append(" xmlns:");
      sb.append(prefix);
      sb.append("=\"");
      sb.append(uri);
      sb.append("\"");

      declaredPrefixes.add(prefix);
    }
  }
  public void outputDelimiter(ContentHandler contentHandler, String classes, String id)
      throws SAXException {

    reusableAttributes.clear();
    if (id != null) reusableAttributes.addAttribute("", "id", "id", ContentHandlerHelper.CDATA, id);

    if (classes != null)
      reusableAttributes.addAttribute("", "class", "class", ContentHandlerHelper.CDATA, classes);

    final String delimiterQName = XMLUtils.buildQName(delimiterPrefix, delimiterLocalName);
    contentHandler.startElement(
        delimiterNamespaceURI, delimiterLocalName, delimiterQName, reusableAttributes);
    contentHandler.endElement(delimiterNamespaceURI, delimiterLocalName, delimiterQName);
  }
Esempio n. 7
0
  public static AttributesImpl addOrReplaceAttribute(
      Attributes attributes, String uri, String prefix, String localname, String value) {
    final AttributesImpl newAttributes = new AttributesImpl();
    boolean replaced = false;
    for (int i = 0; i < attributes.getLength(); i++) {
      final String attributeURI = attributes.getURI(i);
      final String attributeValue = attributes.getValue(i);
      final String attributeType = attributes.getType(i);
      final String attributeQName = attributes.getQName(i);
      final String attributeLocalname = attributes.getLocalName(i);

      if (uri.equals(attributeURI) && localname.equals(attributeLocalname)) {
        // Found existing attribute
        replaced = true;
        newAttributes.addAttribute(
            uri,
            localname,
            XMLUtils.buildQName(prefix, localname),
            ContentHandlerHelper.CDATA,
            value);
      } else {
        // Not a matched attribute
        newAttributes.addAttribute(
            attributeURI, attributeLocalname, attributeQName, attributeType, attributeValue);
      }
    }
    if (!replaced) {
      // Attribute did not exist already so add it
      newAttributes.addAttribute(
          uri,
          localname,
          XMLUtils.buildQName(prefix, localname),
          ContentHandlerHelper.CDATA,
          value);
    }
    return newAttributes;
  }
  private Attributes getAttributesWithClass(Attributes originalAttributes) {
    String newClassAttribute = originalAttributes.getValue("class");

    if (addedClasses != null && addedClasses.length() > 0) {
      if (newClassAttribute == null || newClassAttribute.length() == 0) {
        newClassAttribute = addedClasses;
      } else {
        newClassAttribute += " " + addedClasses;
      }
    }

    if (newClassAttribute != null)
      return XMLUtils.addOrReplaceAttribute(originalAttributes, "", "", "class", newClassAttribute);
    else return originalAttributes;
  }