use of org.jdom2.Comment in project coprhd-controller by CoprHD.
the class XmlDiff method compareElement.
/**
* Compares two XML Elements and their children recursively
*
* @param oldElement
* The old element to compare
* @param newElement
* The new element to compare
* @return true if two elements are same, else false
*/
public static boolean compareElement(Element oldElement, Element newElement) {
// Check element name
if (!oldElement.getName().equals(newElement.getName())) {
return false;
}
// Check attributes
if (!compareAttributes(oldElement.getAttributes(), newElement.getAttributes())) {
return false;
}
// Check children number
if (oldElement.getChildren().isEmpty() && newElement.getChildren().isEmpty()) {
return true;
}
// Check non leaf element
Iterator<Element> oldIter = oldElement.getChildren().iterator();
while (oldIter.hasNext()) {
Element oldChild = oldIter.next();
boolean found = false;
Iterator<Element> newIter = newElement.getChildren().iterator();
while (newIter.hasNext()) {
Element newChild = newIter.next();
if (newChild.getName().equals(oldChild.getName())) {
found = compareElement(oldChild, newChild);
if (found) {
break;
}
}
}
if (found) {
oldIter.remove();
newIter.remove();
}
}
// Check children
if (oldElement.getChildren().isEmpty() && newElement.getChildren().isEmpty()) {
return true;
} else if (oldElement.getChildren().size() != newElement.getChildren().size()) {
// Add comments to stand for same tags.
if (!(oldElement.getContent(0) instanceof Comment) && !(newElement.getContent(0) instanceof Comment)) {
oldElement.addContent(0, new Comment("..."));
newElement.addContent(0, new Comment("..."));
}
}
return false;
}
use of org.jdom2.Comment in project mycore by MyCoRe-Org.
the class MCRRestAPIObjectsHelper method showMCRObject.
public static Response showMCRObject(String pathParamId, String queryParamStyle, UriInfo info, HttpServletRequest request) throws MCRRestAPIException {
MCRObject mcrObj = retrieveMCRObject(pathParamId);
Document doc = mcrObj.createXML();
Element eStructure = doc.getRootElement().getChild("structure");
if (queryParamStyle != null && !MCRRestAPIObjects.STYLE_DERIVATEDETAILS.equals(queryParamStyle)) {
throw new MCRRestAPIException(Response.Status.BAD_REQUEST, new MCRRestAPIError(MCRRestAPIError.CODE_WRONG_PARAMETER, "The value of parameter {style} is not allowed.", "Allowed values for {style} parameter are: " + MCRRestAPIObjects.STYLE_DERIVATEDETAILS));
}
if (MCRRestAPIObjects.STYLE_DERIVATEDETAILS.equals(queryParamStyle) && eStructure != null) {
Element eDerObjects = eStructure.getChild("derobjects");
if (eDerObjects != null) {
for (Element eDer : eDerObjects.getChildren("derobject")) {
String derID = eDer.getAttributeValue("href", MCRConstants.XLINK_NAMESPACE);
try {
MCRDerivate der = MCRMetadataManager.retrieveMCRDerivate(MCRObjectID.getInstance(derID));
eDer.addContent(der.createXML().getRootElement().detach());
// <mycorederivate xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xlink="http://www.w3.org/1999/xlink" xsi:noNamespaceSchemaLocation="datamodel-derivate.xsd" ID="cpr_derivate_00003760" label="display_image" version="1.3">
// <derivate display="true">
eDer = eDer.getChild("mycorederivate").getChild("derivate");
Document docContents = listDerivateContentAsXML(MCRMetadataManager.retrieveMCRDerivate(MCRObjectID.getInstance(derID)), "/", -1, info);
if (docContents.hasRootElement()) {
eDer.addContent(docContents.getRootElement().detach());
}
} catch (MCRException e) {
eDer.addContent(new Comment("Error: Derivate not found."));
} catch (IOException e) {
eDer.addContent(new Comment("Error: Derivate content could not be listed: " + e.getMessage()));
}
}
}
}
StringWriter sw = new StringWriter();
XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
try {
String filter_id = MCRConfiguration.instance().getString("MCR.RestAPI.v1.Filter.XML", "");
if (filter_id.length() > 0) {
MCRContentTransformer trans = MCRContentTransformerFactory.getTransformer(filter_id);
Document filtered_doc = trans.transform(new MCRJDOMContent(doc)).asXML();
outputter.output(filtered_doc, sw);
} else {
outputter.output(doc, sw);
}
} catch (SAXException | JDOMException e) {
throw new MCRRestAPIException(Response.Status.INTERNAL_SERVER_ERROR, new MCRRestAPIError(MCRRestAPIError.CODE_INTERNAL_ERROR, "Unable to transform MCRContent to XML document", e.getMessage()));
} catch (IOException e) {
throw new MCRRestAPIException(Response.Status.INTERNAL_SERVER_ERROR, new MCRRestAPIError(MCRRestAPIError.CODE_INTERNAL_ERROR, "Unable to retrieve/transform MyCoRe object", e.getMessage()));
}
String authHeader = MCRJSONWebTokenUtil.createJWTAuthorizationHeader(MCRJSONWebTokenUtil.retrieveAuthenticationToken(request));
return Response.ok(sw.toString()).type("application/xml").header(HEADER_NAME_AUTHORIZATION, authHeader).build();
}
Aggregations