tracesrv/tracecompiler/src/com.nokia.tracecompiler/src/com/nokia/tracecompiler/document/StringDocument.java
changeset 56 aa2539c91954
parent 41 838cdffd57ce
equal deleted inserted replaced
54:a151135b0cf9 56:aa2539c91954
       
     1 /*
       
     2 * Copyright (c) 2007 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 * String-based document object
       
    17 *
       
    18 */
       
    19 package com.nokia.tracecompiler.document;
       
    20 
       
    21 import java.util.ArrayList;
       
    22 
       
    23 import com.nokia.tracecompiler.source.SourceParserException;
       
    24 import com.nokia.tracecompiler.utils.DocumentAdapter;
       
    25 
       
    26 /**
       
    27  * String-based document object
       
    28  * 
       
    29  */
       
    30 class StringDocument extends DocumentAdapter {
       
    31 
       
    32 	/**
       
    33 	 * The document data
       
    34 	 */
       
    35 	private String sourceData;
       
    36 
       
    37 	/**
       
    38 	 * Line information
       
    39 	 */
       
    40 	private ArrayList<Integer> lines = new ArrayList<Integer>();
       
    41 
       
    42 	/**
       
    43 	 * Constructor
       
    44 	 */
       
    45 	StringDocument() {
       
    46 	}
       
    47 
       
    48 	/**
       
    49 	 * Constructor
       
    50 	 * 
       
    51 	 * @param data
       
    52 	 *            the document data
       
    53 	 */
       
    54 	StringDocument(String data) {
       
    55 		setSourceData(data);
       
    56 	}
       
    57 
       
    58 	/**
       
    59 	 * Sets the source data
       
    60 	 * 
       
    61 	 * @param data
       
    62 	 *            the source data
       
    63 	 */
       
    64 	void setSourceData(String data) {
       
    65 		this.sourceData = data;
       
    66 		if (data.length() > 0) {
       
    67 			lines.add(0);
       
    68 		}
       
    69 		for (int i = 0; i < data.length(); i++) {
       
    70 			if (data.charAt(i) == '\n') {
       
    71 				lines.add(i + 1);
       
    72 			}
       
    73 		}
       
    74 	}
       
    75 
       
    76 	/*
       
    77 	 * (non-Javadoc)
       
    78 	 * 
       
    79 	 * @see com.nokia.tracecompiler.utils.DocumentAdapter#get(int, int)
       
    80 	 */
       
    81 	@Override
       
    82 	public String get(int start, int length) throws SourceParserException {
       
    83 		return sourceData.substring(start, start + length);
       
    84 	}
       
    85 
       
    86 	/*
       
    87 	 * (non-Javadoc)
       
    88 	 * 
       
    89 	 * @see com.nokia.tracecompiler.utils.DocumentAdapter#getChar(int)
       
    90 	 */
       
    91 	@Override
       
    92 	public char getChar(int offset) throws SourceParserException {
       
    93 		return sourceData.charAt(offset);
       
    94 	}
       
    95 
       
    96 	/*
       
    97 	 * (non-Javadoc)
       
    98 	 * 
       
    99 	 * @see com.nokia.tracecompiler.utils.DocumentAdapter#getLength()
       
   100 	 */
       
   101 	@Override
       
   102 	public int getLength() {
       
   103 		return sourceData.length();
       
   104 	}
       
   105 
       
   106 	/*
       
   107 	 * (non-Javadoc)
       
   108 	 * 
       
   109 	 * @see com.nokia.tracecompiler.utils.DocumentAdapter#getLineOfOffset(int)
       
   110 	 */
       
   111 	@Override
       
   112 	public int getLineOfOffset(int offset) throws SourceParserException {
       
   113 		int retval = -1;
       
   114 		if (offset >= 0 && offset < sourceData.length()) {
       
   115 			for (int i = 0; i < lines.size(); i++) {
       
   116 				int lineStart = lines.get(i);
       
   117 				if (lineStart > offset) {
       
   118 					retval = i - 1;
       
   119 					i = lines.size();
       
   120 				}
       
   121 			}
       
   122 		}
       
   123 		return retval;
       
   124 	}
       
   125 
       
   126 }