crashanalysis/crashanalyser/com.nokia.s60tools.crashanalyser/src/com/nokia/s60tools/crashanalyser/containers/RegisterDetails.java
changeset 0 5ad7ad99af01
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 org.w3c.dom.Element;
       
    21 import org.w3c.dom.NodeList;
       
    22 import org.w3c.dom.Node;
       
    23 import org.w3c.dom.NamedNodeMap;
       
    24 import java.util.*;
       
    25 import com.nokia.s60tools.crashanalyser.model.XmlUtils;
       
    26 
       
    27 /**
       
    28  * 
       
    29  *
       
    30  */
       
    31 public final class RegisterDetails {
       
    32 	
       
    33 	// XML tags
       
    34 	final static String TAG_DESCRIPTION = "description";
       
    35 	final static String TAG_ENDIAN = "endian";
       
    36 	final static String TAG_BIT_RANGE = "bit_range";
       
    37 	final static String TAG_BIT = "bit";
       
    38 	final static String ATTRIBUTE_BIT0 = "bit0";
       
    39 	
       
    40 	// Data
       
    41 	private final String registerDescription;
       
    42 	private final List<RegisterBit> registerBits;
       
    43 	private final boolean bitsStartFromRight;
       
    44 	
       
    45 	/**
       
    46 	 * Constructor
       
    47 	 * @param description register description
       
    48 	 * @param bits list of bits
       
    49 	 * @param startFromRight bits start from right (true) or from left (false), endianess issue
       
    50 	 */
       
    51 	private RegisterDetails(String description, List<RegisterBit> bits, boolean startFromRight) {
       
    52 		registerDescription = description;
       
    53 		registerBits = bits;
       
    54 		bitsStartFromRight = startFromRight;
       
    55 	}
       
    56 	
       
    57 	public String getDescription() {
       
    58 		return registerDescription;
       
    59 	}
       
    60 	
       
    61 	public boolean bitsStartFromRight() {
       
    62 		return bitsStartFromRight;
       
    63 	}
       
    64 	
       
    65 	public RegisterBit getBit(int index) {
       
    66 		RegisterBit bit = null;
       
    67 		for (int i = 0; i < registerBits.size(); i++) {
       
    68 			if (registerBits.get(i).getIndex() == index) {
       
    69 				bit = registerBits.get(i);
       
    70 				break;
       
    71 			}
       
    72 		}
       
    73 		return bit;
       
    74 	}
       
    75 	
       
    76 	/**
       
    77 	 * Reads and creates a register details from vientry xml element
       
    78 	 * @param elementViEntry
       
    79 	 * @return created register details
       
    80 	 */
       
    81 	public static RegisterDetails read(Element elementViEntry) {
       
    82 		String description = XmlUtils.getTextValue(elementViEntry, TAG_DESCRIPTION);
       
    83 		if (description == null)
       
    84 			return null;
       
    85 		
       
    86 		boolean bitsStartFromRight = true;
       
    87 		NodeList endian = elementViEntry.getElementsByTagName(TAG_ENDIAN);
       
    88 		if (endian != null && endian.getLength() > 0) {
       
    89 			NamedNodeMap attributes = endian.item(0).getAttributes();
       
    90 			if (attributes != null && attributes.getLength() > 0) {
       
    91 				Node bit0 = attributes.getNamedItem(ATTRIBUTE_BIT0);
       
    92 				String bitStart = XmlUtils.getNodeValue(bit0);
       
    93 				if (bitStart != null && "left".equals(bitStart))
       
    94 					bitsStartFromRight = false;
       
    95 			}
       
    96 		}
       
    97 
       
    98 		List<RegisterBit> registerBits = new ArrayList<RegisterBit>();
       
    99 
       
   100 		// read bit ranges
       
   101 		NodeList bitRanges = elementViEntry.getElementsByTagName(TAG_BIT_RANGE);
       
   102 		if (bitRanges != null && bitRanges.getLength() > 0) {
       
   103 			for (int i = 0; i < bitRanges.getLength(); i++) {
       
   104 				List<RegisterBit> regBits = RegisterBit.readFromRange(bitRanges.item(i));
       
   105 				if (regBits == null || regBits.size() < 1)
       
   106 					return null;
       
   107 				registerBits.addAll(regBits);
       
   108 			}
       
   109 		}
       
   110 		
       
   111 		// read bits
       
   112 		NodeList bits = elementViEntry.getElementsByTagName(TAG_BIT);
       
   113 		if (bits != null && bits.getLength() > 0) {
       
   114 			for (int i = 0; i < bits.getLength(); i++) {
       
   115 				RegisterBit regBit = RegisterBit.readFromBit(bits.item(i));
       
   116 				if (regBit == null)
       
   117 					return null;
       
   118 				registerBits.add(regBit);
       
   119 			}
       
   120 		}
       
   121 
       
   122 		return new RegisterDetails(description, registerBits, bitsStartFromRight);
       
   123 	}
       
   124 }