Java Code Examples for org.jdom2.Element#removeAttribute()

The following examples show how to use org.jdom2.Element#removeAttribute() . 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
Source File: NcMLWriter.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public Element makeNetcdfElement(NetcdfFile ncFile, String location) {
  Element rootElem = makeGroupElement(ncFile.getRootGroup());

  // rootElem isn't just like any other group element; we must undo some of the changes made to it in writeGroup().
  rootElem.setName("netcdf"); // Was "group".
  rootElem.removeAttribute("name"); // This attribute is not defined on the root "netcdf" element.

  rootElem.addNamespaceDeclaration(namespace);

  if (null == location)
    location = ncFile.getLocation();

  if (null != location) {
    rootElem.setAttribute("location", URLnaming.canonicalizeWrite(location));
  }

  if (null != ncFile.getId())
    rootElem.setAttribute("id", ncFile.getId());

  if (null != ncFile.getTitle())
    rootElem.setAttribute("title", ncFile.getTitle());

  return rootElem;
}
 
Example 2
Source File: NcmlWriter.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public Element makeNetcdfElement(NetcdfFile ncFile, @Nullable String location) {
  Element rootElem = makeGroupElement(ncFile.getRootGroup());

  // rootElem isn't just like any other group element; we must undo some of the changes made to it in writeGroup().
  rootElem.setName("netcdf"); // Was "group".
  rootElem.removeAttribute("name"); // This attribute is not defined on the root "netcdf" element.

  rootElem.addNamespaceDeclaration(namespace);

  if (null == location)
    location = ncFile.getLocation();

  if (null != location) {
    rootElem.setAttribute("location", URLnaming.canonicalizeWrite(location));
  }

  if (null != ncFile.getId())
    rootElem.setAttribute("id", ncFile.getId());

  if (null != ncFile.getTitle())
    rootElem.setAttribute("title", ncFile.getTitle());

  return rootElem;
}
 
Example 3
Source File: MCRIncludeHandler.java    From mycore with GNU General Public License v3.0 6 votes vote down vote up
private boolean handleBeforeAfter(Element container, Element includeRule, String attributeName, int offset,
    int defaultPos) {
    String refID = includeRule.getAttributeValue(attributeName);
    if (refID != null) {
        includeRule.removeAttribute(attributeName);

        Element parent = container;
        int pos = defaultPos;

        Optional<Element> neighbor = findDescendant(container, refID);
        if (neighbor.isPresent()) {
            Element n = neighbor.get();
            parent = n.getParentElement();
            List<Element> children = parent.getChildren();
            pos = children.indexOf(n) + offset;
        }

        LOGGER.debug("including  " + Arrays.toString(includeRule.getAttributes().toArray()) + " at pos " + pos);
        parent.getChildren().add(pos, includeRule.clone());
    }
    return refID != null;
}
 
Example 4
Source File: MCRMODSDateHelperTest.java    From mycore with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void testISO8601yearOnly() {
    Element element = new Element("date");

    int fullYear = Year.now().get(ChronoField.YEAR);

    MCRMODSDateHelper.setDate(element, new Date(), MCRMODSDateFormat.iso8601_4);

    int year = Integer.parseInt(element.getText());
    assertEquals(fullYear, year);
    assertEquals("iso8601", element.getAttributeValue("encoding"));

    Date parsed = MCRMODSDateHelper.getDate(element);
    assertEquals(year, parsed.toInstant().atZone(ZoneId.systemDefault()).get(ChronoField.YEAR));

    element.removeAttribute("encoding");
    assertEquals(fullYear, MCRMODSDateHelper.getCalendar(element).get(Calendar.YEAR));
}
 
Example 5
Source File: MCRExtractRelatedItemsEventHandler.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
private Element cloneRelatedItem(Element relatedItem) {
    Element mods = relatedItem.clone();
    mods.setName("mods");
    mods.removeAttribute("type");
    mods.removeAttribute("href", MCRConstants.XLINK_NAMESPACE);
    mods.removeAttribute("type", MCRConstants.XLINK_NAMESPACE);
    mods.removeChildren("part", MCRConstants.MODS_NAMESPACE);
    return mods;
}
 
Example 6
Source File: MCRWCMSUtil.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Converts the navigation.xml to the old format.
 */
private static byte[] convertToOldFormat(byte[] xml) throws JDOMException, IOException {
    SAXBuilder builder = new SAXBuilder();
    Document doc = builder.build(new ByteArrayInputStream(xml));
    Element rootElement = doc.getRootElement();
    rootElement.setAttribute("href", rootElement.getName());
    List<Element> children = rootElement.getChildren();
    for (Element menu : children) {
        String id = menu.getAttributeValue("id");
        menu.setName(id);
        menu.setAttribute("href", id);
        menu.removeAttribute("id");
    }
    XMLOutputter out = new XMLOutputter(Format.getPrettyFormat());
    ByteArrayOutputStream bout = new ByteArrayOutputStream(xml.length);
    out.output(doc, bout);
    return bout.toByteArray();
}
 
Example 7
Source File: MCREditorOutValidator.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
public static String checkMetaObject(Element datasubtag, Class<? extends MCRMetaInterface> metaClass,
    boolean keepLang) {
    if (!keepLang) {
        datasubtag.removeAttribute("lang", XML_NAMESPACE);
    }
    MCRMetaInterface test = null;
    try {
        test = metaClass.getDeclaredConstructor().newInstance();
    } catch (Exception e) {
        throw new MCRException("Could not instantiate " + metaClass.getCanonicalName());
    }
    test.setFromDOM(datasubtag);
    test.validate();
    return null;
}
 
Example 8
Source File: MCRQLSearchUtils.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Rename elements conditionN to condition. Transform condition with multiple child values to OR-condition.
 */
protected static void renameElements(Element element) {
    if (element.getName().startsWith("condition")) {
        element.setName("condition");

        String field = new StringTokenizer(element.getAttributeValue("field"), " -,").nextToken();
        String operator = element.getAttributeValue("operator");
        if (operator == null) {
            LOGGER.warn("No operator defined for field: {}", field);
            operator = "=";
        }
        element.setAttribute("operator", operator);

        List<Element> values = element.getChildren("value");
        if (values != null && values.size() > 0) {
            element.removeAttribute("field");
            element.setAttribute("operator", "or");
            element.setName("boolean");
            for (Element value : values) {
                value.setName("condition");
                value.setAttribute("field", field);
                value.setAttribute("operator", operator);
                value.setAttribute("value", value.getText());
                value.removeContent();
            }
        }
    } else if (element.getName().startsWith("boolean")) {
        element.setName("boolean");
        for (Object child : element.getChildren()) {
            if (child instanceof Element) {
                renameElements((Element) child);
            }
        }
    }
}
 
Example 9
Source File: XMLUtils.java    From yawl with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static void removeAttribute(Element e, String attr)
{
	try
	{
		e.removeAttribute(attr);
	}
	catch (Exception e1)
	{
		logger.error("cannot remove Attribute", e1);
	}
}
 
Example 10
Source File: RSS20Generator.java    From rome with Apache License 2.0 5 votes vote down vote up
@Override
public void populateItem(final Item item, final Element eItem, final int index) {

    super.populateItem(item, eItem, index);

    final Element description = eItem.getChild("description", getFeedNamespace());
    if (description != null) {
        description.removeAttribute("type");
    }

    final String author = item.getAuthor();
    if (author != null) {
        eItem.addContent(generateSimpleElement("author", author));
    }

    final String comments = item.getComments();
    if (comments != null) {
        eItem.addContent(generateSimpleElement("comments", comments));
    }

    final Guid guid = item.getGuid();
    if (guid != null) {
        final Element eGuid = generateSimpleElement("guid", guid.getValue());
        if (!guid.isPermaLink()) {
            eGuid.setAttribute("isPermaLink", "false");
        }
        eItem.addContent(eGuid);
    }
}