feat: complete reverse engineering, standard Maven structure, detailed code annotations and IoT-M platform architecture
This commit is contained in:
1 parent
e8dc09fb3c
commit
cd6a4c70e7
3045 files changed
+566733
-1
No files matched your search
@@ -0,0 +1,226 @@
|
||||
package com.mitac.cell.io;
|
||||
|
||||
import com.mitac.cell.utils.McStringUtils;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
import javax.xml.transform.Transformer;
|
||||
import javax.xml.transform.TransformerFactory;
|
||||
import javax.xml.transform.dom.DOMSource;
|
||||
import javax.xml.transform.stream.StreamResult;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
import org.w3c.dom.NamedNodeMap;
|
||||
import org.w3c.dom.Node;
|
||||
import org.w3c.dom.NodeList;
|
||||
import org.xml.sax.SAXException;
|
||||
|
||||
public class Vector2Svg {
|
||||
private final InputStream source;
|
||||
|
||||
public Vector2Svg(String source) throws FileNotFoundException {
|
||||
this(new File(source));
|
||||
}
|
||||
|
||||
public Vector2Svg(File source) throws FileNotFoundException {
|
||||
this((InputStream)(new FileInputStream(source)));
|
||||
}
|
||||
|
||||
public Vector2Svg(byte[] data) {
|
||||
this((InputStream)(new ByteArrayInputStream(data)));
|
||||
}
|
||||
|
||||
public Vector2Svg(InputStream source) {
|
||||
this.source = source;
|
||||
}
|
||||
|
||||
public byte[] toSvgBytes() {
|
||||
try {
|
||||
AndroidVectorDrawable drawable = this.getDrawable();
|
||||
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
|
||||
Element svg = doc.createElement("svg");
|
||||
svg.setAttribute("viewBox", String.format("0 0 %.1f %.1f", drawable.width, drawable.height));
|
||||
|
||||
for(Group group : drawable.groups) {
|
||||
Element g = doc.createElement("g");
|
||||
|
||||
for(VectorPath path : group.paths) {
|
||||
Element child = doc.createElement("path");
|
||||
if (path.fillColor != null) {
|
||||
child.setAttribute("fill", path.fillColor);
|
||||
}
|
||||
|
||||
child.setAttribute("d", path.pathData);
|
||||
g.appendChild(child);
|
||||
}
|
||||
|
||||
svg.appendChild(g);
|
||||
}
|
||||
|
||||
for(VectorPath path : drawable.paths) {
|
||||
Element child = doc.createElement("path");
|
||||
if (path.fillColor != null) {
|
||||
child.setAttribute("fill", path.fillColor);
|
||||
}
|
||||
|
||||
child.setAttribute("d", path.pathData);
|
||||
svg.appendChild(child);
|
||||
}
|
||||
|
||||
doc.appendChild(svg);
|
||||
TransformerFactory transformerFactory = TransformerFactory.newInstance();
|
||||
Transformer transformer = transformerFactory.newTransformer();
|
||||
DOMSource source = new DOMSource(doc);
|
||||
ByteArrayOutputStream output = new ByteArrayOutputStream();
|
||||
StreamResult result = new StreamResult(output);
|
||||
transformer.setOutputProperty("omit-xml-declaration", "yes");
|
||||
transformer.setOutputProperty("indent", "yes");
|
||||
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
|
||||
transformer.transform(source, result);
|
||||
return output.toByteArray();
|
||||
} catch (Exception var10) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private AndroidVectorDrawable getDrawable() throws ParserConfigurationException, IOException, SAXException {
|
||||
Document xml = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(this.source);
|
||||
xml.getDocumentElement().normalize();
|
||||
Node vector = xml.getElementsByTagName("vector").item(0);
|
||||
NamedNodeMap attributes = vector.getAttributes();
|
||||
NodeList children = vector.getChildNodes();
|
||||
double width = (double)0.0F;
|
||||
double height = (double)0.0F;
|
||||
|
||||
for(int i = 0; i < attributes.getLength(); ++i) {
|
||||
if (attributes.item(i).getNodeName().equals("android:viewportHeight")) {
|
||||
height = this.getDouble(attributes.item(i).getNodeValue());
|
||||
} else if (attributes.item(i).getNodeName().equals("android:viewportWidth")) {
|
||||
width = this.getDouble(attributes.item(i).getNodeValue());
|
||||
}
|
||||
}
|
||||
|
||||
List<VectorPath> paths = new ArrayList();
|
||||
List<Group> groups = new ArrayList();
|
||||
|
||||
for(int i = 0; i < children.getLength(); ++i) {
|
||||
Node item = children.item(i);
|
||||
if (!item.getNodeName().equals("group")) {
|
||||
VectorPath path = this.getVectorPathFromNode(item);
|
||||
if (path != null) {
|
||||
paths.add(path);
|
||||
}
|
||||
} else {
|
||||
List<VectorPath> groupPaths = new ArrayList();
|
||||
|
||||
for(int j = 0; j < item.getChildNodes().getLength(); ++j) {
|
||||
VectorPath path = this.getVectorPathFromNode(item.getChildNodes().item(j));
|
||||
if (path != null) {
|
||||
groupPaths.add(path);
|
||||
}
|
||||
}
|
||||
|
||||
if (!groupPaths.isEmpty()) {
|
||||
groups.add(new Group(groupPaths));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return new AndroidVectorDrawable(paths, groups, width, height);
|
||||
}
|
||||
|
||||
private Double getDouble(String nodeValue) {
|
||||
if (nodeValue.startsWith("{")) {
|
||||
String[] token = nodeValue.split(":");
|
||||
Double d1 = Double.parseDouble(token[0].substring(1));
|
||||
Double d2 = Double.parseDouble(token[1].substring(0, token[1].length() - 1)) / (double)1024.0F / (double)1024.0F;
|
||||
return d2 / d1;
|
||||
} else {
|
||||
return Double.parseDouble(nodeValue);
|
||||
}
|
||||
}
|
||||
|
||||
private VectorPath getVectorPathFromNode(Node item) {
|
||||
if (item.getNodeName().equals("path")) {
|
||||
String pathData = null;
|
||||
String fillColor = null;
|
||||
|
||||
for(int j = 0; j < item.getAttributes().getLength(); ++j) {
|
||||
Node node = item.getAttributes().item(j);
|
||||
String name = node.getNodeName();
|
||||
String value = node.getNodeValue();
|
||||
if (name.equals("android:pathData")) {
|
||||
pathData = this.normalizedPathData(value);
|
||||
} else if (name.equals("android:fillColor")) {
|
||||
try {
|
||||
McStringUtils.toBytes(value);
|
||||
fillColor = "#" + value;
|
||||
} catch (Exception var9) {
|
||||
fillColor = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (pathData != null) {
|
||||
return new VectorPath(pathData, fillColor);
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private String normalizedPathData(String value) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
for(int i = 0; i < value.length(); ++i) {
|
||||
char ch = value.charAt(i);
|
||||
if (ch == '.' && (i - 1 < 0 || !Character.isDigit(value.charAt(i - 1)))) {
|
||||
sb.append('0');
|
||||
}
|
||||
|
||||
sb.append(ch);
|
||||
}
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private class VectorPath {
|
||||
private String pathData;
|
||||
private String fillColor;
|
||||
|
||||
private VectorPath(String pathData, String fillColor) {
|
||||
this.pathData = pathData;
|
||||
this.fillColor = fillColor;
|
||||
}
|
||||
}
|
||||
|
||||
private class Group {
|
||||
private final List paths;
|
||||
|
||||
public Group(List paths) {
|
||||
this.paths = paths;
|
||||
}
|
||||
}
|
||||
|
||||
private class AndroidVectorDrawable {
|
||||
private final List paths;
|
||||
private final List groups;
|
||||
private final double height;
|
||||
private final double width;
|
||||
|
||||
private AndroidVectorDrawable(List paths, List groups, double width, double height) {
|
||||
this.paths = paths;
|
||||
this.groups = groups;
|
||||
this.height = height;
|
||||
this.width = width;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user