Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (C) 2010-2012 Agrosense info@agrosense.eu Licensed under the Eclipse Public License - v 1.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.eclipse.org/legal/epl-v10.html Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 package nl.cloudfarming.client.isobus.model;
 
 import java.io.*;
 import java.util.*;
Class that will construct Time Object from a binary file and the XML-file Constructor expects the fileName without extension and a file object which represents the map containing the files

Author(s):
Merijn Zengers & Gerben Feenstra
 
 public final class TimeLogReader {
 
     private XmlReader xmlReader;
     private BinaryReader binaryReader;
     private IsobusTaskData isobusTaskData;
 
     public TimeLogReader(String timeLogFileNameFileObject parentDirIsobusTaskData isobusTaskDatathrows ParserConfigurationExceptionSAXExceptionFileNotFoundExceptionIOException {
         this. = isobusTaskData;
 
         FileObject timeLogXml = getTimeLogXml(timeLogFileNameparentDir);
         FileObject timeLogBin = getTimeLogBin(timeLogFileNameparentDir);
 
          = new BinaryReader(timeLogBin.getInputStream());
          = new XmlReader(timeLogXml.getInputStream());
     }

    
Get the time elements from the binary file

Returns:
a list of times constructed from the XML and the binary file combined
Throws:
java.io.IOException when the binary or the XML file cannot be read
 
     List<IsobusTimegetTimes() throws IOException {
         ArrayList<IsobusTimetimes = new ArrayList<>();
         if ( != null &&  != null) {
             while (.available()) {
                 IsobusTime time = new IsobusTime();
                 Map<TimeFieldEnumStringtimeMap = .getTimeElement();
 
                 // Get 01-01-1980
                 Calendar cal = getIsobusEpoch();
 
                 for (TimeFieldEnum timeFieldEnum : timeMap.keySet()) {
 
                     // Empty attributes will be read from binary
                     switch (timeFieldEnum) {
                         case :
                             if ("".equals(timeMap.get(timeFieldEnum))) {
                                 cal.add(., Long.valueOf(.read(timeFieldEnum)).intValue());
                             }
                             // else -> Can starttime be specified in XML?
                             break;
                         case :
                             if ("".equals(timeMap.get(timeFieldEnum))) {
                                 cal.add(., Long.valueOf(.read(timeFieldEnum)).intValue());
                             }
                             // else -> Can startdate be specified in XML?
                             break;
                         case :
                             if ("".equals(timeMap.get(timeFieldEnum))) {
                                 int timeType = Long.valueOf(.read(timeFieldEnum)).intValue();
                                 // TimeTypeEnum starts at 1 instead of 0, so do a -1
                                 time.setTimeType(TimeTypeEnum.values()[timeType-1]);
                                 //time.setTimeType(getTimeType(Long.valueOf(binaryReader.read(timeFieldEnum)).intValue()));
                             } else {
                                 int timeType = Integer.parseInt(timeMap.get(timeFieldEnum));  
                                 // TimeTypeEnum starts at 1 instead of 0, so do a -1
                                 time.setTimeType(TimeTypeEnum.values()[timeType-1]);
                             }
                     }
                }
                time.setTimeStart(cal.getTime());
                times.add(time);
                //Position
                time.setPTN(getPosition(.getPositionElement()));
                
                //DLV
                time.getDLV().addAll(getDataLogValues(.getDataLogValues()));
            }
        }
        return times;
    }

    
Gets the position from binary file based on supplied Map of PositionFieldEnum and String

Parameters:
positionMap the position map that contains the PositionFieldEnum and a string containing the value for this PositionFieldEnum
Returns:
the position read from the binary file
Throws:
java.io.IOException when the binary file can not be read
    private Position getPosition(Map<PositionFieldEnumStringpositionMapthrows IOException {
        Position position = new Position();
        for (PositionFieldEnum positionFieldEnum : positionMap.keySet()) {
            switch (positionFieldEnum) {
                case :
                    position.setPositionNorth(BigDecimal.valueOf(.read(positionFieldEnum) / 10000000d));
                    break;
                case :
                    position.setPositionEast(BigDecimal.valueOf(.read(positionFieldEnum) / 10000000d));
                    break;
                case :
                    position.setPositionUp(Long.valueOf(.read(positionFieldEnum)).intValue());
                    break;
                case :
//                    int positionStatus = (int) Long.valueOf(binaryReader.read(positionFieldEnum)).intValue();
//                    position.setPositionStatus(PositionStatusEnum.values()[positionStatus]);
                    position.setPositionStatus(getPositionStatus(Double.valueOf(.read(positionFieldEnum)).intValue()));
                    break;
                case :
                    position.setHDOP(new BigDecimal(.read(positionFieldEnum)));
                    break;
                case :
                    position.setPDOP(new BigDecimal(.read(positionFieldEnum)));
                    break;
                case :
                    position.setNumberOfSatellites(Long.valueOf(.read(positionFieldEnum)).shortValue());
                    break;
                case :
                    position.setGpsUtcTime(.read(positionFieldEnum));
                    break;
                case :
                    position.setGpsUtcDate(Long.valueOf(.read(positionFieldEnum)).intValue());
                    break;
            }
        }
        return position;
    }

    
Gets the DatalogValues from the binary file for the provided List of maps containing DataLogfieldEnums and strings

Parameters:
dataLogValueMaps the list of maps to get the DataLogValues for
Returns:
a list of DataLogValues read from the binary file
Throws:
java.io.IOException when binary file can not be read
    private List<DataLogValuegetDataLogValues(List<Map<DatalogValueFieldEnumString>> dataLogValueMapsthrows IOException {
        //DataLogValue
        List<DataLogValuedataLogValues = new ArrayList<>();
        int dataLogValueIndexXml = 0;
        int dataLogValueIndexBin = 0;
        int dataLogValueCount = 0;
        for (Map<DatalogValueFieldEnumStringdataLogValueMap : dataLogValueMaps) {
            DataLogValue dataLogValue = new DataLogValue();
            for (DatalogValueFieldEnum datalogFieldEnum : dataLogValueMap.keySet()) {
                switch (datalogFieldEnum) {
                    case :
                        String hexAsString = dataLogValueMap.get(datalogFieldEnum);
                        int hexAsInt = Integer.parseInt(hexAsString, 16);
                        byte[] bytes = BinaryUtil.intToByteArray(hexAsInt);
                        dataLogValue.setProcessDataDDI(bytes);
                        break;
                    case :
                        dataLogValue.setDeviceElementIdRef(findDeviceElementByID(dataLogValueMap.get(datalogFieldEnum)));
                        break;
                }
            }
            if (dataLogValueCount == 0) {
                dataLogValueCount = Long.valueOf(.read(.)).intValue();
            }
            if (dataLogValueIndexXml > dataLogValueIndexBin || dataLogValueIndexBin == 0) {
                dataLogValueIndexBin = Long.valueOf(.read(.)).intValue();
            }
            if (dataLogValueIndexXml == dataLogValueIndexBin) {
            }
            dataLogValueIndexXml++;
            dataLogValues.add(dataLogValue);
        }
        return dataLogValues;
    }

    
Helper method to find device by ID

Parameters:
id Id of the device
Returns:
Device with the specified ID
        for (IsobusDevice device : .getDVC()) {
            for (DeviceElement deviceElement : device.getDET()) {
                if (deviceElement.getId().equals(id)) {
                    return deviceElement;
                }
            }
        }
        return null;
    }

    
Class to read the header of a binary timeLog file (header is a name-equivalent XML-file)

Author(s):
Gerben Feenstra & Merijn Zengers
    private static class XmlReader {
        private Document xmlDoc = null;
        private Map<TimeFieldEnumStringtimeMap;
        private Map<PositionFieldEnumStringpositionMap;
        private Element timeLogRootElement;
        public XmlReader(InputStream xmlInputStreamthrows ParserConfigurationExceptionSAXExceptionFileNotFoundException {
            DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
            try {
                 = docBuilder.parse(xmlInputStream);
            } catch (IOException ex) {
                throw new FileNotFoundException(ex.getMessage());
            }
            // Get root element (TIM)
             = .getDocumentElement();
        }

        
Gets the Time element from XML Will construct a LinkedHashMap containing a TimeFieldEnum as key and the value from XML as value if the map is already constructed returns the already constructed map

Returns:
the constructed map
        Map<TimeFieldEnumStringgetTimeElement() {
            if ( == null) {
                // Keep map ordered by natural ordering
                 = new LinkedHashMap<>();
                for (TimeFieldEnum timeFieldEnum : TimeFieldEnum.values()) {
                    // Only attributes that ARE PRESENT are stored in the binary file belonging to the XML
                    // So we explicitly check for the existence of the attribute
                    if (.hasAttribute(timeFieldEnum.getAttributeName())) {
                        String value = .getAttribute(timeFieldEnum.getAttributeName());
                        // Empty value will be parsed from the binary file using the binary reader.
                        .put(timeFieldEnumvalue);
                    }
                }
            }
            return ;
        }

        
Gets the Position element from XML Will construct a Linked HashMap containing a PositionFieldEnum as key and the value from the XML as value if the Map is already constructed will return the already constructed map

Returns:
the constructed LinkedHashMap
            if ( == null) {
                 = new LinkedHashMap<>();
                // Get positions list
                NodeList position = .getElementsByTagName("PTN");
                Node positionNode = null;
                Element positionElement = null;
                // Retrieve the Position Element
                if (position.getLength() > 0) {
                    positionNode = position.item(0);
                    if (positionNode.getNodeType() == .) {
                        positionElement = (ElementpositionNode;
                    }
                }
                for (PositionFieldEnum fieldEnum : PositionFieldEnum.values()) {
                    if (positionElement.hasAttribute(fieldEnum.getAttributeName())) {
                        String value = positionElement.getAttribute(fieldEnum.getAttributeName());
                        .put(fieldEnumvalue);
                    }
                }
            }
            return ;
        }

        
Gets the data log values from the XML Will construct a Linked list containing maps with DatalogValueFieldEnum as key and the value from the XML as value If the linked list is already constructed the list is returned instantly

Returns:
The constructed linked list
            if ( == null) {
                 = new LinkedList<>();
                // Get datalogvalues list
                NodeList datalogNodes = .getElementsByTagName("DLV");
                Node datalogValueNode = null;
                Element datalogValue = null;
                for (int i = 0; i < datalogNodes.getLength(); i++) {
                    LinkedHashMap dataLogValueMap = new LinkedHashMap<>();
                    datalogValueNode = datalogNodes.item(i);
                    if (datalogValueNode.getNodeType() == .) {
                        datalogValue = (ElementdatalogValueNode;
                    }
                    for (DatalogValueFieldEnum dataLogEnum : DatalogValueFieldEnum.values()) {
                        if (datalogValue.hasAttribute(dataLogEnum.getAttributeName())) {
                            String value = datalogValue.getAttribute(dataLogEnum.getAttributeName());
                            dataLogValueMap.put(dataLogEnumvalue);
                        }
                    }
                    .add(dataLogValueMap);
                }
            }
            return ;
        }
    }

    
TODO: Refactor to something nicer. Method to get the binary file from the specified directory Will only accept files with the fileName provided to the constructor with a .bin or .BIN extension

Parameters:
dir the directory where the file should reside in
Returns:
the File if found else null
    private FileObject getTimeLogBin(final String fileNamefinal FileObject dirObjthrows IOException {
        FileObject result = null;
        String fileCap = fileName + ".BIN";
        result = dirObj.getFileObject(fileCap);
        if (result == null) {
            String fileNoCap = fileName + ".bin";
            result = dirObj.getFileObject(fileNoCap);
        }
        if (result == null) {
            throw new IOException("Binary Timelog " + result + " could not be found");
        }
        return result;
    }

    
TODO: Refactor to something nicer. Gets the XML file from the specified directory Will only accept files with the fileName provided to the constructor with a .xml or .XML extension

Parameters:
dir the directory to find the file in
Returns:
null if not found else the the File object
    private FileObject getTimeLogXml(final String fileNamefinal FileObject dirObjthrows IOException {
        FileObject result = null;
        String fileCap = fileName + ".XML";
        result = dirObj.getFileObject(fileCap);
        if (result == null) {
            String fileNoCap = fileName + ".xml";
            result = dirObj.getFileObject(fileNoCap);
        }
        if (result == null) {
            throw new IOException("XML Timelog " + result + " could not be found");
        }
        return result;
    }

    
TODO: Remove!

Deprecated:
Replace by PositionStatusEnum.values()[value] ??
    @Deprecated
    private PositionStatusEnum getPositionStatus(int value) {
        switch (value) {
            case 0:
                return .;
            case 1:
                return .;
            case 2:
                return .;
            case 3:
                return .;
            case 4:
                return .;
            case 5:
                return .;
            case 6:
                return .;
            case 7:
                return .;
            case 8:
                return .;
            case 9:
            case 10:
            case 11:
            case 12:
            case 13:
                return .;
            case 14:
                return .;
            case 15:
                return .;
        }
        return null;
    }

    
Creates a new Calendar object and set it on the first of januari 1980.

Returns:
A calendar object set to 01-01-1980
    private Calendar getIsobusEpoch() {
        //make Calendar and set it on the first of januari 1980
        Calendar cal = Calendar.getInstance();
        cal.set(., 1980);
        cal.set(., 0);
        cal.set(., 1);
        cal.set(., 0);
        cal.set(., 0);
        cal.set(., 0);
        cal.set(., 0);
        return cal;
    }
New to GrepCode? Check out our FAQ X