List of usage examples for org.w3c.dom Node lookupNamespaceURI
public String lookupNamespaceURI(String prefix);
From source file:org.fosstrak.epcis.repository.capture.CaptureOperationsModule.java
/** * Takes an XML document node, parses it as EPCIS event and inserts the data * into the database. The parse routine is generic for all event types; the * query generation part has some if/elses to take care of different event * parameters./* ww w . j av a 2s.c om*/ * * @param eventNode * The current event node. * @param eventType * The current event type. * @throws Exception * @throws DOMException */ private void handleEvent(Session session, final Node eventNode, final String eventType) throws DOMException, SAXException, InvalidFormatException { if (eventNode == null) { // nothing to do return; } else if (eventNode.getChildNodes().getLength() == 0) { throw new SAXException("Event element '" + eventNode.getNodeName() + "' has no children elements."); } Node curEventNode = null; // A lot of the initialized variables have type URI. This type isn't to // compare with the URI-Type of the standard. In fact, most of the // variables having type URI are declared as Vocabularies in the // Standard. Commonly, we use String for the Standard-Type URI. Calendar eventTime = null; Calendar recordTime = GregorianCalendar.getInstance(); String eventTimeZoneOffset = null; String action = null; String parentId = null; Long quantity = null; String bizStepUri = null; String dispositionUri = null; String readPointUri = null; String bizLocationUri = null; String epcClassUri = null; List<String> epcs = null; List<BusinessTransaction> bizTransList = null; List<EventFieldExtension> fieldNameExtList = new ArrayList<EventFieldExtension>(); for (int i = 0; i < eventNode.getChildNodes().getLength(); i++) { curEventNode = eventNode.getChildNodes().item(i); String nodeName = curEventNode.getNodeName(); if (nodeName.equals("#text") || nodeName.equals("#comment")) { // ignore text or comments LOG.debug(" ignoring text or comment: '" + curEventNode.getTextContent().trim() + "'"); continue; } LOG.debug(" handling event field: '" + nodeName + "'"); if (nodeName.equals("eventTime")) { String xmlTime = curEventNode.getTextContent(); LOG.debug(" eventTime in xml is '" + xmlTime + "'"); try { eventTime = TimeParser.parseAsCalendar(xmlTime); } catch (ParseException e) { throw new SAXException("Invalid date/time (must be ISO8601).", e); } LOG.debug(" eventTime parsed as '" + eventTime.getTime() + "'"); } else if (nodeName.equals("recordTime")) { // ignore recordTime } else if (nodeName.equals("eventTimeZoneOffset")) { eventTimeZoneOffset = checkEventTimeZoneOffset(curEventNode.getTextContent()); } else if (nodeName.equals("epcList") || nodeName.equals("childEPCs")) { epcs = handleEpcs(eventType, curEventNode); } else if (nodeName.equals("bizTransactionList")) { bizTransList = handleBizTransactions(session, curEventNode); } else if (nodeName.equals("action")) { action = curEventNode.getTextContent(); if (!action.equals("ADD") && !action.equals("OBSERVE") && !action.equals("DELETE")) { throw new SAXException("Encountered illegal 'action' value: " + action); } } else if (nodeName.equals("bizStep")) { bizStepUri = curEventNode.getTextContent(); } else if (nodeName.equals("disposition")) { dispositionUri = curEventNode.getTextContent(); } else if (nodeName.equals("readPoint")) { Element attrElem = (Element) curEventNode; Node id = attrElem.getElementsByTagName("id").item(0); readPointUri = id.getTextContent(); } else if (nodeName.equals("bizLocation")) { Element attrElem = (Element) curEventNode; Node id = attrElem.getElementsByTagName("id").item(0); bizLocationUri = id.getTextContent(); } else if (nodeName.equals("epcClass")) { epcClassUri = curEventNode.getTextContent(); } else if (nodeName.equals("quantity")) { quantity = Long.valueOf(curEventNode.getTextContent()); } else if (nodeName.equals("parentID")) { checkEpcOrUri(curEventNode.getTextContent(), false); parentId = curEventNode.getTextContent(); } else { String[] parts = nodeName.split(":"); if (parts.length == 2) { LOG.debug(" treating unknown event field as extension."); String prefix = parts[0]; String localname = parts[1]; // String namespace = // document.getDocumentElement().getAttribute("xmlns:" + // prefix); String namespace = curEventNode.lookupNamespaceURI(prefix); String value = curEventNode.getTextContent(); EventFieldExtension evf = new EventFieldExtension(prefix, namespace, localname, value); fieldNameExtList.add(evf); } else { // this is not a valid extension throw new SAXException(" encountered unknown event field: '" + nodeName + "'."); } } } if (eventType.equals(EpcisConstants.AGGREGATION_EVENT)) { // for AggregationEvents, the parentID is only optional for // action=OBSERVE if (parentId == null && ("ADD".equals(action) || "DELETE".equals(action))) { throw new InvalidFormatException("'parentID' is required if 'action' is ADD or DELETE"); } } // Changed by nkef (use "getOrEditVocabularyElement" instead of // "getOrInsertVocabularyElement") String nodeName = eventNode.getNodeName(); VocabularyElement bizStep = bizStepUri != null ? getOrEditVocabularyElement(session, EpcisConstants.BUSINESS_STEP_ID, String.valueOf(bizStepUri), "1") : null; VocabularyElement disposition = dispositionUri != null ? getOrEditVocabularyElement(session, EpcisConstants.DISPOSITION_ID, String.valueOf(dispositionUri), "1") : null; VocabularyElement readPoint = readPointUri != null ? getOrEditVocabularyElement(session, EpcisConstants.READ_POINT_ID, String.valueOf(readPointUri), "1") : null; VocabularyElement bizLocation = bizLocationUri != null ? getOrEditVocabularyElement(session, EpcisConstants.BUSINESS_LOCATION_ID, String.valueOf(bizLocationUri), "1") : null; VocabularyElement epcClass = epcClassUri != null ? getOrEditVocabularyElement(session, EpcisConstants.EPC_CLASS_ID, String.valueOf(epcClassUri), "1") : null; BaseEvent be; if (nodeName.equals(EpcisConstants.AGGREGATION_EVENT)) { AggregationEvent ae = new AggregationEvent(); ae.setParentId(parentId); ae.setChildEpcs(epcs); ae.setAction(Action.valueOf(action)); be = ae; } else if (nodeName.equals(EpcisConstants.OBJECT_EVENT)) { ObjectEvent oe = new ObjectEvent(); oe.setAction(Action.valueOf(action)); if (epcs != null && epcs.size() > 0) { oe.setEpcList(epcs); } be = oe; } else if (nodeName.equals(EpcisConstants.QUANTITY_EVENT)) { QuantityEvent qe = new QuantityEvent(); qe.setEpcClass((EPCClass) epcClass); if (quantity != null) { qe.setQuantity(quantity.longValue()); } be = qe; } else if (nodeName.equals(EpcisConstants.TRANSACTION_EVENT)) { TransactionEvent te = new TransactionEvent(); te.setParentId(parentId); te.setEpcList(epcs); te.setAction(Action.valueOf(action)); be = te; } else { throw new SAXException("Encountered unknown event element '" + nodeName + "'."); } be.setEventTime(eventTime); be.setRecordTime(recordTime); be.setEventTimeZoneOffset(eventTimeZoneOffset); be.setBizStep((BusinessStepId) bizStep); be.setDisposition((DispositionId) disposition); be.setBizLocation((BusinessLocationId) bizLocation); be.setReadPoint((ReadPointId) readPoint); if (bizTransList != null && bizTransList.size() > 0) { be.setBizTransList(bizTransList); } if (!fieldNameExtList.isEmpty()) { be.setExtensions(fieldNameExtList); } session.save(be); }