/** * Parse a value, ref or collection sub-element of a property or * constructor-arg element. * @param ele subelement of property element; we don't know which yet * @param defaultTypeClassName the default type (class name) for any * <code><value></code> tag that might be created */ public static Object parseDataSubElement(Element ele, String defaultTypeClassName) throws XMLParseException{ if (VALUE_ELEMENT.equals(ele.getName())) return parseValueElement(ele, defaultTypeClassName); else if (NULL_ELEMENT.equals(ele.getName())) return null; else if (LIST_ELEMENT.equals(ele.getName())) return parseListElement(ele); else if (SET_ELEMENT.equals(ele.getName())) return parseSetElement(ele); else if (MAP_ELEMENT.equals(ele.getName())) return parseMapElement(ele); else if (PROPS_ELEMENT.equals(ele.getName())) return parsePropsElement(ele); else throw new XMLParseException("Unknown property sub-element: [" + ele.getName() + "]"); }
@Override public Response toResponse(final XMLParseException exception) { return Response .status(Response.Status.BAD_REQUEST) .type(getMediaType(headers,MediaType.APPLICATION_XML_TYPE)) .entity(new ErrorCodeDto(ErrorCodeDto.VMIDC_VALIDATION_EXCEPTION_ERROR_CODE, Arrays.asList( "Value "+ exception.getMessage() + " is invalid" ))) .build(); }
/** * Return a typed String value Object for the given value element. */ public static Object parseValueElement(Element ele, String defaultTypeClassName) throws XMLParseException{ String value = ele.getTextTrim(); String typeClassName = ele.attributeValue(TYPE_ATTRIBUTE); if (StringUtils.isBlank(typeClassName)) { typeClassName = defaultTypeClassName; } try { return buildTypedStringValue(value, typeClassName, ele); }catch (ClassNotFoundException ex) { log.error("Type class [{}] not found for <value> element", typeClassName, ex); return value; } }
protected static Object buildTypedStringValue(String value, String targetTypeName, Element ele) throws ClassNotFoundException, XMLParseException { if(StringUtils.isNotBlank(targetTypeName)) { Class<?> clazz = classPool.get(targetTypeName); if(clazz == null) clazz = Class.forName(targetTypeName); if (clazz != null) return DataConvert.convertType(value, clazz); } return value; }
public static List parseListElement(Element collectionEle) throws XMLParseException{ String defaultTypeClassName = collectionEle.attributeValue(VALUE_TYPE_ATTRIBUTE); List<Element> el = collectionEle.elements(); List values = new ArrayList(el.size()); for (Element e : el) { if (!COMMENT_ELEMENT.equals(e.getName())) { values.add(parseDataSubElement(e, defaultTypeClassName)); } } return values; }
public static Set parseSetElement(Element collectionEle) throws XMLParseException{ String defaultTypeClassName = collectionEle.attributeValue(VALUE_TYPE_ATTRIBUTE); List<Element> el = collectionEle.elements(); Set set = new HashSet(el.size()); for (Element e : el) { if (!COMMENT_ELEMENT.equals(e.getName())) { set.add(parseDataSubElement(e, defaultTypeClassName)); } } return set; }
protected static Object buildTypedStringValueForMap(String value, String defaultTypeClassName, Element entryEle) throws XMLParseException{ try { return buildTypedStringValue(value, defaultTypeClassName, entryEle); } catch (ClassNotFoundException ex) { log.error("Type class [{}] not found for Map key/value type {}", defaultTypeClassName, entryEle, ex); return value; } }
public static Properties parsePropsElement(Element propsEle) throws XMLParseException{ Properties props = new Properties(); List propEles = propsEle.elements(PROP_ELEMENT); for (Iterator it = propEles.iterator(); it.hasNext();) { Element propEle = (Element) it.next(); String key = propEle.attributeValue(KEY_ATTRIBUTE); // Trim the text value to avoid unwanted whitespace // caused by typical XML formatting. String value = propEle.getTextTrim(); props.put(key, value); } return props; }
@SuppressWarnings("unchecked") public static void loadXmlFile(Properties properties, InputStream i) throws DocumentException, XMLParseException { SAXReader reader = new SAXReader(); Document document = reader.read(i); Element root = document.getRootElement(); List<Element> subs = root.elements("data"); for (Element e : subs) { String nameVal = e.attributeValue("name"); if (StringUtils.isNotBlank(nameVal)) properties.put(nameVal, XmlDataParser.parseDataElement(e)); } }
private void readMappings(Reader in) throws FileNotFoundException, XMLStreamException, XMLParseException { //open the orthoXML for reading OrthoXMLReader reader = new OrthoXMLReader(in); // get species from reader for (Species species : reader.getSpecies()) { System.out.printf(species.getName() + "\t"); System.out.printf(species.getNcbiTaxId() + "\t"); } System.out.printf("#"); //read the group iteratively Group group; while ((group = reader.next()) != null) { Set<Xref> groupRefs = new HashSet<Xref>(); for(Gene gene : group.getNestedGenes()) { Database database = gene.getDatabase(); DataSource ds = aliases.get(database.getName()); if (ds == null) ds = DataSource.getByFullName(database.getName()); dataSources.add(ds); groupRefs.add (new Xref(gene.getGeneIdentifier(), ds)); groupRefs.add (new Xref(gene.getProteinIdentifier(), ds)); } for (Xref ref : groupRefs) { allIds.put (ref, groupRefs); } } }
private void buildHostNodes(List<XmlNodeWrapper> contexts) throws Throwable { if (0 == contexts.size()) { throw new XmlParseException("Missing <mqSource> node."); } for (XmlNodeWrapper xNode : contexts) { String id = StringUtils.trim(xNode.getStringAttribute("id")); if (context.getMqSourceMap().containsKey(id)) { throw new XmlParseException("Duplicate <mqSource> id: " + id); } MqSourceType type = null; String _type = StringUtils.trim(xNode.getStringAttribute("type")); type = getMqSourceType(_type); if (null == type) { throw new XmlParseException("Unsupported MQ types in <mqSource>: " + id); } boolean defaultMs = false; String _isDefault = StringUtils.trim(xNode.getStringAttribute("isDefault")); if (null != _isDefault) { defaultMs = Boolean.parseBoolean(_isDefault); if (defaultMs) { if (null != this.defaultMqSource) { throw new XMLParseException("The default mqSource can only have one"); } this.defaultMqSource = id; } } Map<String, String> data = new HashMap<String, String>(); List<XmlNodeWrapper> properties = xNode.evalNodes("property"); for (XmlNodeWrapper propertyNode : properties) { data.put(StringUtils.trim(propertyNode.getStringAttribute("name")).toUpperCase(), StringUtils.trim(propertyNode.getStringAttribute("value"))); } MqSourceVo hostVo = new MqSourceVo(id, type, data); context.getMqSourceMap().put(id, hostVo); log.info("add mq source: " + id); } this.context.setDefaultMqSource(defaultMqSource); MqSourceManager manager = new MqManagerCreater().create(defaultMqSource, context.getMqSourceMap()); MqContainer.getInstance().setMqSourceManager(manager); }
@Override public void start() throws Throwable { boolean durableSubscribers = false;// 持久化订阅 String clientID = null; if (ChannelType.Topic == queue.getType()) { // durableSubscribers = (Boolean) queue.getProperties().get(ActiveMqVo.ACTIVEMQ_C_DURABLESUBSCRIBERS); durableSubscribers = queue.isDurableSubscribers(); if (durableSubscribers) { // clientID = (String) queue.getProperties().get(ActiveMqVo.ACTIVEMQ_C_CLIENTID); clientID = queue.getClientID(); if (null == clientID) { throw new XMLParseException("durable subscribers is missing a clientID: " + queue.getName()); } } } ActiveMqSource mqSource = (ActiveMqSource) MqContainer.getInstance().getMqSourceManager().getMqSource(queue.getMsKey()); Connection connection = null; if (!durableSubscribers) { connection = mqSource.getConnection(); } else { connection = mqSource.getConnection(clientID); this.durableSubscriberConn = connection; } // boolean transacted = (Boolean) queue.getProperties().get(ActiveMqVo.ACTIVEMQ_C_TRANSACTED); // int acknowledgeMode = (Integer) queue.getProperties().get(ActiveMqVo.ACTIVEMQ_C_ACKNOWLEDGEMODE); boolean transacted = queue.isTransacted(); int acknowledgeMode = queue.getAcknowledgeMode(); session = connection.createSession(transacted, acknowledgeMode); Destination destination = null; if (ChannelType.Queue == queue.getType()) { destination = session.createQueue(queue.getName()); typeStr = "queue"; } else if (ChannelType.Topic == queue.getType()) { destination = session.createTopic(queue.getName()); typeStr = "topic"; } MessageConsumer messageConsumer = null; if (!durableSubscribers) { messageConsumer = session.createConsumer(destination); } else { messageConsumer = session.createDurableSubscriber((Topic) destination, clientID); } running = true; // boolean asynReceiveMessages = (Boolean) queue.getProperties().get(ActiveMqVo.ACTIVEMQ_C_ASYNRECEIVEMESSAGES); boolean asynReceiveMessages = queue.isAsynReceive(); if (asynReceiveMessages) { messageConsumer.setMessageListener(new MessageListener() { @Override public void onMessage(Message message) { try { // TODO 如果是session.commit();, 是否需要使用同步关键字, 防止提交别的线程的东西 // System.out.println("####################:" + Thread.currentThread().getName()); processMessage(message); } catch (Throwable e) { log.error("listen to the [" + queue.getName() + "] error.", e); } } }); } else { // long receiveTimeout = (Long) queue.getProperties().get(ActiveMqVo.ACTIVEMQ_C_RECEIVETIMEOUT); long receiveTimeout = queue.getReceiveTimeout(); startSyncReceiveThread(messageConsumer, receiveTimeout); } }
public static Object parseDataSubElement(Element ele) throws XMLParseException{ return parseDataSubElement(ele, null); }
public static Map parseMapElement(Element mapEle) throws XMLParseException{ String defaultKeyTypeClassName = mapEle.attributeValue(KEY_TYPE_ATTRIBUTE); String defaultValueTypeClassName = mapEle.attributeValue(VALUE_TYPE_ATTRIBUTE); List entryEles = mapEle.elements(ENTRY_ELEMENT); Map map = new HashMap(entryEles.size()); for (Iterator it = entryEles.iterator(); it.hasNext();) { Element entryEle = (Element) it.next(); // Should only have one value child element: value, list, etc. // Optionally, there might be a key child element. List<Element> el = entryEle.elements(); Element keyEle = null; Element valueEle = null; for (Element candidateEle : el) { if (KEY_ELEMENT.equals(candidateEle.getName())) { if (keyEle != null) { throw new XMLParseException(entryEle.attributeValue("name") + "<entry> element is only allowed to contain one <key> sub-element"); } else { keyEle = candidateEle; } } else { if (valueEle != null) { throw new XMLParseException(entryEle.attributeValue("name") + "<entry> element must not contain more than one value sub-element"); } else { valueEle = candidateEle; } } } // Extract key from attribute or sub-element. Object key; Attribute keyAttribute = entryEle.attribute(KEY_ATTRIBUTE); if (keyAttribute != null && keyEle != null) { throw new XMLParseException(entryEle + "<entry> element is only allowed to contain either " + "a 'key' attribute OR a <key> sub-element"); } if (keyAttribute != null) { key = buildTypedStringValueForMap( entryEle.attributeValue(KEY_ATTRIBUTE), defaultKeyTypeClassName, entryEle); } else if (keyEle != null) { key = parseKeyElement(keyEle,defaultKeyTypeClassName); } else { throw new XMLParseException("<entry> element must specify a key" + entryEle); } // Extract value from attribute or sub-element. Object value; Attribute valueAttribute = entryEle.attribute(VALUE_ATTRIBUTE); if (valueAttribute != null && valueEle != null) { throw new XMLParseException("<entry> element is only allowed to contain either " + "'value' attribute OR <value> sub-element" + entryEle); } if (valueAttribute != null) { value = buildTypedStringValueForMap( entryEle.attributeValue(VALUE_ATTRIBUTE), defaultValueTypeClassName, entryEle); } else if (valueEle != null) { value = parseDataSubElement(valueEle,defaultValueTypeClassName); } else { throw new XMLParseException("<entry> element must specify a value" + entryEle); } map.put(key, value); } return map; }
/** * * @param properties * @param resource 相对于项目的资源文件 * @throws IOException * @throws XMLParseException * @throws DocumentException */ public static void loadXmlFile(Properties properties, String resource) throws IOException, DocumentException, XMLParseException { Assert.notNull(properties, "'properties' can't be null."); log.info("properties load xml file {}.", resource); try (InputStream is = Classes.getClassLoader().getResourceAsStream(resource)) { loadXmlFile(properties, is); } }
/** * * @param properties * @param absoluteFile 文件的绝对地址 * @throws IOException * @throws FileNotFoundException * @throws XMLParseException * @throws DocumentException */ public static void loadAbsoluteXmlFile(Properties properties, String absoluteFile) throws IOException, DocumentException, XMLParseException { Assert.notNull(properties, "'properties' can't be null."); log.info("properties load file {}.", absoluteFile); try (InputStream i = new FileInputStream(absoluteFile)) { loadXmlFile(properties, i); } }