Deserialize / unmarshal generic list from XML to list in Android - java

I made a webservice in java with a method that returns a string (a generic list in XML format). I consume this webservice from Android, and I get this string, but after several tries the Android emulator just crashes when trying to deserialize the string. This is an example for the string I get:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<peliculas>
<pelicula>
<id>18329</id>
<poster>http://cache-cmx.netmx.mx/image/muestras/5368.rrr.jpg</poster>
<titulo>007 Operaci&oacute;n Skyfall</titulo>
</pelicula>
...
</peliculas>
This is the class in the webservice:
#XmlRootElement
public class Peliculas{
#XmlElement(name="pelicula")
protected List<Pelicula> peliculas;
public Peliculas(){ peliculas = new ArrayList<Pelicula>();}
public Peliculas(List<Pelicula> pe){
peliculas = pe;
}
public List<Pelicula> getList(){
return peliculas;
}
public void add(Pelicula pelicula) {
peliculas.add(pelicula);
}
}
________EDIT______________
Seems like you can't use JAXB with Android, and there's better/lighter libraries for that. so I tried Simple XML. This is the method:
public Peliculas unmarshal(String xml) throws Exception{
Peliculas peliculas = new Peliculas();
Serializer serializer = new Persister();
StringBuffer xmlStr = new StringBuffer( xml );
peliculas = serializer.read(Peliculas.class, ( new StringReader( xmlStr.toString() ) ) );
return peliculas;
}
BUT I get this exception, seems like it can't save data in object:
11-12 20:30:10.898: I/Error(1058): Element 'Pelicula' does not have a match in class app.cinemexservice.Pelicula at line 3

I think you are doing correct, Try this code which is given in the API.
JAXBContext jc = JAXBContext.newInstance( "add your class's full qualified class name here" );
Unmarshaller u = jc.createUnmarshaller();
Object o = u.unmarshal( xmlSource );
You can cast the Object o to your type I think. Please refer this. http://jaxb.java.net/nonav/2.2.4/docs/api/javax/xml/bind/Unmarshaller.html

I used SAX to parse the file, and then convert it manually to an object. This is the code:
public List<Pelicula> unmarshal(String xml) throws Exception{
List<Pelicula> peliculas = new ArrayList<Pelicula>();
InputStream is = new ByteArrayInputStream(xml.getBytes("UTF-8"));
XmlPullParser parser = Xml.newPullParser();
char[] c;
String id="", titulo="", poster="", atributo="";
int datos =0;
try{
parser.setInput(is, "UTF-8");
int event = parser.next();
while(event != XmlPullParser.END_DOCUMENT) {
if(event == XmlPullParser.START_TAG) {
Log.d(TAG, "<"+ parser.getName() + ">");
atributo = parser.getName();
for(int i = 0; i < parser.getAttributeCount(); i++) {
Log.d(TAG, "\t"+ parser.getAttributeName(i) + " = "+ parser.getAttributeValue(i));
}
}
if(event == XmlPullParser.TEXT&& parser.getText().trim().length() != 0)
{
Log.d(TAG, "\t\t"+ parser.getText());
if (atributo=="id"){id=parser.getText(); datos++;}
else if(atributo=="titulo"){titulo=parser.getText(); datos++;}
else if(atributo=="poster"){poster=parser.getText(); datos++;}
if(datos==3){peliculas.add(new Pelicula(id, titulo, poster)); datos=0;}
}
if(event == XmlPullParser.END_TAG)
Log.d(TAG, "</"+ parser.getName() + ">");
event = parser.next();
is.close();
}
} catch(Exception e) { Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show(); }
for (Pelicula p : peliculas){
Log.d("PelĂ­cula en lista: ", p.titulo);
}
return peliculas;
}
It's way too long for my taste, but I just couldn't figure out Simple XML to match my classes.

Related

javax.xml.bind.JAXBElement cannot be cast to com.oracle.xmlns.communications.ordermanagement.CreateOrderRequestType

Here is the operation that I am working on. I generated WSDL and I got CreateOrderRequestType.java
But when I am unmarshalling , it is looking for CreateOrder.
How can I get CreateOrder class.
#WebMethod(operationName = "CreateOrder", action = "http://xmlns.oracle.com/communications/ordermanagement/CreateOrder")
#WebResult(name = "CreateOrderResponse", targetNamespace = "http://xmlns.oracle.com/communications/ordermanagement", partName = "CreateOrderResponse")
public CreateOrderResponseType createOrder(
#WebParam(name = "CreateOrder", targetNamespace = "http://xmlns.oracle.com/communications/ordermanagement", partName = "CreateOrderRequest")
CreateOrderRequestType createOrderRequest)
throws CreateOrderFaultMsg, CreateOrderFaultMsg1, CreateOrderFaultMsg3
;
public static CreateOrderRequestType convertXmlStringToJavaObject(String xmlString) {
JAXBContext jaxbContext;
try {
jaxbContext = JAXBContext.newInstance(CreateOrderRequestType.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
StringReader reader = new StringReader(xmlString);
CreateOrderRequestType msg = (CreateOrderRequestType) unmarshaller.unmarshal(reader);
return msg;
} catch (JAXBException e) {
throw new OSMException("Unable to Convert XML String to Java Objects " + e.getMessage(), e);
}
}
public CreateOrderResponseType createAncillariesBySoloOrderId(String soloOrderId) {
String message = findOrderBySoloOrderId(soloOrderId);
//CreateOrderRequestType createOrderRequestType = null;
message = "<CreateOrder xmlns=\"http://xmlns.oracle.com/communications/ordermanagement\">"
+ message
+ "</CreateOrder>";
log.info("message before converting to Java Object = " + message);
CreateOrderRequestType createOrderRequestType = com.spectrum.order.util.OrderUtil.convertXmlStringToJavaObject(message);
log.info("Msg Object = " + createOrderRequestType.toString());
return osmClient.createOrder(createOrderRequestType);
}
My xmlString
<CreateOrder xmlns="http://xmlns.oracle.com/communications/ordermanagement">
<msg xmlns=""><head><Client>Charter</Client><Routing>8260|1700|0090</Routing></head></msg>
</CreateOrder>
It seems, that during unmarshalling an instance of JAXBElement is returned. You can check for the returned type and handle it appropriately:
Object obj = unmarshaller.unmarshal(reader);
CreateOrderRequestType msg = null;
if (obj instanceof CreateOrderRequestType) {
msg = (CreateOrderRequestType) obj;
}
if (obj instanceof JAXBElement) {
msg = ((JAXBElement<CreateOrderRequestType>) obj).getValue();
}
if (msg == null) {
throw new OSMException("Unable to Convert XML String to Java Objects ");
}
And by the way, it would be better make variable JAXBContext jaxbContext; the class variable and initiate it not during service call, but in constructor or in some initialization method, because initialization of JAXBContext is time consuming operation and should normally happen once. JAXBContext is thread safe and making it a class variable should not affect service functionality.

Transform XML to declare all namespaces on root element

Is there a simple Java method way of "moving" all XML namespace declarations of an XML document to the root element? Due to a bug in parser implementation of an unnamed huge company, I need to programmatically rewrite our well formed and valid RPC requests in a way that the root element declares all used namespaces.
Not OK:
<document-element xmlns="uri:ns1">
<foo>
<bar xmlns="uri:ns2" xmlns:ns3="uri:ns3">
<ns3:foobar/>
<ns1:sigh xmlns:ns1="uri:ns1"/>
</bar>
</foo>
</document-element>
OK:
<document-element xmlns="uri:ns1" xmlns:ns1="uri:ns1" xmlns:ns2="uri:ns2" xmlns:ns3="uri:ns3">
<foo>
<ns2:bar>
<ns3:foobar/>
<ns1:sigh/>
</ns2:bar>
</foo>
</document-element>
Generic names for missing prefixes are acceptable. Default namespace may stay or be replaced/added as long as it is defined on the root element. I don't really mind which specific XML technology is used to achieve this (I would prefer to avoid DOM though).
To clarify, this answer refers to what I'd like to achieve as redeclaring namespace declarations within root element scope (entire document) on the root element. Essentially the related question is asking why oh why would anyone implement what I now need to work around.
Wrote a two-pass StAX reader/writer, which is simple enough.
import java.io.*;
import java.util.*;
import javax.xml.stream.*;
import javax.xml.stream.events.*;
public class NamespacesToRoot {
private static final String GENERATED_PREFIX = "pfx";
private final XMLInputFactory inputFact;
private final XMLOutputFactory outputFact;
private final XMLEventFactory eventFactory;
private NamespacesToRoot() {
inputFact = XMLInputFactory.newInstance();
outputFact = XMLOutputFactory.newInstance();
eventFactory = XMLEventFactory.newInstance();
}
public String transform(String xmlString) throws XMLStreamException {
Map<String, String> pfxToNs = new HashMap<String, String>();
XMLEventReader reader = null;
// first pass - analyze
try {
if (xmlString == null || xmlString.isEmpty()) {
throw new IllegalArgumentException("xmlString is null or empty");
}
StringReader stringReader = new StringReader(xmlString);
XMLStreamReader streamReader = inputFact.createXMLStreamReader(stringReader);
reader = inputFact.createXMLEventReader(streamReader);
while (reader.hasNext()) {
XMLEvent event = reader.nextEvent();
if (event.isStartElement()) {
buildNamespaces(event, pfxToNs);
}
}
System.out.println(pfxToNs);
} finally {
try {
if (reader != null) {
reader.close();
}
} catch (XMLStreamException ex) {
}
}
// reverse mapping, also gets rid of duplicates
Map<String, String> nsToPfx = new HashMap<String, String>();
for (Map.Entry<String, String> entry : pfxToNs.entrySet()) {
nsToPfx.put(entry.getValue(), entry.getKey());
}
List<Namespace> namespaces = new ArrayList<Namespace>(nsToPfx.size());
for (Map.Entry<String, String> entry : nsToPfx.entrySet()) {
namespaces.add(eventFactory.createNamespace(entry.getValue(), entry.getKey()));
}
// second pass - rewrite
XMLEventWriter writer = null;
try {
StringWriter stringWriter = new StringWriter();
writer = outputFact.createXMLEventWriter(stringWriter);
StringReader stringReader = new StringReader(xmlString);
XMLStreamReader streamReader = inputFact.createXMLStreamReader(stringReader);
reader = inputFact.createXMLEventReader(streamReader);
boolean rootElement = true;
while (reader.hasNext()) {
XMLEvent event = reader.nextEvent();
if (event.isStartElement()) {
StartElement origStartElement = event.asStartElement();
String prefix = nsToPfx.get(origStartElement.getName().getNamespaceURI());
String namespace = origStartElement.getName().getNamespaceURI();
String localName = origStartElement.getName().getLocalPart();
Iterator attributes = origStartElement.getAttributes();
Iterator namespaces_;
if (rootElement) {
namespaces_ = namespaces.iterator();
rootElement = false;
} else {
namespaces_ = null;
}
writer.add(eventFactory.createStartElement(
prefix, namespace, localName, attributes, namespaces_));
} else {
writer.add(event);
}
}
writer.flush();
return stringWriter.toString();
} finally {
try {
if (reader != null) {
reader.close();
}
} catch (XMLStreamException ex) {
}
try {
if (writer != null) {
writer.close();
}
} catch (XMLStreamException ex) {
}
}
}
private void buildNamespaces(XMLEvent event, Map<String, String> pfxToNs) {
System.out.println("el: " + event);
StartElement startElement = event.asStartElement();
Iterator nsIternator = startElement.getNamespaces();
while (nsIternator.hasNext()) {
Namespace nsAttr = (Namespace) nsIternator.next();
if (nsAttr.isDefaultNamespaceDeclaration()) {
System.out.println("need to generate a prefix for " + nsAttr.getNamespaceURI());
generatePrefix(nsAttr.getNamespaceURI(), pfxToNs);
} else {
System.out.println("add prefix binding for " + nsAttr.getPrefix() + " --> " + nsAttr.getNamespaceURI());
addPrefix(nsAttr.getPrefix(), nsAttr.getNamespaceURI(), pfxToNs);
}
}
}
private void generatePrefix(String namespace, Map<String, String> pfxToNs) {
int i = 1;
String prefix = GENERATED_PREFIX + i;
while (pfxToNs.keySet().contains(prefix)) {
i++;
prefix = GENERATED_PREFIX + i;
}
pfxToNs.put(prefix, namespace);
}
private void addPrefix(String prefix, String namespace, Map<String, String> pfxToNs) {
String existingNs = pfxToNs.get(prefix);
if (existingNs != null) {
if (existingNs.equals(namespace)) {
// nothing to do
} else {
// prefix clash, need to rename this prefix or reuse an existing
// one
if (pfxToNs.values().contains(namespace)) {
// reuse matching prefix
} else {
// rename
generatePrefix(namespace, pfxToNs);
}
}
} else {
// need to add this prefix
pfxToNs.put(prefix, namespace);
}
}
public static void main(String[] args) throws XMLStreamException {
String xmlString = "" +
"<document-element xmlns=\"uri:ns1\" attr=\"1\">\n" +
" <foo>\n" +
" <bar xmlns=\"uri:ns2\" xmlns:ns3=\"uri:ns3\">\n" +
" <ns3:foobar ns3:attr1=\"meh\" />\n" +
" <ns1:sigh xmlns:ns1=\"uri:ns1\"/>\n" +
" </bar>\n" +
" </foo>\n" +
"</document-element>";
System.out.println(xmlString);
NamespacesToRoot transformer = new NamespacesToRoot();
System.out.println(transformer.transform(xmlString));
}
}
Note that this is just fast example code which could use some tweaks, but is also a good start for anyone with a similar problem.
Below is a simple app does the namespace re-declaration... based on XPath and VTD-XML.
import com.ximpleware.*;
import java.io.*;
public class moveNSDeclaration {
public static void main(String[] args) throws IOException, VTDException{
// TODO Auto-generated method stub
VTDGen vg = new VTDGen();
String xml="<document-element xmlns=\"uri:ns1\">\n"+
"<foo>\n"+
"<bar xmlns=\"uri:ns2\" xmlns:ns3=\"uri:ns3\">\n"+
"<ns3:foobar/>\n"+
"<ns1:sigh xmlns:ns1=\"uri:ns1\"/>\n"+
"</bar>\n"+
"</foo>\n"+
"</document-element>\n";
vg.setDoc(xml.getBytes());
vg.parse(false); // namespace unaware to all name space nodes addressable using xpath #*
VTDNav vn = vg.getNav();
XMLModifier xm = new XMLModifier(vn);
FastIntBuffer fib = new FastIntBuffer();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// get the index value of xmlns declaration of root element
AutoPilot ap =new AutoPilot (vn);
ap.selectXPath("//#*");
int i=0;
//remove all ns node under root element
//save those nodes to be re-inserted into the root element up on verification of uniqueness
while((i=ap.evalXPath())!=-1){
if (vn.getTokenType(i)==VTDNav.TOKEN_ATTR_NS){
xm.remove(); //remove all ns node
fib.append(i);
}
}
//remove redundant ns nodes
for (int j=0;j<fib.size();j++){
if (fib.intAt(j)!=-1){
for (i=j+1;i<fib.size();i++){
if (fib.intAt(i)!=-1)
if (vn.compareTokens(fib.intAt(j), vn, fib.intAt(i))==0){
fib.modifyEntry(i, -1);
}
}
}
}
// compose a string to insert back into the root element containing all subordinate ns nodes
for (int j=0;j<fib.size();j++){
if (fib.intAt(j)!=-1){
int os = vn.getTokenOffset(fib.intAt(j));
int len = vn.getTokenOffset(fib.intAt(j)+1)+vn.getTokenLength(fib.intAt(j)+1)+1-os;
//System.out.println(" os len "+ os + " "+len);
//System.out.println(vn.toString(os,len));
baos.write(" ".getBytes());
baos.write(vn.getXML().getBytes(),os,len);
}
}
byte[] attrBytes = baos.toByteArray();
vn.toElement(VTDNav.ROOT);
xm.insertAttribute(attrBytes);
//System.out.println(baos.toString());
baos.reset();
xm.output(baos);
System.out.println(baos.toString());
}
}
Output looks like
<document-element xmlns="uri:ns2" xmlns:ns3="uri:ns3" xmlns:ns1="uri:ns1" >
<foo>
<bar >
<ns3:foobar/>
<ns1:sigh />
</bar>
</foo>
</document-element>

How to process XML file in Hadoop Mapper

I have large XML file in below format. I can read line by line and doing some string operations as I only need to extract values for a couple of fields. But, in general, how do we process file in below format ? I found Mahout XML parser, but I think it is not for below format.
<?xml version="1.0" encoding="utf-8"?>
<posts>
<row Id="1" PostTypeId="1" AcceptedAnswerId="13" CreationDate="2010-09-13T19:16:26.763" Score="155" ViewCount="160162" Body="<p>This is a common question by those who have just rooted their phones. What apps, ROMs, benefits, etc. do I get from rooting? What should I be doing now?</p>
" OwnerUserId="10" LastEditorUserId="16575" LastEditDate="2013-04-05T15:50:48.133" LastActivityDate="2013-09-03T05:57:21.440" Title="I've rooted my phone. Now what? What do I gain from rooting?" Tags="<rooting><root>" AnswerCount="2" CommentCount="0" FavoriteCount="107" CommunityOwnedDate="2011-01-25T08:44:10.820" />
<row Id="2" PostTypeId="1" AcceptedAnswerId="4" CreationDate="2010-09-13T19:17:17.917" Score="10" ViewCount="966" Body="<p>I have a Google Nexus One with Android 2.2. I didn't like the default SMS-application so I installed Handcent-SMS. Now when I get an SMS, I get notified twice. How can I fix this?</p>
" OwnerUserId="7" LastEditorUserId="981" LastEditDate="2011-11-01T18:30:32.300" LastActivityDate="2011-11-01T18:30:32.300" Title="I installed another SMS application, now I get notified twice" Tags="<2.2-froyo><sms><notifications><handcent-sms>" AnswerCount="3" FavoriteCount="2" />
</posts>
The data you have posted is from SO data dump (I know because I am currently playing with it on Hadoop). Following is the mapper I've written to create a tab separated file out of this.
You essentially read line by line and use JAXP api to parse and extract the required information
public class StackoverflowDataWranglerMapper extends Mapper<LongWritable, Text, Text, Text>
{
private final Text outputKey = new Text();
private final Text outputValue = new Text();
private final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
private DocumentBuilder builder;
private static final Joiner TAG_JOINER = Joiner.on(",").skipNulls();
// 2008-07-31T21:42:52.667
private static final DateFormat DATE_PARSER = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
private static final SimpleDateFormat DATE_BUILDER = new SimpleDateFormat("yyyy-MM-dd");
#Override
protected void setup(Context context) throws IOException, InterruptedException
{
try
{
builder = factory.newDocumentBuilder();
}
catch (ParserConfigurationException e)
{
new IOException(e);
}
}
#Override
protected void map(LongWritable inputKey, Text inputValue, Mapper<LongWritable, Text, Text, Text>.Context context)
throws IOException, InterruptedException
{
try
{
String entry = inputValue.toString();
if (entry.contains("<row "))
{
Document doc = builder.parse(new InputSource(new StringReader(entry)));
Element rootElem = doc.getDocumentElement();
String id = rootElem.getAttribute("Id");
String postedBy = rootElem.getAttribute("OwnerUserId").trim();
String viewCount = rootElem.getAttribute("ViewCount");
String postTypeId = rootElem.getAttribute("PostTypeId");
String score = rootElem.getAttribute("Score");
String title = rootElem.getAttribute("Title");
String tags = rootElem.getAttribute("Tags");
String answerCount = rootElem.getAttribute("AnswerCount");
String commentCount = rootElem.getAttribute("CommentCount");
String favoriteCount = rootElem.getAttribute("FavoriteCount");
String creationDate = rootElem.getAttribute("CreationDate");
Date parsedDate = null;
if (creationDate != null && creationDate.trim().length() > 0)
{
try
{
parsedDate = DATE_PARSER.parse(creationDate);
}
catch (ParseException e)
{
context.getCounter("Bad Record Counters", "Posts missing CreationDate").increment(1);
}
}
if (postedBy.length() == 0 || postedBy.trim().equals("-1"))
{
context.getCounter("Bad Record Counters", "Posts with either empty UserId or UserId contains '-1'")
.increment(1);
try
{
parsedDate = DATE_BUILDER.parse("2100-00-01");
}
catch (ParseException e)
{
// ignore
}
}
tags = tags.trim();
String tagTokens[] = null;
if (tags.length() > 1)
{
tagTokens = tags.substring(1, tags.length() - 1).split("><");
}
else
{
context.getCounter("Bad Record Counters", "Untagged Posts").increment(1);
}
outputKey.clear();
outputKey.set(id);
StringBuilder sb = new StringBuilder(postedBy).append("\t").append(parsedDate.getTime()).append("\t")
.append(postTypeId).append("\t").append(title).append("\t").append(viewCount).append("\t").append(score)
.append("\t");
if (tagTokens != null)
{
sb.append(TAG_JOINER.join(tagTokens)).append("\t");
}
else
{
sb.append("").append("\t");
}
sb.append(answerCount).append("\t").append(commentCount).append("\t").append(favoriteCount).toString();
outputValue.set(sb.toString());
context.write(outputKey, outputValue);
}
}
catch (SAXException e)
{
context.getCounter("Bad Record Counters", "Unparsable records").increment(1);
}
finally
{
builder.reset();
}
}
}

BeanSerializer/BeanDeserializer Axis Generated Object

I have a large number of axis generated objects from several WSDLs, I need a generic solution to store the objects in xml format in the database, but also load them back in java when needed.
This is what I've made so far:
private String serializeAxisObject(Object obj) throws Exception {
if (obj == null) {
return null;
}
StringWriter outStr = new StringWriter();
TypeDesc typeDesc = getAxisTypeDesc(obj);
QName qname = typeDesc.getXmlType();
String lname = qname.getLocalPart();
if (lname.startsWith(">") && lname.length() > 1)
lname = lname.substring(1);
qname = new QName(qname.getNamespaceURI(), lname);
AxisServer server = new AxisServer();
BeanSerializer ser = new BeanSerializer(obj.getClass(), qname, typeDesc);
SerializationContext ctx = new SerializationContext(outStr,
new MessageContext(server));
ctx.setSendDecl(false);
ctx.setDoMultiRefs(false);
ctx.setPretty(true);
try {
ser.serialize(qname, new AttributesImpl(), obj, ctx);
} catch (final Exception e) {
throw new Exception("Unable to serialize object "
+ obj.getClass().getName(), e);
}
String xml = outStr.toString();
return xml;
}
private Object deserializeAxisObject(Class<?> cls, String xml)
throws Exception {
final String SOAP_START = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"><soapenv:Header /><soapenv:Body>";
final String SOAP_START_XSI = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><soapenv:Header /><soapenv:Body>";
final String SOAP_END = "</soapenv:Body></soapenv:Envelope>";
Object result = null;
try {
Message message = new Message(SOAP_START + xml + SOAP_END);
result = message.getSOAPEnvelope().getFirstBody()
.getObjectValue(cls);
} catch (Exception e) {
try {
Message message = new Message(SOAP_START_XSI + xml + SOAP_END);
result = message.getSOAPEnvelope().getFirstBody()
.getObjectValue(cls);
} catch (Exception e1) {
throw new Exception(e1);
}
}
return result;
}
private TypeDesc getAxisTypeDesc(Object obj) throws Exception {
final Class<? extends Object> objClass = obj.getClass();
try {
final Method methodGetTypeDesc = objClass.getMethod("getTypeDesc",
new Class[] {});
final TypeDesc typeDesc = (TypeDesc) methodGetTypeDesc.invoke(obj,
new Object[] {});
return (typeDesc);
} catch (final Exception e) {
throw new Exception("Unable to get Axis TypeDesc for "
+ objClass.getName(), e);
}
}
I have fixed it, I will leave this here if anyone else needs to use it
Have fun.
I have fixed it, I will leave this here if anyone else needs to use it. See the updated version.
Thank you

Why can't I parse a javamail attachment using toString?

It seems to me the snippet below should work, but "mp.getBodyPart(1).getContent().toString()" returns
com.sun.mail.util.BASE64DecoderStream#44b07df8
instead of the contents of the attachment.
public class GMailParser {
public String getParsedMessage(Message message) throws Exception {
try {
Multipart mp = (Multipart) message.getContent();
String s = mp.getBodyPart(1).getContent().toString();
if (s.contains("pattern 1")) {
return "return 1";
} else if (s.contains("pattern 2")) {
return "return 2";
}
...
It simply means that the BASE64DecoderStream class does not provide a custom toString definition. The default toString definition is to display the class name + '#' + Hash Code, which is what you see.
To get the "content" of the Stream you need to use the read() method.
This parses BASE64DecoderStream attachments exactly as needed.
private String getParsedAttachment(BodyPart bp) throws Exception {
InputStream is = null;
ByteArrayOutputStream os = null;
try {
is = bp.getInputStream();
os = new ByteArrayOutputStream(256);
int c = 0;
while ((c = is.read()) != -1) {
os.write(c);
}
String s = os.toString();
if (s.contains("pattern 1")) {
return "return 1";
} else if (s.contains("pattern 2")) {
return "return 2";
}
...

Categories