View Javadoc
1   /*
2    * Copyright 2009, openv4j.sf.net, and individual contributors as indicated
3    * by the @authors tag. See the copyright.txt in the distribution for a
4    * full listing of individual contributors.
5    *
6    * This is free software; you can redistribute it and/or modify it
7    * under the terms of the GNU General Public License as
8    * published by the Free Software Foundation; either version 3 of
9    * the License, or (at your option) any later version.
10   *
11   * This software is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14   * Lesser General Public License for more details.
15   *
16   * You should have received a copy of the GNU Lesser General Public
17   * License along with this software; if not, write to the Free
18   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
19   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
20   *
21   * $Id: $
22   *
23   * @author arnep
24   */
25  package net.sf.openv4j.protocolhandlers;
26  
27  import java.io.BufferedReader;
28  import java.io.IOException;
29  import java.io.InputStream;
30  import java.io.InputStreamReader;
31  
32  import org.slf4j.Logger;
33  import org.slf4j.LoggerFactory;
34  
35  import net.sf.openv4j.DataPoint;
36  
37  /*
38   * To change this template, choose Tools | Templates
39   * and open the template in the editor.
40   */
41  /**
42   * DOCUMENT ME!
43   *
44   * @author aploese
45   */
46  public abstract class DataContainer extends MemoryImage {
47      private static Logger log = LoggerFactory.getLogger(DataContainer.class);
48  
49      /**
50       * DOCUMENT ME!
51       *
52       * @param startAddress DOCUMENT ME!
53       * @param length DOCUMENT ME!
54       */
55      public abstract void addToDataContainer(int startAddress, int length);
56  
57      /**
58       * DOCUMENT ME!
59       *
60       * @param startAddress DOCUMENT ME!
61       * @param data DOCUMENT ME!
62       */
63  
64      //TODO What happend if 1 param is given?
65      public abstract void addToDataContainer(int startAddress, int[] data);
66  
67      /**
68       * DOCUMENT ME!
69       *
70       * @param dataPoint DOCUMENT ME!
71       * @param data DOCUMENT ME!
72       *
73       * @throws IllegalArgumentException DOCUMENT ME!
74       */
75      public void addToDataContainer(DataPoint dataPoint, int[] data) {
76          if (dataPoint.getLength() != data.length) {
77              throw new IllegalArgumentException(String.format("Lenght of datapoint %s (%d) must match lenght of data (%d)", dataPoint.getName(), dataPoint.getLength(), data.length));
78          }
79  
80          addToDataContainer(dataPoint.getAddr(), data);
81      }
82  
83      /**
84       * DOCUMENT ME!
85       *
86       * @param i DOCUMENT ME!
87       *
88       * @return DOCUMENT ME!
89       */
90      public abstract DataBlock getDataBlock(int i);
91  
92      /**
93       * DOCUMENT ME!
94       *
95       * @return DOCUMENT ME!
96       */
97      public abstract int getDataBlockCount();
98  
99      /**
100      * DOCUMENT ME!
101      *
102      * @param line DOCUMENT ME!
103      */
104     public void addMemoryImageLine(String line) {
105         String[] splitted = line.split(" ");
106         int address = -1;
107 
108         for (int i = 0; i < splitted.length; i++) {
109             if (i == 0) {
110                 address = Integer.parseInt(splitted[0], 16);
111                 addToDataContainer(address, 16);
112             } else {
113                 if ((splitted[i].length() != 0) && (!"|".equals(splitted[i]))) {
114                     setRawByte(address, (byte) Integer.parseInt(splitted[i], 16));
115                     address++;
116                 }
117             }
118         }
119     }
120 
121     /**
122      * DOCUMENT ME!
123      *
124      * @param dataPoint DOCUMENT ME!
125      */
126     public void addToDataContainer(DataPoint dataPoint) {
127         addToDataContainer(dataPoint.getAddr(), dataPoint.getLength());
128     }
129 
130     /**
131      * DOCUMENT ME!
132      *
133      * @param in DOCUMENT ME!
134      *
135      * @throws IOException DOCUMENT ME!
136      */
137     public void readFromStream(InputStream in) throws IOException {
138         BufferedReader br = new BufferedReader(new InputStreamReader(in));
139         String line;
140 
141         while ((line = br.readLine()) != null) {
142             addMemoryImageLine(line);
143         }
144     }
145 }