merge all into single repo
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.github.gumtreediff</groupId>
|
||||
<artifactId>gumtree</artifactId>
|
||||
<version>2.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<artifactId>gen.sax</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
<name>Gumtree XML Tree Generator using SAX</name>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.github.gumtreediff</groupId>
|
||||
<artifactId>core</artifactId>
|
||||
<version>2.0.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>xerces</groupId>
|
||||
<artifactId>xerces</artifactId>
|
||||
<version>2.4.0</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
||||
@@ -0,0 +1,178 @@
|
||||
/*
|
||||
* This file is part of GumTree.
|
||||
*
|
||||
* GumTree is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* GumTree 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 Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with GumTree. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* Copyright 2011-2015 Jean-Rémy Falleri <jr.falleri@gmail.com>
|
||||
* Copyright 2011-2015 Floréal Morandat <florealm@gmail.com>
|
||||
*/
|
||||
|
||||
package com.github.gumtreediff.gen.sax;
|
||||
|
||||
import com.github.gumtreediff.gen.Register;
|
||||
import com.github.gumtreediff.io.LineReader;
|
||||
import com.github.gumtreediff.gen.TreeGenerator;
|
||||
import com.github.gumtreediff.tree.ITree;
|
||||
import com.github.gumtreediff.tree.TreeContext;
|
||||
import org.xml.sax.*;
|
||||
import org.xml.sax.helpers.DefaultHandler;
|
||||
import org.xml.sax.helpers.XMLReaderFactory;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Reader;
|
||||
import java.util.*;
|
||||
|
||||
import static com.github.gumtreediff.tree.ITree.NO_LABEL;
|
||||
|
||||
@Register(id = "xml-sax", accept = {"\\.xml$", "\\.xsd$", "\\.wadl$"})
|
||||
public class SaxTreeGenerator extends TreeGenerator {
|
||||
public static final String DOCUMENT = "Document";
|
||||
public static final String ATTR = "Attr";
|
||||
public static final String CDATA = "CData";
|
||||
public static final String ELT = "Elt";
|
||||
public static final String VALUE = "Value";
|
||||
|
||||
public static final int CDATA_ID = 3;
|
||||
public static final int DOCUMENT_ID = 0;
|
||||
public static final int ATTR_ID = 2;
|
||||
public static final int ELT_ID = 1;
|
||||
public static final int VALUE_ID = 4;
|
||||
|
||||
public TreeContext generate(Reader reader) throws IOException {
|
||||
try {
|
||||
// TreeContext tc = new TreeContext();
|
||||
XMLReader xr = XMLReaderFactory.createXMLReader();
|
||||
LineReader lr = new LineReader(reader);
|
||||
XmlHandlers hdl = new XmlHandlers(lr);
|
||||
|
||||
xr.setContentHandler(hdl);
|
||||
xr.setErrorHandler(hdl);
|
||||
xr.parse(new InputSource(lr));
|
||||
return hdl.tc;
|
||||
} catch (SAXException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
// close resources
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
class XmlHandlers extends DefaultHandler {
|
||||
public int[] lastPosition = {1, 1};
|
||||
|
||||
Locator locator;
|
||||
Deque<ITree> stack = new ArrayDeque<ITree>();
|
||||
TreeContext tc = new TreeContext();
|
||||
Map<String, Integer> names = new HashMap<>();
|
||||
LineReader lineReader;
|
||||
|
||||
public XmlHandlers(LineReader lr) {
|
||||
lineReader = lr;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setDocumentLocator(Locator locator) {
|
||||
this.locator = locator;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void startDocument() throws SAXException {
|
||||
debug("startdoc");
|
||||
|
||||
ITree t = tc.createTree(DOCUMENT_ID, NO_LABEL, DOCUMENT);
|
||||
t.setPos(0);
|
||||
// t.setLcPosStart(lastPosition);
|
||||
tc.setRoot(t);
|
||||
stack.push(t);
|
||||
}
|
||||
|
||||
public void processingInstruction(String target, String data) {
|
||||
System.out.println(target + " " + data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endDocument() throws SAXException {
|
||||
debug("enddoc");
|
||||
ITree t = stack.pop();
|
||||
int line = locator.getLineNumber();
|
||||
int col = locator.getColumnNumber();
|
||||
// t.setLcPosEnd(new int[]{line, col});
|
||||
t.setLength(lineReader.positionFor(line, col));
|
||||
assert stack.isEmpty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void startElement(String uri, String localName, String qName,
|
||||
Attributes attributes) throws SAXException {
|
||||
debug("startElt", qName);
|
||||
ITree t = tc.createTree(ELT_ID, qName, ELT);
|
||||
addAttributes(t, attributes);
|
||||
setStartPosition(t);
|
||||
ITree p = stack.peek();
|
||||
p.addChild(t);
|
||||
stack.push(t);
|
||||
}
|
||||
|
||||
void debug(Object... o) {
|
||||
System.out.println("lc: " + locator.getLineNumber() + ":" + locator.getColumnNumber());
|
||||
System.out.println("last: " + Arrays.toString(lastPosition));
|
||||
System.out.println("stack: " + stack);
|
||||
if (o.length > 0)
|
||||
System.out.println("=> " + Arrays.toString(o));
|
||||
}
|
||||
|
||||
private void setStartPosition(ITree t) {
|
||||
int[] pos = currentPosition();
|
||||
// t.setLcPosStart(pos);
|
||||
t.setPos(lineReader.positionFor(pos[0], pos[1]));
|
||||
}
|
||||
|
||||
private void setEndPosition(ITree t) {
|
||||
int[] pos = currentPosition();
|
||||
// t.setLcPosEnd(new int[]{locator.getLineNumber(), locator.getColumnNumber()}); //FIXME
|
||||
t.setPos(lineReader.positionFor(locator.getLineNumber(), locator.getColumnNumber()) - t.getPos());
|
||||
}
|
||||
|
||||
private int[] currentPosition() {
|
||||
int[] lp = lastPosition;
|
||||
lastPosition = new int[]{locator.getLineNumber(), locator.getColumnNumber()};
|
||||
return lp;
|
||||
}
|
||||
|
||||
private void addAttributes(ITree tree, Attributes attrs) {
|
||||
int len = attrs.getLength();
|
||||
for (int i = 0; i < len; i++) {
|
||||
ITree at = tc.createTree(ATTR_ID, attrs.getQName(i), ATTR);
|
||||
at.addChild(tc.createTree(VALUE_ID, attrs.getValue(i), VALUE));
|
||||
tree.addChild(at);
|
||||
setStartPosition(at);
|
||||
setEndPosition(at); // FIXME grab next
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endElement(String uri, String localName, String qName) throws SAXException {
|
||||
debug("endelt", localName);
|
||||
|
||||
ITree t = stack.pop();
|
||||
setEndPosition(t);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void characters(char[] ch, int start, int length) throws SAXException {
|
||||
debug("char", start, length);
|
||||
tc.createTree(CDATA_ID, new String(ch, start, length), CDATA);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* This file is part of GumTree.
|
||||
*
|
||||
* GumTree is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* GumTree 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 Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with GumTree. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* Copyright 2011-2015 Jean-Rémy Falleri <jr.falleri@gmail.com>
|
||||
* Copyright 2011-2015 Floréal Morandat <florealm@gmail.com>
|
||||
*/
|
||||
|
||||
package com.github.gumtreediff.gen.sax;
|
||||
|
||||
import com.github.gumtreediff.io.TreeIoUtils;
|
||||
import com.github.gumtreediff.tree.TreeContext;
|
||||
import com.github.gumtreediff.io.TreeIoUtils;
|
||||
import com.github.gumtreediff.tree.TreeContext;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.InputStreamReader;
|
||||
|
||||
public class TestParsing {
|
||||
@Test
|
||||
public void testA1() throws Exception {
|
||||
TreeContext tc = new SaxTreeGenerator().generateFromReader(
|
||||
new InputStreamReader(getClass().getResourceAsStream("/action_v0.xml")));
|
||||
TreeIoUtils.toXml(tc).writeTo(System.out);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
<!--
|
||||
This file is part of GumTree.
|
||||
|
||||
GumTree is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
GumTree 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 Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with GumTree. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
Copyright 2011-2015 Jean-Rémy Falleri <jr.falleri@gmail.com>
|
||||
Copyright 2011-2015 Floréal Morandat <florealm@gmail.com>
|
||||
-->
|
||||
<tree type="0" label="a" >
|
||||
<tree type="1" label="b" >
|
||||
<tree type="2" label="c" />
|
||||
<tree type="2" label="d" />
|
||||
</tree>
|
||||
<tree type="1" label="e" />
|
||||
<tree type="1" label="f" >
|
||||
<tree type="2" label="g" >
|
||||
<tree type="3" label="h" >
|
||||
<tree type="4" label="i" />
|
||||
<tree type="4" label="j" />
|
||||
<tree type="4" label="k" />
|
||||
</tree>
|
||||
</tree>
|
||||
<tree type="2" label="l" >
|
||||
<tree type="3" label="m" />
|
||||
</tree>
|
||||
</tree>
|
||||
</tree>
|
||||
@@ -0,0 +1,26 @@
|
||||
<!--
|
||||
This file is part of GumTree.
|
||||
|
||||
GumTree is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
GumTree 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 Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with GumTree. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
Copyright 2011-2015 Jean-Rémy Falleri <jr.falleri@gmail.com>
|
||||
Copyright 2011-2015 Floréal Morandat <florealm@gmail.com>
|
||||
-->
|
||||
<tree type="0" typeLabel="0" label="a" pos="0" length="0">
|
||||
<tree type="1" typeLabel="1" label="b" pos="0" length="0">
|
||||
<tree type="3" typeLabel="3" label="c" pos="0" length="0"/>
|
||||
<tree type="3" typeLabel="3" label="d" pos="0" length="0"/>
|
||||
</tree>
|
||||
<tree type="2" typeLabel="2" label="e" pos="0" length="0"/>
|
||||
</tree>
|
||||
@@ -0,0 +1,31 @@
|
||||
<!--
|
||||
This file is part of GumTree.
|
||||
|
||||
GumTree is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
GumTree 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 Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with GumTree. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
Copyright 2011-2015 Jean-Rémy Falleri <jr.falleri@gmail.com>
|
||||
Copyright 2011-2015 Floréal Morandat <florealm@gmail.com>
|
||||
-->
|
||||
<tree type="0" typeLabel="0" label="a" pos="0" length="0">
|
||||
<tree type="4" typeLabel="4" label="f" pos="0" length="0">
|
||||
<tree type="1" typeLabel="1" label="b" pos="0" length="0">
|
||||
<tree type="3" typeLabel="3" label="c" pos="0" length="0"/>
|
||||
<tree type="3" typeLabel="3" label="d" pos="0" length="0"/>
|
||||
<tree type="5" typeLabel="5" label="h" pos="0" length="0"/>
|
||||
</tree>
|
||||
</tree>
|
||||
<tree type="2" typeLabel="2" label="i" pos="0" length="0">
|
||||
<tree type="2" typeLabel="2" label="j" pos="0" length="0"/>
|
||||
</tree>
|
||||
</tree>
|
||||
@@ -0,0 +1,29 @@
|
||||
<!--
|
||||
This file is part of GumTree.
|
||||
|
||||
GumTree is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
GumTree 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 Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with GumTree. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
Copyright 2011-2015 Jean-Rémy Falleri <jr.falleri@gmail.com>
|
||||
Copyright 2011-2015 Floréal Morandat <florealm@gmail.com>
|
||||
-->
|
||||
<tree type="0" label="a">
|
||||
<tree type="0" label="e">
|
||||
<tree type="0" label="f" />
|
||||
</tree>
|
||||
<tree type="0" label="b">
|
||||
<tree type="0" label="c" />
|
||||
<tree type="0" label="d" />
|
||||
</tree>
|
||||
<tree type="0" label="g" />
|
||||
</tree>
|
||||
@@ -0,0 +1,30 @@
|
||||
<!--
|
||||
This file is part of GumTree.
|
||||
|
||||
GumTree is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
GumTree 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 Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with GumTree. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
Copyright 2011-2015 Jean-Rémy Falleri <jr.falleri@gmail.com>
|
||||
Copyright 2011-2015 Floréal Morandat <florealm@gmail.com>
|
||||
-->
|
||||
<tree type="0" label="a">
|
||||
<tree type="0" label="b">
|
||||
<tree type="0" label="c" />
|
||||
<tree type="0" label="d" />
|
||||
</tree>
|
||||
<tree type="1" label="h">
|
||||
<tree type="0" label="e">
|
||||
<tree type="0" label="f" />
|
||||
</tree>
|
||||
</tree>
|
||||
</tree>
|
||||
Reference in New Issue
Block a user