created mirror

This commit is contained in:
whatever
2026-09-14 20:19:29 -04:00
commit 6764738d92
600 changed files with 87187 additions and 0 deletions

276
Modules/xml/XML.java Normal file
View File

@@ -0,0 +1,276 @@
/*
XML.java
*
* You may use and distribute under the terms of either the GNU Lesser
* General Public License, either version 2 of the license or,
* at your choice, any later version. Alternatively, you may use and
* distribute under the terms of the XPL.
*
* See the LICENSE.lgpl and LICENSE.xpl files for the specific terms of
* the licenses.
*
* This software 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 README
* file for more details.
*
*/
/*
* Written by Tuukka Hastrup
* Very heavily modified by Tuomas Lukka
*/
/** An XML importer/exporter.
* <p>
* Export: <ul>
* <li> with data view cursor, point to a cell containing the filename
* <li> running the action <b>XML.EXPORT</b> will now export the whole space
* </ul>
*<p>
* Import: <ul>
* <li> with data view cursor, point to a cell containing the filename
* <li> if there's a cell from that cell poswards on <d>d.1</b>, this cell
* will be used as a root for the imported space, otherwise a new
* cell is created there
* <li> running the action <b>XML.IMPORT</b> will now import the whole space.
* XXX <string>Notice</string> that if there was a connection from home cell
* negwards on d.1 to be imported, it's not there because that's where
* the filename cell is!
*/
package org.gzigzag.module;
import org.gzigzag.*;
import java.io.*;
import java.net.*;
import org.w3c.dom.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
//import org.openxml.x3p.*;
import org.apache.xml.serialize.*;
import java.util.*;
import java.awt.Point;
public class XML {
public static final String rcsid = "$Id: XML.java,v 1.11 2000/11/03 08:01:05 ajk Exp $";
public static boolean dbg = true;
static final void p(String s) { if(dbg) System.out.println(s); }
static final void pa(String s) { System.out.println(s); }
static public XMLReader getXMLReader() {
return new org.apache.xerces.parsers.SAXParser();
}
static public Document getDocument() {
return new org.apache.xerces.dom.DOMImplementationImpl().
createDocument(null, "ZZSpace", null);
}
static public XMLSerializer getXMLSerializer(Writer writ) {
return getXMLSerializer(writ, "UTF-8");
}
static public XMLSerializer getXMLSerializer(Writer writ, String enc) {
return new org.apache.xml.serialize.XMLSerializer(writ,
new org.apache.xml.serialize.OutputFormat("doc",
enc, true));
}
ZZSpace zz;
static public ZZModule module = new ZZModule() {
public void action(String id, ZZCell code,
ZZCell target,
ZZView v, ZZView cv,
String key, Point pt,
ZZScene xi) {
if(id.equals("EXPORT")) {
synchronized(code.getSpace()) {
(new XML()).export(new File(target.getText()), code.getSpace());
}
} else if(id.equals("IMPORT")) {
(new XML()).load(new File(target.getText()), target.getOrNewCell("d.1", 1));
}
}
};
AttributesImpl ats(String[] a) {
AttributesImpl ai = new AttributesImpl();
for(int i=0; i<a.length-1; i+=2) {
ai.addAttribute(null, a[i], a[i], null, a[i+1]);
}
return ai;
}
AttributesImpl ats(String a, String v) {
return ats(new String[] {a,v});
}
AttributesImpl ats(String a, String v, String a2, String v2) {
return ats(new String[] {a,v, a2, v2});
}
public void export(File out, ZZSpace space) { export(out, space, "UTF-8"); }
public void export(File out, ZZSpace space, String encoding) {
zz = space;
try {
XMLSerializer xser = getXMLSerializer(new FileWriter(out), encoding);
xser.setOutputCharStream(new FileWriter(out));
org.xml.sax.ContentHandler ser = xser.asContentHandler();
ser.startDocument();
ZZCell home = zz.getHomeCell();
ser.startElement(null, "ZZSpace", "ZZSpace",
ats("homeid", home.getID()));
appendCells(ser);
//XXX Should be done appendScrolls();
appendDims(ser);
ser.endElement(null, "ZZSpace", "ZZSpace");
ser.endDocument();
} catch (Exception e) {
ZZLogger.exc(e);
}
}
public void appendCells(org.xml.sax.ContentHandler ser)
throws SAXException {
for(Enumeration e = zz.cells();e.hasMoreElements();) {
ZZCell c = (ZZCell) e.nextElement();
String id = c.getID();
Span span = c.getSpan();
String content;
String dt;
if(span != null) {
dt="SPAN";
content = span.toString();
} else {
dt="TEXT";
content = c.getText(); // XXX clones!
}
ser.startElement(null, "CELL", "CELL",
ats("data", dt, "id", id));
ser.characters(content.toCharArray(), 0, content.length());
ser.endElement(null, "CELL", "CELL");
}
}
public void appendScrolls(Document doc, Element docElem) {
StringScroll scr = zz.getStringScroll();
Element n = doc.createElement("SCROLL");
n.setAttribute("id", scr.getId());
n.setAttribute("type", "STRING");
n.appendChild(doc.createTextNode(scr.getString(0, (int)scr.curEnd())));
docElem.appendChild(n);
}
public void appendDims(org.xml.sax.ContentHandler ser)
throws SAXException {
String[] dims = zz.dims();
for(int n = 0;n < dims.length;n++) {
if(!dims[n].equals("d.cellcreation") && // XXX non-persistents!
!dims[n].equals("d.masterdim")) {
ser.startElement(null, "DIMENSION", "DIMENSION",
ats("name", dims[n]));
for(Enumeration e = zz.posconns(dims[n]);e.hasMoreElements();) {
ZZConnection c = (ZZConnection) e.nextElement();
ser.startElement(null, "CONN", "CONN",
ats("neg", c.c1.getID(), "pos", c.c2.getID()));
ser.endElement(null, "CONN", "CONN");
}
ser.endElement(null, "DIMENSION", "DIMENSION");
}
}
}
public void load(File in, final ZZCell nhome) {
zz = nhome.getSpace();
XMLReader p = getXMLReader();
p.setContentHandler(new DefaultHandler() {
Hashtable id2cell = new Hashtable();
String curdim;
String curcontent;
String ctype;
ZZCell cell;
ZZCell getCell(String id) {
ZZCell c = (ZZCell)id2cell.get(id);
if(c == null)
throw new ZZError("Can't find cell "+id+" in import");
return c;
}
public void startElement(String uri, String nsname,
String name, Attributes attrs) {
if(name.equals("CELL")) {
curcontent = "";
ctype = attrs.getValue("data");
if(attrs.getValue("id").equals("1")) {
cell = nhome;
} else {
cell = nhome.N();
}
id2cell.put(attrs.getValue("id"), cell);
return;
} else {
curcontent = null;
}
if(name.equals("DIMENSION"))
curdim = attrs.getValue("name");
else if(name.equals("CONN")) {
getCell(attrs.getValue("neg")).
connect(curdim,
getCell(attrs.getValue("pos")));
}
}
public void endElement(String uri, String local, String name) {
if(!ctype.equals("TEXT"))
throw new ZZError("No referential XML text yet");
if(curcontent != null)
cell.setText(curcontent);
}
public void characters(char[] ch, int start, int len) {
if(curcontent == null) return;
curcontent = curcontent + new String(ch, start, len);
}
});
try {
p.parse(new InputSource(new FileInputStream(in)));
} catch (Exception e) {
ZZLogger.exc(e);
throw new ZZError("Error in XML import!");
};
/*
ZZCell c;
if(e.getAttribute("id").equals("1")) {
p("Got home cell");
c = nhome; // We're substituting cell #1
} else
c = nhome.N();
*/
/*
if(type.equals("TEXT")) {
c.setText(content);
} else if(type.equals("SPAN")) {
c.setSpan(Span.parse(content));
}
*/
/*
else if(tag.equals("SCROLL")) {
Scroll scr = Scroll.obtain(zz, e.getAttribute("id"));
if(e.getAttribute("type").equals("STRING")) {
((StringScroll)scr).append(((Text)e.getFirstChild()).getData());
}
}
*/
}
}

241
Modules/xml/old2xml.pl Normal file
View File

@@ -0,0 +1,241 @@
#!/usr/bin/perl -w
#############################################################################
#
#
# old2xml.pl
# ==========
#
# Copyright (c) 1999-2000, Ted Nelson, Tuomas Lukka and Vesa Parkkinen
#
# You may use and distribute under the terms of either the GNU Lesser
# General Public License, either version 2 of the license or,
# at your choice, any later version. Alternatively, you may use and
# distribute under the terms of the XPL.
#
# See the LICENSE.lgpl and LICENSE.xpl files for the specific terms of
# the licenses.
#
# This software 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 README
# file for more details.
#
#
#
#
# DON'T EXPECT THIS TO WORK !!
#
# However, if it doesn't work I'd like to know why, so please send me
# a patch to fix it or some kind of bug report.
#
# Author:
# =======
#
# Vesa Parkkinen <veparkki@st.jyu.fi>
#
# Description:
# ===========
#
# This is a simple script to port ZigZag (Andrew Pam's perl version) spaces
# to xml.
#
# Usage:
# =====
#
# First convert zigzag file to ASCII using zzdump from .
# Then convert that file to XML using this script.
#
# zzdump oldfile > somefile
# old2xml.pl < somefile > newfile.xml
#
#
#
# WARNING:
# =======
#
# This script leaves out all connections and cells that
# have anything to do with d.cursor !!!
# It also adds 1000 to cell ids, just to avoid collision with GZZ cell ids.
# ( This should be changed to something more elegant )
#
#
#############################################################################
$_ = <>;
#%names;
print qq(
<?xml version="1.0" encoding="UTF-8"?>
<ZZ home="1">
);
my $buffer;
while (<>) {
#print "line: $_\n";
s/ {2,}//;
if (/'(\d+)'.*/ or /^(\d+).*/) {
$cellno = $1;
/.*=>(.*)/;
$val = $1;
unless ( $val =~ /,/ ) {
$val .= "\n" . <>;
}
$val =~ s/'//g;
# '
$val =~ s/,//g;
$val =~ s/^ //g;
$val =~ s/ $//g;
#print "$val\n";
$names{$cellno} = $val;
} else {
$buffer .= $_;
}
}
#while ( ($key, $value) = each %names) {
# print "KEY: $key = VALUE:$value\n";
#}
delete $names{0};
@lin = split /\n/,$buffer;
print qq(<Cell old="1" dim="d.3" dir="1" new="1000">home</Cell>\n);
#@lines = sort @lin;
@lines = @lin;
#print "lines = @lines";
foreach (@lines) {
if (/d\.cursor/) {
goto NEW;
}
if (/'n' => 100/) {
goto NEW;
}
/'(\d+)(.)(.+)'\s+=>\s+(\d+)/;
$cell = $1;
if ($2 eq "-"){ $dir = -1; }
else {
$dir = 1;
}
$dim = $3;
$other = $4;
$cc = $names{$other};
$cc2 = $names{$cell};
$nother = $other + 1000;
$ncell = $cell + 1000;
#print "cc = $cc, cc2 = $cc2\n";
if ($cc && ! $cc2) {
print qq(<Cell old="$ncell" dim="$dim" dir="$dir" new="$nother">$cc</Cell>\n);
delete $names{$other};
} elsif ( $cc2 ) {
$new_buf .= $_ . "\n";
}
else {
$conn_buf .= $_ . "\n";
}
NEW:
}
while ($new_buf) {
@new_lines = split /\n/,$new_buf;
$new_buf = "";
@new_lines = sort @new_lines;
#print "NEW_LINES @new_lines /NEWLINES\n";
foreach (@new_lines) {
#print "LINE:$_\n";
/'(\d+)(.)(.+)'\s+=>\s+(\d+)/;
#print "S1 = $1 S2 = $2 S3 = $3 S4 = $4\n";
$cell = $1;
if ($2 eq "-"){ $dir = -1; }
else {
$dir = 1;
}
$dim = $3;
$other = $4;
#print "S1 = $cell S2 = $dir S3 = $dim S4 = $other\n";
$cc = $names{$other};
$cc2 = $names{$cell};
#print "CELLS:cc = $cc, cc2 = $cc2\n";
$nother = $other + 1000;
$ncell = $cell + 1000;
# if (! $cc || ! $cc2 ) {
# print "CELLS2:cc = $cc, cc2 = $cc2\n";
# $new_buf .= $_ . "\n";
# goto START;
# }
if ($cc && ! $cc2) {
print qq(<Cell old="$ncell" dim="$dim" dir="$dir" new="$nother">$cc</Cell>\n);
delete $names{$other};
} elsif ( $cc2 ) {
$new_buf .= $_ . "\n";
}
else {
$conn_buf .= $_ . "\n";
}
#
# print qq(<Cell old="$ncell" dim="$dim" dir="$dir" new="$nother">$cc</Cell>\n);
# delete $names{$other};
START:
}
}
if (! $conn_buf) {
goto END;
}
@conn_lines = split /\n/,$conn_buf;
foreach (@conn_lines) {
#print "$_\n";
/'(\d+)(.)(.+)'\s+=>\s+(\d+)/;
#print "S1 = $1 S2 = $2 S3 = $3 S4 = $4\n";
$cell = $1;
if ($2 eq "-"){ $dir = -1; }
else {
$dir = 1;
}
$dim = $3;
$other = $4;
#print "S1 = $cell S2 = $dir S3 = $dim S4 = $other\n";
$cc = $names{$other};
$other = $other + 1000;
$cell = $cell + 1000;
print qq(<Conn c1="$cell" dim="$dim" dir="$dir" c2="$other"/>\n)
}
END:
print "</ZZ>\n";