Search in sources :

Example 31 with NamedNodeMap

use of org.w3c.dom.NamedNodeMap in project OpenClinica by OpenClinica.

the class FSItemProcessor method processFieldSubmissionGroupItems.

private void processFieldSubmissionGroupItems(ArrayList<HashMap> listOfUploadFilePaths, Node repeatNode, Node itemNode, SubmissionContainer container, ItemGroup itemGroup) throws Exception {
    String itemName;
    Integer itemOrdinal = 1;
    String itemValue;
    // Node repeatGroupNode = itemNode.getParentNode();
    if (repeatNode != null) {
        final NamedNodeMap attributes = repeatNode.getAttributes();
        // check to see if groupNode has any enketo attributes
        for (int attrIndex = 0; attrIndex < attributes.getLength(); attrIndex++) {
            if (attributes.item(attrIndex).getNodeName().equals(ENKETO_ORDINAL)) {
                logger.debug("found enketo attribute");
                logger.debug(attributes.item(attrIndex).getNodeName());
                logger.debug(attributes.item(attrIndex).getNodeValue());
                itemOrdinal = new Integer(attributes.item(attrIndex).getNodeValue());
            }
        }
    } else {
        itemOrdinal = 1;
    }
    FormLayout formLayout = container.getFormLayout();
    CrfVersion crfVersion = crfVersionDao.findAllByCrfId(formLayout.getCrf().getCrfId()).get(0);
    container.setCrfVersion(crfVersion);
    Item item = null;
    ItemGroupMetadata igm = null;
    if (container.getRequestType() == FieldRequestTypeEnum.DELETE_FIELD) {
        List<ItemGroupMetadata> igms = itemGroupMetadataDao.findByItemGroupCrfVersion(itemGroup.getItemGroupId(), crfVersion.getCrfVersionId());
        for (ItemGroupMetadata ig : igms) {
            ItemData existingItemData = itemDataDao.findByItemEventCrfOrdinal(ig.getItem().getItemId(), container.getEventCrf().getEventCrfId(), itemOrdinal);
            // ItemData existingItemData = lookupFieldItemData(itemGroup, itemOrdinal, container);
            if (existingItemData != null) {
                existingItemData.setDeleted(true);
                existingItemData.setValue("");
                existingItemData.setOldStatus(existingItemData.getStatus());
                existingItemData.setUserAccount(container.getUser());
                existingItemData.setStatus(Status.AVAILABLE);
                existingItemData.setUpdateId(container.getUser().getUserId());
                existingItemData.setInstanceId(container.getInstanceId());
                existingItemData = itemDataDao.saveOrUpdate(existingItemData);
                resetSdvStatus(container);
                // Close discrepancy notes
                closeItemDiscrepancyNotes(container, existingItemData);
            }
        }
        return;
    }
    // igm = itemGroupMetadataDao.findByItemCrfVersion(item.getItemId(), crfVersion.getCrfVersionId());
    // Item loop
    QueryServiceHelperBean helperBean = new QueryServiceHelperBean();
    if (queryService.getQueryAttribute(helperBean, itemNode) != null) {
        queryService.process(helperBean, container, itemNode, itemOrdinal);
    } else if (shouldProcessItemNode(itemNode)) {
        itemName = itemNode.getNodeName().trim();
        itemValue = itemNode.getTextContent();
        item = itemDao.findByNameCrfId(itemNode.getNodeName(), crfVersion.getCrf().getCrfId());
        if (item == null) {
            logger.error("Failed to lookup item: '" + itemName + "'.  Continuing with submission.");
        }
        ItemFormMetadata itemFormMetadata = itemFormMetadataDao.findByItemCrfVersion(item.getItemId(), crfVersion.getCrfVersionId());
        // Convert space separated Enketo multiselect values to comma separated OC multiselect values
        Integer responseTypeId = itemFormMetadata.getResponseSet().getResponseType().getResponseTypeId();
        if (responseTypeId == 3 || responseTypeId == 7) {
            itemValue = itemValue.replaceAll(" ", ",");
        }
        if (responseTypeId == 4) {
            /*
                 * for (HashMap uploadFilePath : listOfUploadFilePaths) {
                 * if ((boolean) uploadFilePath.containsKey(itemValue) && itemValue != "") {
                 * itemValue = (String) uploadFilePath.get(itemValue);
                 * break;
                 * }
                 * }
                 */
            FormLayoutMedia media = formLayoutMediaDao.findByEventCrfIdAndFileName(container.getEventCrf().getEventCrfId(), itemValue);
            if (media == null) {
                media = new FormLayoutMedia();
            }
            media.setName(itemValue);
            media.setFormLayout(formLayout);
            media.setEventCrfId(container.getEventCrf().getEventCrfId());
            media.setPath("/" + container.getStudy().getOc_oid() + "/");
            formLayoutMediaDao.saveOrUpdate(media);
        }
        ItemData newItemData = createItemData(item, itemValue, itemOrdinal, container);
        Errors itemErrors = validateItemData(newItemData, item, responseTypeId);
        if (itemErrors.hasErrors()) {
            container.getErrors().addAllErrors(itemErrors);
            throw new Exception("Item validation error.  Rolling back submission changes.");
        }
        ItemData existingItemData = itemDataDao.findByItemEventCrfOrdinal(item.getItemId(), container.getEventCrf().getEventCrfId(), itemOrdinal);
        if (existingItemData == null) {
            newItemData.setStatus(Status.UNAVAILABLE);
            itemDataDao.saveOrUpdate(newItemData);
            resetSdvStatus(container);
        } else if (existingItemData.getValue().equals(newItemData.getValue())) {
        } else {
            // Existing item. Value changed. Update existing value.
            existingItemData.setInstanceId(container.getInstanceId());
            existingItemData.setValue(newItemData.getValue());
            existingItemData.setUpdateId(container.getUser().getUserId());
            existingItemData.setDateUpdated(new Date());
            itemDataDao.saveOrUpdate(existingItemData);
            resetSdvStatus(container);
        }
    }
}
Also used : FormLayout(org.akaza.openclinica.domain.datamap.FormLayout) NamedNodeMap(org.w3c.dom.NamedNodeMap) FormLayoutMedia(org.akaza.openclinica.domain.datamap.FormLayoutMedia) ItemFormMetadata(org.akaza.openclinica.domain.datamap.ItemFormMetadata) Date(java.util.Date) Item(org.akaza.openclinica.domain.datamap.Item) Errors(org.springframework.validation.Errors) CrfVersion(org.akaza.openclinica.domain.datamap.CrfVersion) ItemGroupMetadata(org.akaza.openclinica.domain.datamap.ItemGroupMetadata) ItemData(org.akaza.openclinica.domain.datamap.ItemData)

Example 32 with NamedNodeMap

use of org.w3c.dom.NamedNodeMap in project jdk8u_jdk by JetBrains.

the class BmpDefaultImageMetadataTest method displayMetadata.

void displayMetadata(Node node, int level) {
    // emit open tag
    indent(level);
    System.out.print("<" + node.getNodeName());
    NamedNodeMap map = node.getAttributes();
    if (map != null) {
        // print attribute values
        int length = map.getLength();
        for (int i = 0; i < length; i++) {
            Node attr = map.item(i);
            System.out.print(" " + attr.getNodeName() + "=\"" + attr.getNodeValue() + "\"");
        }
    }
    Node child = node.getFirstChild();
    if (node.getNodeValue() != null && !node.getNodeValue().equals("")) {
        System.out.println(">");
        indent(level);
        System.out.println(node.getNodeValue());
        // emit close tag
        indent(level);
        System.out.println("</" + node.getNodeName() + ">");
    } else if (child != null) {
        // close current tag
        System.out.println(">");
        while (child != null) {
            // emit child tags recursively
            displayMetadata(child, level + 1);
            child = child.getNextSibling();
        }
        // emit close tag
        indent(level);
        System.out.println("</" + node.getNodeName() + ">");
    } else {
        System.out.println("/>");
    }
}
Also used : NamedNodeMap(org.w3c.dom.NamedNodeMap) Node(org.w3c.dom.Node)

Example 33 with NamedNodeMap

use of org.w3c.dom.NamedNodeMap in project midpoint by Evolveum.

the class PrettyPrinter method prettyPrint.

public static String prettyPrint(Element element, boolean displayTag) {
    if (element == null) {
        return "null";
    }
    StringBuilder sb = new StringBuilder();
    if (displayTag) {
        sb.append("<");
        if (element.getLocalName() != null) {
            sb.append(prettyPrint(new QName(element.getNamespaceURI(), element.getLocalName())));
        } else {
            sb.append("<null>");
        }
        sb.append(">");
    }
    NamedNodeMap attributes = element.getAttributes();
    for (int i = 0; i < attributes.getLength(); i++) {
        Node attr = attributes.item(i);
        if ("xmlns".equals(attr.getPrefix())) {
            // they are too long for prettyPrint
            continue;
        }
        if ((attr.getPrefix() == null || attr.getPrefix().isEmpty()) && "xmlns".equals(attr.getLocalName())) {
            // Skip default ns declaration as well
            continue;
        }
        sb.append("@");
        sb.append(attr.getLocalName());
        sb.append("=");
        sb.append(attr.getTextContent());
        if (i < (attributes.getLength() - 1)) {
            sb.append(",");
        }
    }
    if (attributes.getLength() > 0) {
        sb.append(":");
    }
    StringBuilder content = new StringBuilder();
    Node child = element.getFirstChild();
    while (child != null) {
        if (child.getNodeType() == Node.TEXT_NODE) {
            content.append(((Text) child).getTextContent());
        } else if (child.getNodeType() == Node.COMMENT_NODE) {
        // just ignore this
        } else {
            content = new StringBuilder("[complex content]");
            break;
        }
        child = child.getNextSibling();
    }
    sb.append(content);
    return sb.toString();
}
Also used : NamedNodeMap(org.w3c.dom.NamedNodeMap) QName(javax.xml.namespace.QName) Node(org.w3c.dom.Node)

Example 34 with NamedNodeMap

use of org.w3c.dom.NamedNodeMap in project midpoint by Evolveum.

the class DOMUtil method getNamespaceDeclarations.

/**
	 * Returns map of all namespace declarations from specified element (prefix -> namespace).
	 */
public static Map<String, String> getNamespaceDeclarations(Element element) {
    Map<String, String> nsDeclMap = new HashMap<>();
    NamedNodeMap attributes = element.getAttributes();
    for (int i = 0; i < attributes.getLength(); i++) {
        Attr attr = (Attr) attributes.item(i);
        if (isNamespaceDefinition(attr)) {
            String prefix = getNamespaceDeclarationPrefix(attr);
            String namespace = getNamespaceDeclarationNamespace(attr);
            nsDeclMap.put(prefix, namespace);
        }
    }
    return nsDeclMap;
}
Also used : NamedNodeMap(org.w3c.dom.NamedNodeMap) Attr(org.w3c.dom.Attr)

Example 35 with NamedNodeMap

use of org.w3c.dom.NamedNodeMap in project midpoint by Evolveum.

the class DOMUtil method isPrefixUsed.

public static boolean isPrefixUsed(Element targetElement, String prefix) {
    if (comparePrefix(prefix, targetElement.getPrefix())) {
        return true;
    }
    NamedNodeMap attributes = targetElement.getAttributes();
    for (int i = 0; i < attributes.getLength(); i++) {
        Attr attr = (Attr) attributes.item(i);
        if (comparePrefix(prefix, attr.getPrefix())) {
            return true;
        }
    }
    NodeList childNodes = targetElement.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node node = childNodes.item(i);
        if (node instanceof Element) {
            Element element = (Element) node;
            if (isPrefixUsed(element, prefix)) {
                return true;
            }
        }
    }
    return false;
}
Also used : NamedNodeMap(org.w3c.dom.NamedNodeMap) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) Attr(org.w3c.dom.Attr)

Aggregations

NamedNodeMap (org.w3c.dom.NamedNodeMap)991 Node (org.w3c.dom.Node)688 NodeList (org.w3c.dom.NodeList)338 Attr (org.w3c.dom.Attr)295 Element (org.w3c.dom.Element)237 Document (org.w3c.dom.Document)153 ArrayList (java.util.ArrayList)92 HashMap (java.util.HashMap)91 DocumentBuilder (javax.xml.parsers.DocumentBuilder)59 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)58 IOException (java.io.IOException)53 SAXException (org.xml.sax.SAXException)41 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)40 List (java.util.List)36 Map (java.util.Map)33 File (java.io.File)27 InputStream (java.io.InputStream)26 CMNamedNodeMap (org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap)25 DOMException (org.w3c.dom.DOMException)25 ByteArrayInputStream (java.io.ByteArrayInputStream)19