Java Code Examples for org.xml.sax.helpers.AttributesImpl#removeAttribute()

The following examples show how to use org.xml.sax.helpers.AttributesImpl#removeAttribute() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
/**
 * Javadoc says getLocalName returns null if the index if out of range.
 */
@Test
public void testcase09() {
    AttributesImpl attr = new AttributesImpl();
    attr.addAttribute(CAR_URI, CAR_LOCALNAME, CAR_QNAME, CAR_TYPE, CAR_VALUE);
    attr.addAttribute(JEEP_URI, JEEP_LOCALNAME, JEEP_QNAME, JEEP_TYPE,
            JEEP_VALUE);
    attr.removeAttribute(1);
    assertNull(attr.getLocalName(1));
}
 
Example 2
/**
 * Javadoc says java.lang.ArrayIndexOutOfBoundsException is thrown When the
 * supplied index does not point to an attribute in the list.
 */
@Test(expectedExceptions = ArrayIndexOutOfBoundsException.class)
public void testcase10() {
    AttributesImpl attr = new AttributesImpl();
    attr.addAttribute(CAR_URI, CAR_LOCALNAME, CAR_QNAME, CAR_TYPE, CAR_VALUE);
    attr.addAttribute(JEEP_URI, JEEP_LOCALNAME, JEEP_QNAME, JEEP_TYPE,
            JEEP_VALUE);
    attr.removeAttribute(1);
    attr.removeAttribute(1);
}
 
Example 3
static
protected Attributes removeAttribute(Attributes attributes, String localName){
	int index = attributes.getIndex("", localName);

	if(index < 0){
		return attributes;
	}

	AttributesImpl result = new AttributesImpl(attributes);
	result.removeAttribute(index);

	return result;
}
 
Example 4
Source Project: uima-uimaj   File: BinarySerializer.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Serialize an element named name, with the indicated attributes and value.
 *
 * @param name          is the element name
 * @param attributes          are the attributes...serializer is free to add more.
 * @param value          is the value
 * @param context          is the SerializationContext
 * @throws IOException Signals that an I/O exception has occurred.
 */
@Override
public void serialize(QName name, Attributes attributes, Object value,
        SerializationContext context) throws IOException {
  if (value instanceof Serializable) {
    byte[] bytes = SerializationUtils.serialize((Serializable) value);

    // Should we use an attachment? Do so if:
    // (a) attachment support exists and mUseAttachments == true and
    // (b) if we are the server, the client sent us an attachment
    // (so we know client wants attachment support as well)]
    Message msg = context.getCurrentMessage();
    Attachments attachments = msg.getAttachmentsImpl();
    boolean useAttachments = (attachments != null) && mUseAttachments;
    if (useAttachments) {
      useAttachments = !context.getMessageContext().getPastPivot()
              || context.getMessageContext().getRequestMessage().getAttachments().hasNext();
    }
    // if we have attachment support, do this as an attachment
    if (useAttachments) {
      // System.out.println("Creating attachment"); //DEBUG
      SOAPConstants soapConstants = context.getMessageContext().getSOAPConstants();
      DataHandler dataHandler = new DataHandler(new OctetStreamDataSource("test",
              new OctetStream(bytes)));
      Part attachmentPart = attachments.createAttachmentPart(dataHandler);

      AttributesImpl attrs = new AttributesImpl();
      if (attributes != null && 0 < attributes.getLength())
        attrs.setAttributes(attributes); // copy the existing ones.

      int typeIndex = -1;
      if ((typeIndex = attrs.getIndex(Constants.URI_DEFAULT_SCHEMA_XSI, "type")) != -1) {
        // Found a xsi:type which should not be there for attachments.
        attrs.removeAttribute(typeIndex);
      }

      attrs.addAttribute("", soapConstants.getAttrHref(), soapConstants.getAttrHref(), "CDATA",
              attachmentPart.getContentIdRef());
      context.startElement(name, attrs);
      context.endElement();
    } else {
      // no attachment support - Base64 encode
      // System.out.println("No attachment support"); //DEBUG
      context.startElement(name, attributes);

      String base64str = Base64.encode(bytes);
      context.writeChars(base64str.toCharArray(), 0, base64str.length());
      context.endElement();
    }

  } else {
    throw new IOException(value.getClass().getName() + " is not serializable.");
  }
}
 
Example 5
/**
 * Serialize an element named name, with the indicated attributes and value.
 *
 * @param name          is the element name
 * @param attributes          are the attributes...serializer is free to add more.
 * @param value          is the value
 * @param context          is the SerializationContext
 * @throws IOException Signals that an I/O exception has occurred.
 */
@Override
public void serialize(QName name, Attributes attributes, Object value,
        SerializationContext context) throws IOException {
  if (value instanceof Serializable) {
    byte[] bytes = SerializationUtils.serialize((Serializable) value);

    // Should we use an attachment? Do so if:
    // (a) attachment support exists and mUseAttachments == true and
    // (b) if we are the server, the client sent us an attachment
    // (so we know client wants attachment support as well)]
    Message msg = context.getCurrentMessage();
    Attachments attachments = msg.getAttachmentsImpl();
    boolean useAttachments = (attachments != null) && mUseAttachments;
    if (useAttachments) {
      useAttachments = !context.getMessageContext().getPastPivot()
              || context.getMessageContext().getRequestMessage().getAttachments().hasNext();
    }
    // if we have attachment support, do this as an attachment
    if (useAttachments) {
      // System.out.println("Creating attachment"); //DEBUG
      SOAPConstants soapConstants = context.getMessageContext().getSOAPConstants();
      DataHandler dataHandler = new DataHandler(new OctetStreamDataSource("test",
              new OctetStream(bytes)));
      Part attachmentPart = attachments.createAttachmentPart(dataHandler);

      AttributesImpl attrs = new AttributesImpl();
      if (attributes != null && 0 < attributes.getLength())
        attrs.setAttributes(attributes); // copy the existing ones.

      int typeIndex = -1;
      if ((typeIndex = attrs.getIndex(Constants.URI_DEFAULT_SCHEMA_XSI, "type")) != -1) {
        // Found a xsi:type which should not be there for attachments.
        attrs.removeAttribute(typeIndex);
      }

      attrs.addAttribute("", soapConstants.getAttrHref(), soapConstants.getAttrHref(), "CDATA",
              attachmentPart.getContentIdRef());
      context.startElement(name, attrs);
      context.endElement();
    } else {
      // no attachment support - Base64 encode
      // System.out.println("No attachment support"); //DEBUG
      context.startElement(name, attributes);

      String base64str = Base64.encode(bytes);
      context.writeChars(base64str.toCharArray(), 0, base64str.length());
      context.endElement();
    }

  } else {
    throw new IOException(value.getClass().getName() + " is not serializable.");
  }
}