You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by ch...@apache.org on 2010/07/07 05:58:27 UTC
svn commit: r961110 -
/activemq/sandbox/activemq-apollo-actor/activemq-dto/src/main/java/org/apache/activemq/apollo/dto/XmlEncoderDecoder.java
Author: chirino
Date: Wed Jul 7 03:58:27 2010
New Revision: 961110
URL: http://svn.apache.org/viewvc?rev=961110&view=rev
Log:
Adding missing file breaking compile
Added:
activemq/sandbox/activemq-apollo-actor/activemq-dto/src/main/java/org/apache/activemq/apollo/dto/XmlEncoderDecoder.java
Added: activemq/sandbox/activemq-apollo-actor/activemq-dto/src/main/java/org/apache/activemq/apollo/dto/XmlEncoderDecoder.java
URL: http://svn.apache.org/viewvc/activemq/sandbox/activemq-apollo-actor/activemq-dto/src/main/java/org/apache/activemq/apollo/dto/XmlEncoderDecoder.java?rev=961110&view=auto
==============================================================================
--- activemq/sandbox/activemq-apollo-actor/activemq-dto/src/main/java/org/apache/activemq/apollo/dto/XmlEncoderDecoder.java (added)
+++ activemq/sandbox/activemq-apollo-actor/activemq-dto/src/main/java/org/apache/activemq/apollo/dto/XmlEncoderDecoder.java Wed Jul 7 03:58:27 2010
@@ -0,0 +1,122 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.activemq.apollo.dto;
+
+import javax.xml.bind.JAXBContext;
+import javax.xml.bind.JAXBException;
+import javax.xml.bind.Marshaller;
+import javax.xml.bind.Unmarshaller;
+import javax.xml.stream.XMLInputFactory;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamReader;
+import javax.xml.stream.util.StreamReaderDelegate;
+import java.io.*;
+import java.net.URL;
+import java.util.Properties;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ * @author <a href="http://hiramchirino.com">Hiram Chirino</a>
+ */
+public class XmlEncoderDecoder {
+
+ /**
+ * Changes ${property} with values from a properties object
+ */
+ static public class PropertiesFilter extends StreamReaderDelegate {
+
+ static final Pattern pattern = Pattern.compile("\\$\\{[^\\}]\\}");
+ private final Properties props;
+
+ public PropertiesFilter(XMLStreamReader parent, Properties props) {
+ super(parent);
+ this.props = props;
+ }
+
+ public String getAttributeValue(int index) {
+ return filter(super.getAttributeValue(index));
+ }
+
+ public String filter(String str) {
+ int start = 0;
+ while (true) {
+ Matcher matcher = pattern.matcher(str);
+ if (!matcher.find(start)) {
+ break;
+ }
+ String property = System.getProperty(matcher.group(1));
+ if (property != null) {
+ str = matcher.replaceFirst(property);
+ } else {
+ start = matcher.end();
+ }
+ }
+ return str;
+ }
+
+ }
+
+ private static final XMLInputFactory factory = XMLInputFactory.newInstance();
+ private static final JAXBContext context;
+
+ static {
+ try {
+ context = JAXBContext.newInstance("org.apache.activemq.apollo.dto");
+ } catch (JAXBException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ static public BrokerDTO unmarshalBrokerDTO(URL url) throws IOException, XMLStreamException, JAXBException {
+ return unmarshalBrokerDTO(url, null);
+ }
+
+ static public BrokerDTO unmarshalBrokerDTO(URL url, Properties props) throws IOException, XMLStreamException, JAXBException {
+ return unmarshalBrokerDTO(url.openStream(), props);
+ }
+
+ static public BrokerDTO unmarshalBrokerDTO(InputStream is) throws IOException, XMLStreamException, JAXBException {
+ return unmarshalBrokerDTO(is, null);
+ }
+
+ static public BrokerDTO unmarshalBrokerDTO(InputStream is, Properties props) throws IOException, XMLStreamException, JAXBException {
+ if (is == null) {
+ throw new IllegalArgumentException("input stream was null");
+ }
+ try {
+ XMLStreamReader reader = factory.createXMLStreamReader(is);
+ if (props != null) {
+ reader = new PropertiesFilter(reader, props);
+ }
+ Unmarshaller unmarshaller = context.createUnmarshaller();
+ return (BrokerDTO) unmarshaller.unmarshal(reader);
+ } finally {
+ is.close();
+ }
+ }
+
+ static public void marshalBrokerDTO(BrokerDTO in, OutputStream os, boolean format) throws JAXBException {
+ Marshaller marshaller = context.createMarshaller();
+ if( format ) {
+ marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, java.lang.Boolean.TRUE);
+ }
+ marshaller.marshal(in, new OutputStreamWriter(os));
+ }
+
+
+}