Skip to content

Commit

Permalink
Version 1.5. Added C header file support to the Import function.
Browse files Browse the repository at this point in the history
- changed SingleDOM.java to call FileTools.getNewFileChooser()
- changed getImageFile() in Tools.java to call FileTools.joinPaths()
- changed PoolImportDialog to show header files (.h)
- added processHeaderFile() to PoolImporter.java
- added new CArrayReader class

NOTES:
- importing h files is useful in testing as it allows importing
  object pools that have been exported and then parsed into
  h files
- currently the system almost works but there is a slight
  problem with the various character encodings in the parser
  which is likely related to this (from the Expat XML parser
  homepage):

"... although Expat may accept input in various encodings, the
strings that it passes to the handlers are always encoded in UTF-8
or UTF-16 (depending on how Expat was compiled). Your application
is responsible for any translation of these strings into other
encodings."
  • Loading branch information
moehman committed Nov 11, 2019
1 parent 70668d5 commit c9724f2
Show file tree
Hide file tree
Showing 5 changed files with 249 additions and 13 deletions.
3 changes: 2 additions & 1 deletion src/multidom/SingleDOM.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.tree.TreePath;
import org.w3c.dom.Document;
import pooledit.FileTools;
import pooledit.Tools;
import pooledit.Utils;
import treemodel.XMLTreeModel;
Expand All @@ -41,7 +42,7 @@
*/
public class SingleDOM {

private JFileChooser fc = new JFileChooser();
private JFileChooser fc = FileTools.getNewFileChooser();
private final XMLTreeModel treeModel = new XMLTreeModel();

private final MultiDOM multidom;
Expand Down
11 changes: 7 additions & 4 deletions src/pooledit/Tools.java
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ public static void saveDocument(String name, Document doc)
}

/**
* Exports the document using the XML format specified in ISOAbLib.
* Exports the document using the XML format specified in ISOAgLib.
* <ul>
* <li> roles are converted back to attributes
* <li> start and end angles are converted to "double degrees" (divided by two)
Expand Down Expand Up @@ -462,7 +462,7 @@ public static void exportToXML3(PrintStream out, String fileName, Document doc)
}
}

// set output string lengths
// set input string lengths
NodeList inputstrings = clone.getElementsByTagName(INPUTSTRING);
for (int i = 0, n = inputstrings.getLength(); i < n; i++) {
Element inputstring = (Element) inputstrings.item(i);
Expand Down Expand Up @@ -893,14 +893,17 @@ private static String getStdBitmapPath(Document doc) {
return root.getAttribute(STD_BITMAP_PATH);
}

private static BufferedImage getImageFile(Element element, String attrib, String imagepath) throws IOException {
private static BufferedImage getImageFile(Element element, String attrib,
String imagepath) throws IOException
{
String s = element.getAttribute(attrib);
File f = null;
if (s.isEmpty()) {
return null;
}
try {
f = new File(imagepath + s);
String fullname = FileTools.joinPaths(imagepath, s);
f = new File(fullname);
return ImageIO.read(f);
}
catch (IOException ex) {
Expand Down
158 changes: 158 additions & 0 deletions src/poolimporter/CArrayReader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
/*
* Copyright (C) 2019 Automation technology laboratory,
* Helsinki University of Technology
*
* Visit automation.tkk.fi for information about the automation
* technology laboratory.
*
* 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 3
* 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 poolimporter;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
*
* @author moehman
*/
class CArrayReader implements ByteReader {
private final Scanner sc;
public CArrayReader(Scanner sc) {
this.sc = sc;
}
@Override
public int readId() {
return readWord();
}

@Override
public int readWord() {
return
(readByte()) |
(readByte() << 8);
}

@Override
public int readDWord() {
return
(readByte()) |
(readByte() << 8) |
(readByte() << 16) |
(readByte() << 24);
}

@Override
public float readFloat() {
return Float.intBitsToFloat(readDWord());
}

@Override
public int readColor() {
return readByte();
}

@Override
public int readType() {
return readByte();
}

@Override
public int readByte() {
final int val = Integer.parseInt(sc.next());
if (val > 0xFF)
throw new RuntimeException("byte too large: " + val);

return val;
}

@Override
public byte[] readByteArray(int len) {
byte[] b = new byte[len];
for (int i = 0; i < len; i++) {
b[i] = (byte) readByte();
}
return b;
}

@Override
public int readKeyCode() {
return readByte();
}

@Override
public List<Integer> readBytes(int nro) {
List<Integer> list = new ArrayList<>();
for (int i = 0; i < nro; i++) {
list.add(readByte());
}
return list;
}

@Override
public int readRef() {
return readWord();
}

@Override
public List<Integer> readRefs(int nro) {
List<Integer> list = new ArrayList<>();
for (int i = 0; i < nro; i++) {
list.add(readRef());
}
return list;
}

@Override
public List<RefXY> readRefXYs(int nro) {
List<RefXY> list = new ArrayList<>();
for (int i = 0; i < nro; i++) {
list.add(new RefXY(readRef(),
readWord(),
readWord()));
}
return list;
}

@Override
public List<PointXY> readPoints(int nro) {
List<PointXY> list = new ArrayList<>();
for (int i = 0; i < nro; i++) {
list.add(new PointXY(readWord(), readWord()));
}
return list;
}

@Override
public List<String> readLanguages(int nro) {
List<String> list = new ArrayList<>();
for (int i = 0; i < nro; i++) {
String val = readString(2);
list.add(val);
}
return list;
}

@Override
public String readString(int len) {
byte[] b = readByteArray(len);
return new String(b, Charset.forName("ISO-8859-1"));
}
}
2 changes: 1 addition & 1 deletion src/poolimporter/PoolImportDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public PoolImportDialog(Action openAction) {
jSpinner2.setValue(60);
jSpinner3.setValue(32);
INPUT_FC.setFileFilter(new FileNameExtensionFilter(
"Object pool file", "sav", "iop"));
"Object pool file", "sav", "iop", "h"));
BITMAP_FC.setDialogTitle("Choose directory");
BITMAP_FC.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
OUTPUT_FC.setFileFilter(new FileNameExtensionFilter("XML-file", "xml"));
Expand Down
88 changes: 81 additions & 7 deletions src/poolimporter/PoolImporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -2784,15 +2784,86 @@ public void processIOPFile(File file, Document doc) {
}
}

public void processHeaderFile(File file, Document doc) {
try {
BufferedReader input = // new BufferedReader(new FileReader(file));
new BufferedReader(new InputStreamReader(new FileInputStream(file), "8859_1"));

List<String> objs = new ArrayList<>();
Map<Integer, String> map1 = new HashMap<>();

List<VTObject> list = new ArrayList<>();
Map<Integer, String> map2 = new HashMap<>();
Set<String> set = new HashSet<>();

String line;
int mode = 0;
while ((line = input.readLine()) != null) {
switch (mode) {
/* find start of the array */
case 0: {
if (line.equals("unsigned char *pool = {"))
mode = 1;
break;
}
/* collect objects until the end of the array */
case 1: {
if (line.equals("};")) {
mode = 2;
break;
}
objs.add(line);
break;
}
/* collect object names from the definitions */
case 2: {
if (line.startsWith("#define")) {
// e.g.: #define RearAdjAuxFun 642
Scanner sc = new Scanner(line);
sc.useDelimiter("[\\s]+");
String define = sc.next();
String name = sc.next();
int id = Integer.parseInt(sc.next());
map1.put(id, name);
}
break;
}
} /* switch */
}
input.close();

for (String obj : objs) {
System.out.println(obj);
Scanner sc = new Scanner(obj);
sc.useDelimiter("[\\s,]+");
CArrayReader br = new CArrayReader(sc);
while (sc.hasNext()) {
int id = br.readId();
VTObject vto = createVTO(br);
String name = map1.get(id);
if (name == null)
name = ""; // not suppose to happen!
name = createUniqueName(name, vto.getType(), set);
set.add(name);
map2.put(id, name);

vto.setName(name);
vto.setId(id);
vto.read(br);
list.add(vto);
}
}
for (VTObject vto : list) {
vto.appendDoc(map2, doc);
}
}
catch (IOException ex){
ex.printStackTrace();
}
}

/**
* This used to be the main method of a separate program.
* @param inputFile
* @param dimension
* @param sk_width
* @param sk_height
* @param fixBitmapPath
* @param stdBitmapPath
* @param outputFile
*/
public void poolImport() {

Expand Down Expand Up @@ -2821,6 +2892,9 @@ public void poolImport() {
else if (inputFile.getName().endsWith(".iop")) {
processIOPFile(inputFile, doc);
}
else if (inputFile.getName().endsWith(".h")) {
processHeaderFile(inputFile, doc);
}

TransformerFactory tf = TransformerFactory.newInstance();
tf.setAttribute("indent-number", Integer.valueOf(2));
Expand Down

0 comments on commit c9724f2

Please sign in to comment.