crashanalysis/crashanalyser/com.nokia.s60tools.crashanalyser/src/com/nokia/s60tools/crashanalyser/containers/Message.java
changeset 0 5ad7ad99af01
child 4 615035072f7e
equal deleted inserted replaced
-1:000000000000 0:5ad7ad99af01
       
     1 /*
       
     2 * Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 
       
    18 package com.nokia.s60tools.crashanalyser.containers;
       
    19 
       
    20 import java.io.BufferedWriter;
       
    21 import java.io.IOException;
       
    22 
       
    23 import org.w3c.dom.Element;
       
    24 import org.w3c.dom.NodeList;
       
    25 import org.w3c.dom.Node;
       
    26 
       
    27 import com.nokia.s60tools.crashanalyser.model.XmlUtils;
       
    28 
       
    29 /**
       
    30  * Message class represents one xml message. A message can 
       
    31  * be an error message, warning message or a comment.
       
    32  *
       
    33  */
       
    34 public final class Message {
       
    35 	
       
    36 	// XML tags
       
    37 	public static final String TAG_ID = "id";
       
    38 	public static final String TAG_TYPE = "type";
       
    39 	public static final String TAG_TITLE = "title";
       
    40 	public static final String TAG_LINE = "line";
       
    41 
       
    42 	public enum MessageTypes {ERROR, WARNING, COMMENT};
       
    43 	
       
    44 	// message data
       
    45 	private final int messageId;
       
    46 	private final String messageTitle;
       
    47 	private final String messageMessage;
       
    48 	private final MessageTypes messageType;
       
    49 	
       
    50 	/**
       
    51 	 * Constructor
       
    52 	 * @param id message id
       
    53 	 * @param title message title
       
    54 	 * @param message message text
       
    55 	 * @param type message type
       
    56 	 */
       
    57 	private Message(int id, String title, String message, MessageTypes type) {
       
    58 		messageId = id;
       
    59 		messageTitle = title;
       
    60 		messageMessage = message;
       
    61 		messageType = type;
       
    62 	}
       
    63 	
       
    64 	/**
       
    65 	 * Creates a new message with given parameters
       
    66 	 * @param id message id
       
    67 	 * @param title message title
       
    68 	 * @param message message text
       
    69 	 * @return created message
       
    70 	 */
       
    71 	public static Message newMessage(int id, String title, String message, MessageTypes type) {
       
    72 		return new Message(id, title, message, type);
       
    73 	}
       
    74 	
       
    75 	/**
       
    76 	 * Writes message into given buffer (e.g. into file)
       
    77 	 * @param out buffer to write to
       
    78 	 * @throws IOException
       
    79 	 */
       
    80 	public void writeTo(BufferedWriter out) throws IOException {
       
    81 		if (MessageTypes.WARNING.equals(messageType)) {
       
    82 			writeMessage(out, "WARNING:");
       
    83 		} else if (MessageTypes.ERROR.equals(messageType)) {
       
    84 			writeMessage(out, "ERROR:");
       
    85 		}
       
    86 	}
       
    87 	
       
    88 	void writeMessage(BufferedWriter out, String type) throws IOException {
       
    89 		writeLine(out,"");
       
    90 		writeLine(out, type);
       
    91 		writeLine(out, "--------");
       
    92 		writeLine(out, messageTitle);
       
    93 		writeLine(out, messageMessage);		
       
    94 	}
       
    95 	
       
    96 	void writeLine(BufferedWriter out, String line) throws IOException {
       
    97 		out.write(line);
       
    98 		out.newLine();
       
    99 	}
       
   100 	
       
   101 	/**
       
   102 	 * Reads and creates a message from message xml element
       
   103 	 * @param elementMessage
       
   104 	 * @return created message or null
       
   105 	 */
       
   106 	public static Message read(Element elementMessage) {
       
   107 		try {
       
   108 			// read code segment id
       
   109 			String messageId = XmlUtils.getTextValue(elementMessage, TAG_ID);
       
   110 			if (messageId == null)
       
   111 				return null;
       
   112 			
       
   113 			// convert code segment id to integer
       
   114 			int id;
       
   115 			try {
       
   116 				id = Integer.parseInt(messageId);
       
   117 			} catch (Exception e) {
       
   118 				return null;
       
   119 			}
       
   120 			
       
   121 			// read message type
       
   122 			String type = XmlUtils.getTextValue(elementMessage, TAG_TYPE);
       
   123 			if (type == null)
       
   124 				return null;
       
   125 			MessageTypes messageType = getMessageType(type);
       
   126 			
       
   127 			// get title and msg nodes
       
   128 			NodeList children = elementMessage.getChildNodes();
       
   129 			if (children == null || children.getLength() < 1)
       
   130 				return null;
       
   131 			
       
   132 			// read title and message
       
   133 			String title = "";
       
   134 			String message = "";
       
   135 			for (int i = 0; i < children.getLength(); i++) {
       
   136 				Node child = children.item(i);
       
   137 				if (TAG_TITLE.equals(child.getNodeName())) {
       
   138 					title = XmlUtils.getNodeValue(child);
       
   139 				} else if (TAG_LINE.equals(child.getNodeName())) {
       
   140 					if (!"".equals(message))
       
   141 						message += System.getProperty("line.separator");
       
   142 					message += XmlUtils.getNodeValue(child);
       
   143 				}
       
   144 			}
       
   145 			
       
   146 			if ("".equals(title) && "".equals(message))
       
   147 				return null;
       
   148 			
       
   149 			return new Message(id, title, message, messageType);
       
   150 		} catch (Exception e) {
       
   151 			return null;
       
   152 		}
       
   153 	}
       
   154 	
       
   155 	/**
       
   156 	 * Gets the message type from given string
       
   157 	 * @param type e.g. "Error" or "Warning"
       
   158 	 * @return message type
       
   159 	 */
       
   160 	static MessageTypes getMessageType(String type) {
       
   161 		if ("Error".equals(type))
       
   162 			return MessageTypes.ERROR;
       
   163 		else if ("Warning".equals(type))
       
   164 			return MessageTypes.WARNING;
       
   165 		else
       
   166 			return MessageTypes.COMMENT;
       
   167 	}
       
   168 	
       
   169 	public int getId() {
       
   170 		return messageId;
       
   171 	}
       
   172 
       
   173 	public MessageTypes getMessageType() {
       
   174 		return messageType;
       
   175 	}
       
   176 	
       
   177 	public String getTitle() {
       
   178 		return messageTitle;
       
   179 	}
       
   180 	
       
   181 	public String getMessage() {
       
   182 		return messageMessage;
       
   183 	}
       
   184 }