android: AndrOBD library vendored (no submodule, no git link)
This commit is contained in:
+337
@@ -0,0 +1,337 @@
|
||||
/*
|
||||
* (C) Copyright 2015 by fr3ts0n <erwin.scheuch-heilig@gmx.at>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License as
|
||||
* published by the Free Software Foundation; either version 2 of
|
||||
* the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
|
||||
* MA 02111-1307 USA
|
||||
*/
|
||||
|
||||
package com.fr3ts0n.pvs.io;
|
||||
|
||||
import com.fr3ts0n.pvs.ProcessVar;
|
||||
import com.fr3ts0n.pvs.PvChangeEvent;
|
||||
import com.fr3ts0n.pvs.PvChangeListener;
|
||||
import com.fr3ts0n.pvs.PvList;
|
||||
|
||||
import org.xml.sax.Attributes;
|
||||
import org.xml.sax.helpers.DefaultHandler;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.Stack;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.xml.parsers.SAXParser;
|
||||
import javax.xml.parsers.SAXParserFactory;
|
||||
|
||||
/**
|
||||
* Content handler for ProcessVar XML-Streams
|
||||
*
|
||||
* @author $Author: erwin $
|
||||
*/
|
||||
public class PvXMLHandler
|
||||
extends DefaultHandler
|
||||
{
|
||||
|
||||
/** List of XML-Tags handled by this Handler */
|
||||
private static final String[] XML_TAGS =
|
||||
{
|
||||
PvXMLWriter.TAG_PVLIST,
|
||||
PvXMLWriter.TAG_PROCESSVAR,
|
||||
PvXMLWriter.TAG_PVATTRIBUTE
|
||||
};
|
||||
// numeric indices to above List
|
||||
/** ID of TAG for PV-List */
|
||||
static final int TAG_ID_PVLIST = 0;
|
||||
/** ID for Tag Processvar */
|
||||
private static final int TAG_ID_PROCESSVAR = 1;
|
||||
/** ID for tag Attribute */
|
||||
private static final int TAG_ID_PVATTRIBUTE = 2;
|
||||
/** Stack of recursive process variables */
|
||||
private final Stack<Object[]> pvStack = new Stack<Object[]>();
|
||||
/** The root Process variable to be parsed on */
|
||||
private com.fr3ts0n.pvs.PvList rootPv = new PvList();
|
||||
/** currently parsed process variable */
|
||||
private ProcessVar currPv = rootPv;
|
||||
/** currently parsed attribute */
|
||||
private Object currAttrib;
|
||||
/** currently parsed value for attribute */
|
||||
private Object currValue;
|
||||
/** list of process var change listeners */
|
||||
private final HashMap<PvChangeListener, Integer> PvChangeListeners = new HashMap<PvChangeListener, Integer>();
|
||||
/** the logger object */
|
||||
private static final Logger log = Logger.getLogger(PvXMLHandler.class.getPackage().getName());
|
||||
|
||||
/**
|
||||
* Creates a new instance of PvXMLContentHandler
|
||||
*
|
||||
* @throws org.xml.sax.SAXException Exception thrown at Instantiation of SAX-parser
|
||||
*/
|
||||
private PvXMLHandler()
|
||||
{
|
||||
rootPv.setKeyValue("root");
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new instance of PvXMLContentHandler
|
||||
*
|
||||
* @throws org.xml.sax.SAXException Exception thrown at Instantiation of SAX-parser
|
||||
*/
|
||||
private PvXMLHandler(PvList rootPv)
|
||||
{
|
||||
setRootPv(rootPv);
|
||||
}
|
||||
|
||||
/**
|
||||
* get the numeric ID of XML-Tag
|
||||
*
|
||||
* @param tagName Name of XML-Tag
|
||||
* @return numeric ID for XML-Tag or -1 if nott found
|
||||
*/
|
||||
private int getTagID(String tagName)
|
||||
{
|
||||
int result = -1;
|
||||
for (int i = 0; i < XML_TAGS.length; i++)
|
||||
{
|
||||
if (XML_TAGS[i].equals(tagName))
|
||||
{
|
||||
result = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
* handle incoming Text
|
||||
*
|
||||
* @param ch incoming text
|
||||
* @param start start position
|
||||
* @param length length of text
|
||||
*/
|
||||
public void characters(char[] ch, int start, int length)
|
||||
{
|
||||
String currStr = String.copyValueOf(ch, start, length).trim();
|
||||
|
||||
// If this is a valid String, then handle it as the new data
|
||||
if (currStr.length() > 0)
|
||||
{
|
||||
log.fine("Chars:'" + currStr + "'");
|
||||
currValue = currStr;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* handle start tag of a nw element
|
||||
*
|
||||
* @param namespaceURI element's namespace
|
||||
* @param localName element's local name
|
||||
* @param qName element's qualified name
|
||||
* @param atts attributes within element tag
|
||||
*/
|
||||
public void startElement(String namespaceURI, String localName, String qName, Attributes atts)
|
||||
{
|
||||
if (log.isLoggable(Level.FINE))
|
||||
{
|
||||
StringBuffer sb = new StringBuffer("StartElement:" + qName + ":");
|
||||
for (int i = 0; i < atts.getLength(); i++)
|
||||
{
|
||||
sb.append(atts.getQName(i)).append("=").append(atts.getValue(i)).append(",");
|
||||
}
|
||||
log.fine(sb.toString());
|
||||
}
|
||||
|
||||
int tagID = getTagID(qName);
|
||||
try
|
||||
{
|
||||
switch (tagID)
|
||||
{
|
||||
case TAG_ID_PROCESSVAR:
|
||||
if (currPv != null)
|
||||
{
|
||||
pvStack.push(new Object[]{currPv, currAttrib});
|
||||
}
|
||||
currPv = (ProcessVar) Class.forName(atts.getValue(PvXMLWriter.ATTR_TYPE)).newInstance();
|
||||
currPv.setKeyValue(atts.getValue(PvXMLWriter.ATTR_KEY));
|
||||
break;
|
||||
|
||||
case TAG_ID_PVATTRIBUTE:
|
||||
currAttrib = atts.getValue(PvXMLWriter.ATTR_NAME);
|
||||
break;
|
||||
}
|
||||
} catch (Exception e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* handle end tag of element
|
||||
*
|
||||
* @param namespaceURI element's namespace
|
||||
* @param localName element's local name
|
||||
* @param qName element's qualified name
|
||||
*/
|
||||
public void endElement(String namespaceURI, String localName, String qName)
|
||||
{
|
||||
log.fine("EndElement:" + qName);
|
||||
int tagID = getTagID(qName);
|
||||
switch (tagID)
|
||||
{
|
||||
case TAG_ID_PROCESSVAR:
|
||||
log.fine("PV:" + currPv.toString());
|
||||
firePvChanged(new PvChangeEvent(this, currPv.getKeyValue(), currPv, PvChangeEvent.PV_CONFIRMED));
|
||||
currValue = currPv;
|
||||
// If we have PV's on stack, the new PV is a recursive attribute
|
||||
if (!pvStack.empty())
|
||||
{
|
||||
Object[] stackElems = pvStack.pop();
|
||||
currPv = (ProcessVar) stackElems[0];
|
||||
currAttrib = stackElems[1];
|
||||
} else
|
||||
{
|
||||
log.severe("NO more PV's on Stack");
|
||||
}
|
||||
if (currPv == rootPv)
|
||||
{
|
||||
currAttrib = ((ProcessVar) currValue).getKeyValue();
|
||||
}
|
||||
|
||||
currPv.put(currAttrib, currValue);
|
||||
break;
|
||||
|
||||
case TAG_ID_PVATTRIBUTE:
|
||||
// if currently parsed PV is null, then we just finished a recursive one
|
||||
if (currPv != null)
|
||||
{
|
||||
// This is a plain attribute
|
||||
currPv.put(currAttrib, currValue);
|
||||
}
|
||||
currValue = null;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handling for list of PvChangeListeners
|
||||
*/
|
||||
/**
|
||||
* remove listener for Pv changes
|
||||
*
|
||||
* @param l Listener to be removed
|
||||
*/
|
||||
public synchronized void removePvChangeListener(PvChangeListener l)
|
||||
{
|
||||
log.fine("-PvListener:" + String.valueOf(this) + "->" + String.valueOf(l));
|
||||
|
||||
PvChangeListeners.remove(l);
|
||||
}
|
||||
|
||||
/**
|
||||
* add listener for Pv changes with specified change events
|
||||
*
|
||||
* @param l Listener to be added
|
||||
* @param eventMask events (Bitmask) which this listener wants to listen on
|
||||
*/
|
||||
private synchronized void addPvChangeListener(PvChangeListener l, int eventMask)
|
||||
{
|
||||
Object oldListener = PvChangeListeners.get(l);
|
||||
PvChangeListeners.put(l, Integer.valueOf(eventMask));
|
||||
|
||||
if (oldListener == null)
|
||||
{
|
||||
log.fine("+PvListener:" + String.valueOf(this) + "->" + String.valueOf(l));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* add listener for Pv changes
|
||||
*
|
||||
* @param l Listener to be added
|
||||
*/
|
||||
public void addPvChangeListener(PvChangeListener l)
|
||||
{
|
||||
addPvChangeListener(l, PvChangeEvent.PV_ALLEVENTS);
|
||||
}
|
||||
|
||||
/**
|
||||
* fire a Pv Change event
|
||||
*
|
||||
* @param e Event for Process variable change
|
||||
*/
|
||||
@SuppressWarnings("rawtypes")
|
||||
private void firePvChanged(PvChangeEvent e)
|
||||
{
|
||||
log.fine("PvChange:" + e.toString());
|
||||
|
||||
Integer evtMask;
|
||||
Map.Entry curr;
|
||||
|
||||
Set entries = PvChangeListeners.entrySet();
|
||||
Iterator it = entries.iterator();
|
||||
|
||||
while (it.hasNext())
|
||||
{
|
||||
curr = (Map.Entry) it.next();
|
||||
|
||||
if (curr.getKey() != null && curr.getKey() != this)
|
||||
{
|
||||
// check if listener wants to be notified by this event
|
||||
evtMask = (Integer) curr.getValue();
|
||||
|
||||
if ((evtMask.intValue() & e.getType()) != 0)
|
||||
{
|
||||
log.fine("Notify:" + curr);
|
||||
((PvChangeListener) curr.getKey()).pvChanged(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public PvList getRootPv()
|
||||
{
|
||||
return rootPv;
|
||||
}
|
||||
|
||||
private void setRootPv(PvList mainPV)
|
||||
{
|
||||
this.rootPv = mainPV;
|
||||
this.currPv = mainPV;
|
||||
}
|
||||
|
||||
/**
|
||||
* main routine for testing class implementation
|
||||
*
|
||||
* @param argv command line arguments
|
||||
*/
|
||||
public static void main(String[] argv)
|
||||
{
|
||||
try
|
||||
{
|
||||
// Create a new Parser
|
||||
PvXMLHandler handler = new PvXMLHandler();
|
||||
// Use the default (non-validating) parser
|
||||
SAXParserFactory factory = SAXParserFactory.newInstance();
|
||||
// Parse the input
|
||||
SAXParser saxParser = factory.newSAXParser();
|
||||
saxParser.parse(new FileInputStream(argv[0]), handler);
|
||||
} catch (Exception e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
+241
@@ -0,0 +1,241 @@
|
||||
/*
|
||||
* (C) Copyright 2015 by fr3ts0n <erwin.scheuch-heilig@gmx.at>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License as
|
||||
* published by the Free Software Foundation; either version 2 of
|
||||
* the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
|
||||
* MA 02111-1307 USA
|
||||
*/
|
||||
|
||||
package com.fr3ts0n.pvs.io;
|
||||
|
||||
|
||||
import com.fr3ts0n.pvs.ProcessVar;
|
||||
import com.fr3ts0n.pvs.PvChangeEvent;
|
||||
import com.fr3ts0n.pvs.PvChangeListener;
|
||||
|
||||
import org.xml.sax.helpers.AttributesImpl;
|
||||
|
||||
import java.io.OutputStream;
|
||||
import java.util.Iterator;
|
||||
|
||||
import javax.xml.transform.OutputKeys;
|
||||
import javax.xml.transform.Result;
|
||||
import javax.xml.transform.Transformer;
|
||||
import javax.xml.transform.sax.SAXTransformerFactory;
|
||||
import javax.xml.transform.sax.TransformerHandler;
|
||||
import javax.xml.transform.stream.StreamResult;
|
||||
|
||||
/**
|
||||
* XML writer to write Process Variables into an XML-Stream
|
||||
*
|
||||
* @author $Author: erwin $
|
||||
*/
|
||||
public class PvXMLWriter
|
||||
implements PvChangeListener
|
||||
{
|
||||
|
||||
private Result xFormResult;
|
||||
private SAXTransformerFactory factory;
|
||||
private TransformerHandler handler;
|
||||
private Transformer serializer;
|
||||
private boolean documentStarted = false;
|
||||
/** XML Tag-Names */
|
||||
public static final String TAG_PVLIST = "PV-LIST";
|
||||
public static final String TAG_PROCESSVAR = "PV";
|
||||
public static final String TAG_PVATTRIBUTE = "AT";
|
||||
/** XML attribute values */
|
||||
public static final String ATTR_TYPE = "type";
|
||||
public static final String ATTR_KEY = "key";
|
||||
public static final String ATTR_NAME = "name";
|
||||
|
||||
/**
|
||||
* Creates a new instance of PvXMLWriter
|
||||
*/
|
||||
private PvXMLWriter()
|
||||
{
|
||||
try
|
||||
{
|
||||
factory = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
|
||||
handler = factory.newTransformerHandler();
|
||||
serializer = handler.getTransformer();
|
||||
serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
|
||||
serializer.setOutputProperty(OutputKeys.INDENT, "yes");
|
||||
serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
|
||||
} catch (Exception e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new instance of PvXMLWriter
|
||||
*/
|
||||
public PvXMLWriter(OutputStream oStream)
|
||||
{
|
||||
this();
|
||||
setOutputStream(oStream);
|
||||
}
|
||||
|
||||
/**
|
||||
* set a stream writer for the handler
|
||||
*/
|
||||
private void setOutputStream(OutputStream oStream)
|
||||
{
|
||||
try
|
||||
{
|
||||
xFormResult = new StreamResult(oStream);
|
||||
handler.setResult(xFormResult);
|
||||
} catch (Exception e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* initialize document writing
|
||||
*/
|
||||
private void startDocument()
|
||||
{
|
||||
AttributesImpl attrs = new AttributesImpl();
|
||||
try
|
||||
{
|
||||
handler.startDocument();
|
||||
handler.startElement("", TAG_PVLIST, TAG_PVLIST, attrs);
|
||||
documentStarted = true;
|
||||
} catch (Throwable e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* finalize Document writing
|
||||
*/
|
||||
private void endDocument()
|
||||
{
|
||||
try
|
||||
{
|
||||
handler.endElement("", TAG_PVLIST, TAG_PVLIST);
|
||||
handler.endDocument();
|
||||
documentStarted = false;
|
||||
} catch (Throwable e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* write a process variable as XML to an output stream
|
||||
*
|
||||
* @param pv processVar to write
|
||||
* @param recursiveDepth Depth of recursive PV writing
|
||||
* @param completeDoc if TRUE, XML document will be initialized before,
|
||||
* and finalized after writing this PV
|
||||
*/
|
||||
@SuppressWarnings("rawtypes")
|
||||
private void writePv(ProcessVar pv, int recursiveDepth, boolean completeDoc)
|
||||
{
|
||||
Object currKey;
|
||||
Object currVal;
|
||||
|
||||
// Initialize XML-Document
|
||||
if (completeDoc || !documentStarted)
|
||||
{
|
||||
startDocument();
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
AttributesImpl attrs = new AttributesImpl();
|
||||
attrs.addAttribute("", ATTR_TYPE, ATTR_TYPE, "CDATA", pv.getClass().getName());
|
||||
attrs.addAttribute("", ATTR_KEY, ATTR_KEY, "CDATA", String.valueOf(pv.getKeyValue()));
|
||||
handler.startElement("", TAG_PROCESSVAR, TAG_PROCESSVAR, attrs);
|
||||
|
||||
if (recursiveDepth > 0)// Loop through all PV-Attributes
|
||||
{
|
||||
Iterator it = pv.keySet().iterator();
|
||||
while (it.hasNext())
|
||||
{
|
||||
currKey = it.next();
|
||||
currVal = pv.get(currKey);
|
||||
|
||||
attrs.clear();
|
||||
attrs.addAttribute("", ATTR_NAME, ATTR_NAME, "CDADTA", currKey.toString());
|
||||
handler.startElement("", TAG_PVATTRIBUTE, TAG_PVATTRIBUTE, attrs);
|
||||
|
||||
if (currVal != null)
|
||||
{
|
||||
// If current value is a PV itself ...
|
||||
if (currVal instanceof ProcessVar)
|
||||
{
|
||||
// write recursively
|
||||
writePv((ProcessVar) currVal, recursiveDepth - 1, false);
|
||||
} else
|
||||
{
|
||||
// Otherwise write PV-Data as attribute
|
||||
String currStr = currVal.toString();
|
||||
handler.characters(currStr.toCharArray(), 0, currStr.length());
|
||||
}
|
||||
}
|
||||
// finish attribute XML-element
|
||||
handler.endElement("", TAG_PVATTRIBUTE, TAG_PVATTRIBUTE);
|
||||
}
|
||||
}
|
||||
// finish PV XNL-element
|
||||
handler.endElement("", TAG_PROCESSVAR, TAG_PROCESSVAR);
|
||||
} catch (Throwable e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
// Finalize XML document
|
||||
if (completeDoc)
|
||||
{
|
||||
endDocument();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* write a process variable as XML to an output stream
|
||||
*
|
||||
* @param pv processVar to write
|
||||
* @param recursiveDepth Depth of recursive PV writing
|
||||
*/
|
||||
public void writePv(ProcessVar pv, int recursiveDepth)
|
||||
{
|
||||
writePv(pv, recursiveDepth, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Standard writing of PV
|
||||
* (uses recursive depth=100, NON complete Doc
|
||||
*
|
||||
* @param pv processVar to write
|
||||
*/
|
||||
private void writePv(ProcessVar pv)
|
||||
{
|
||||
writePv(pv, 100, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of PvChangeListener
|
||||
*/
|
||||
public void pvChanged(PvChangeEvent event)
|
||||
{
|
||||
// Test für XML-Ausgabe
|
||||
if (event.getValue() instanceof ProcessVar)
|
||||
{
|
||||
writePv((ProcessVar) event.getValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
||||
|
||||
<html>
|
||||
<body>
|
||||
Input / Output and serialisation for Process variables
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user