* This method uses a change in character type as a splitter
* of two words. For example, "abc100ghi" will be splitted into
* {"Abc", "100","Ghi"}.
*/
public static String[] toWordList(String s) {
ArrayList ss = new ArrayList();
int n = s.length();
for (int i = 0; i < n;) {
// Skip punctuation
while (i < n) {
if (!isPunct(s.charAt(i)))
break;
i++;
}
if (i >= n) break;
// Find next break and collect word
int b = nextBreak(s, i);
String w = (b == -1) ? s.substring(i) : s.substring(i, b);
ss.add(escape(capitalize(w)));
if (b == -1) break;
i = b;
}
// we can't guarantee a valid Java identifier anyway,
// so there's not much point in rejecting things in this way.
// if (ss.size() == 0)
// throw new IllegalArgumentException("Zero-length identifier");
return (String[])(ss.toArray(new String[0]));
}
protected static String toMixedCaseName(String[] ss, boolean startUpper) {
StringBuffer sb = new StringBuffer();
if(ss.length>0) {
sb.append(startUpper ? ss[0] : ss[0].toLowerCase());
for (int i = 1; i < ss.length; i++)
sb.append(ss[i]);
}
return sb.toString();
}
protected static String toMixedCaseVariableName(String[] ss,
boolean startUpper,
boolean cdrUpper) {
if (cdrUpper)
for (int i = 1; i < ss.length; i++)
ss[i] = capitalize(ss[i]);
StringBuffer sb = new StringBuffer();
if( ss.length>0 ) {
sb.append(startUpper ? ss[0] : ss[0].toLowerCase());
for (int i = 1; i < ss.length; i++)
sb.append(ss[i]);
}
return sb.toString();
}
/**
* Formats a string into "THIS_KIND_OF_FORMAT_ABC_DEF".
*
* @return
* Always return a string but there's no guarantee that
* the generated code is a valid Java identifier.
*/
public static String toConstantName(String s) {
return toConstantName(toWordList(s));
}
/**
* Formats a string into "THIS_KIND_OF_FORMAT_ABC_DEF".
*
* @return
* Always return a string but there's no guarantee that
* the generated code is a valid Java identifier.
*/
public static String toConstantName(String[] ss) {
StringBuffer sb = new StringBuffer();
if( ss.length>0 ) {
sb.append(ss[0].toUpperCase());
for (int i = 1; i < ss.length; i++) {
sb.append('_');
sb.append(ss[i].toUpperCase());
}
}
return sb.toString();
}
/**
* Escapes characters is the given string so that they can be
* printed by only using US-ASCII characters.
*
* The escaped characters will be appended to the given
* StringBuffer.
*
* @param sb
* StringBuffer that receives escaped string.
* @param s
* String to be escaped.
* When the element names are in conflict, this can be used.
*/
protected String alternativeName;
public WriterNode(Locator location, Leaf leaf) {
super(location, leaf);
}
/**
* Declares the class without its contents.
*
* The first step of the code generation.
*/
abstract void declare(NodeSet nset);
/**
* Generates the contents.
*/
abstract void generate(NodeSet nset);
/**
* Prepares for the code generation.
*/
void prepare(NodeSet nset) {}
}
txw2-0.1+20070624/src/compiler/com/sun/tools/txw2/model/XmlNode.java 0000664 0000000 0000000 00000003641 11571421112 0024516 0 ustar 00root root 0000000 0000000 /*
* The contents of this file are subject to the terms
* of the Common Development and Distribution License
* (the "License"). You may not use this file except
* in compliance with the License.
*
* You can obtain a copy of the license at
* https://jwsdp.dev.java.net/CDDLv1.0.html
* See the License for the specific language governing
* permissions and limitations under the License.
*
* When distributing Covered Code, include this CDDL
* HEADER in each file and include the License file at
* https://jwsdp.dev.java.net/CDDLv1.0.html If applicable,
* add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your
* own identifying information: Portions Copyright [yyyy]
* [name of copyright owner]
*/
package com.sun.tools.txw2.model;
import org.xml.sax.Locator;
import javax.xml.namespace.QName;
import java.util.HashSet;
import java.util.Set;
import java.util.Stack;
/**
* Either an {@link Element} or {@link Attribute}.
*
* @author Kohsuke Kawaguchi
*/
public abstract class XmlNode extends WriterNode {
/**
* Name of the attribute/element.
*
* In TXW, we ignore all infinite names.
* (finite name class will be expanded to a list of {@link XmlNode}s.
*/
public final QName name;
protected XmlNode(Locator location, QName name, Leaf leaf) {
super(location, leaf);
this.name = name;
}
/**
* Expand all refs and collect all children.
*/
protected final Set
* Instances of this class implement {@link #equals(Object)}
* and {@link #hashCode()}. By using these we avoid generating
* the same method twice.
*
* @author Kohsuke Kawaguchi
*/
public abstract class Prop {
}
txw2-0.1+20070624/src/compiler/com/sun/tools/txw2/model/prop/ValueProp.java 0000664 0000000 0000000 00000002365 11571421112 0026047 0 ustar 00root root 0000000 0000000 /*
* The contents of this file are subject to the terms
* of the Common Development and Distribution License
* (the "License"). You may not use this file except
* in compliance with the License.
*
* You can obtain a copy of the license at
* https://jwsdp.dev.java.net/CDDLv1.0.html
* See the License for the specific language governing
* permissions and limitations under the License.
*
* When distributing Covered Code, include this CDDL
* HEADER in each file and include the License file at
* https://jwsdp.dev.java.net/CDDLv1.0.html If applicable,
* add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your
* own identifying information: Portions Copyright [yyyy]
* [name of copyright owner]
*/
package com.sun.tools.txw2.model.prop;
import com.sun.codemodel.JType;
/**
* @author Kohsuke Kawaguchi
*/
public class ValueProp extends Prop {
private final JType type;
public ValueProp(JType type) {
this.type = type;
}
public boolean equals(Object o) {
if (!(o instanceof ValueProp)) return false;
final ValueProp that = (ValueProp) o;
return type.equals(that.type);
}
public int hashCode() {
return type.hashCode();
}
}
txw2-0.1+20070624/src/compiler/com/sun/tools/txw2/model/prop/XmlItemProp.java 0000664 0000000 0000000 00000002773 11571421112 0026355 0 ustar 00root root 0000000 0000000 /*
* The contents of this file are subject to the terms
* of the Common Development and Distribution License
* (the "License"). You may not use this file except
* in compliance with the License.
*
* You can obtain a copy of the license at
* https://jwsdp.dev.java.net/CDDLv1.0.html
* See the License for the specific language governing
* permissions and limitations under the License.
*
* When distributing Covered Code, include this CDDL
* HEADER in each file and include the License file at
* https://jwsdp.dev.java.net/CDDLv1.0.html If applicable,
* add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your
* own identifying information: Portions Copyright [yyyy]
* [name of copyright owner]
*/
package com.sun.tools.txw2.model.prop;
import com.sun.codemodel.JType;
import javax.xml.namespace.QName;
/**
* Common implementation between elements and attributes.
*
* @author Kohsuke Kawaguchi
*/
abstract class XmlItemProp extends Prop {
private final QName name;
private final JType type;
public XmlItemProp(QName name, JType valueType) {
this.name = name;
this.type = valueType;
}
public final boolean equals(Object o) {
if (this.getClass()!=o.getClass()) return false;
XmlItemProp that = (XmlItemProp)o;
return this.name.equals(that.name)
&& this.type.equals(that.type);
}
public final int hashCode() {
return name.hashCode()*29 + type.hashCode();
}
}
txw2-0.1+20070624/src/runtime/ 0000775 0000000 0000000 00000000000 11571421112 0015423 5 ustar 00root root 0000000 0000000 txw2-0.1+20070624/src/runtime/com/ 0000775 0000000 0000000 00000000000 11571421112 0016201 5 ustar 00root root 0000000 0000000 txw2-0.1+20070624/src/runtime/com/sun/ 0000775 0000000 0000000 00000000000 11571421112 0017006 5 ustar 00root root 0000000 0000000 txw2-0.1+20070624/src/runtime/com/sun/xml/ 0000775 0000000 0000000 00000000000 11571421112 0017606 5 ustar 00root root 0000000 0000000 txw2-0.1+20070624/src/runtime/com/sun/xml/txw2/ 0000775 0000000 0000000 00000000000 11571421112 0020512 5 ustar 00root root 0000000 0000000 txw2-0.1+20070624/src/runtime/com/sun/xml/txw2/Attribute.java 0000664 0000000 0000000 00000002642 11571421112 0023324 0 ustar 00root root 0000000 0000000 /*
* The contents of this file are subject to the terms
* of the Common Development and Distribution License
* (the "License"). You may not use this file except
* in compliance with the License.
*
* You can obtain a copy of the license at
* https://jwsdp.dev.java.net/CDDLv1.0.html
* See the License for the specific language governing
* permissions and limitations under the License.
*
* When distributing Covered Code, include this CDDL
* HEADER in each file and include the License file at
* https://jwsdp.dev.java.net/CDDLv1.0.html If applicable,
* add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your
* own identifying information: Portions Copyright [yyyy]
* [name of copyright owner]
*/
package com.sun.xml.txw2;
/**
* @author Kohsuke Kawaguchi
*/
final class Attribute {
final String nsUri;
final String localName;
/**
* Attributes of an element form a linked list.
*/
Attribute next;
/**
* Attribute value that potentially contains dummy prefixes.
*/
final StringBuilder value = new StringBuilder();
Attribute(String nsUri, String localName) {
assert nsUri!=null && localName!=null;
this.nsUri = nsUri;
this.localName = localName;
}
boolean hasName( String nsUri, String localName ) {
return this.localName.equals(localName) && this.nsUri.equals(nsUri);
}
}
txw2-0.1+20070624/src/runtime/com/sun/xml/txw2/Cdata.java 0000664 0000000 0000000 00000002104 11571421112 0022366 0 ustar 00root root 0000000 0000000 /*
* The contents of this file are subject to the terms
* of the Common Development and Distribution License
* (the "License"). You may not use this file except
* in compliance with the License.
*
* You can obtain a copy of the license at
* https://jwsdp.dev.java.net/CDDLv1.0.html
* See the License for the specific language governing
* permissions and limitations under the License.
*
* When distributing Covered Code, include this CDDL
* HEADER in each file and include the License file at
* https://jwsdp.dev.java.net/CDDLv1.0.html If applicable,
* add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your
* own identifying information: Portions Copyright [yyyy]
* [name of copyright owner]
*/
package com.sun.xml.txw2;
/**
* CDATA section.
*
* @author Kohsuke Kawaguchi
*/
final class Cdata extends Text {
Cdata(Document document, NamespaceResolver nsResolver, Object obj) {
super(document, nsResolver, obj);
}
void accept(ContentVisitor visitor) {
visitor.onCdata(buffer);
}
}
txw2-0.1+20070624/src/runtime/com/sun/xml/txw2/Comment.java 0000664 0000000 0000000 00000002415 11571421112 0022761 0 ustar 00root root 0000000 0000000 /*
* The contents of this file are subject to the terms
* of the Common Development and Distribution License
* (the "License"). You may not use this file except
* in compliance with the License.
*
* You can obtain a copy of the license at
* https://jwsdp.dev.java.net/CDDLv1.0.html
* See the License for the specific language governing
* permissions and limitations under the License.
*
* When distributing Covered Code, include this CDDL
* HEADER in each file and include the License file at
* https://jwsdp.dev.java.net/CDDLv1.0.html If applicable,
* add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your
* own identifying information: Portions Copyright [yyyy]
* [name of copyright owner]
*/
package com.sun.xml.txw2;
/**
* Comment.
*
* @author Kohsuke Kawaguchi
*/
final class Comment extends Content {
/**
* The text to be writtten.
*/
private final StringBuilder buffer = new StringBuilder();
public Comment(Document document, NamespaceResolver nsResolver, Object obj) {
document.writeValue(obj,nsResolver,buffer);
}
boolean concludesPendingStartTag() {
return false;
}
void accept(ContentVisitor visitor) {
visitor.onComment(buffer);
}
}
txw2-0.1+20070624/src/runtime/com/sun/xml/txw2/ContainerElement.java 0000664 0000000 0000000 00000025632 11571421112 0024621 0 ustar 00root root 0000000 0000000 /*
* The contents of this file are subject to the terms
* of the Common Development and Distribution License
* (the "License"). You may not use this file except
* in compliance with the License.
*
* You can obtain a copy of the license at
* https://jwsdp.dev.java.net/CDDLv1.0.html
* See the License for the specific language governing
* permissions and limitations under the License.
*
* When distributing Covered Code, include this CDDL
* HEADER in each file and include the License file at
* https://jwsdp.dev.java.net/CDDLv1.0.html If applicable,
* add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your
* own identifying information: Portions Copyright [yyyy]
* [name of copyright owner]
*/
package com.sun.xml.txw2;
import com.sun.xml.txw2.annotation.XmlAttribute;
import com.sun.xml.txw2.annotation.XmlElement;
import com.sun.xml.txw2.annotation.XmlNamespace;
import com.sun.xml.txw2.annotation.XmlValue;
import com.sun.xml.txw2.annotation.XmlCDATA;
import javax.xml.namespace.QName;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
/**
* Dynamically implements {@link TypedXmlWriter} interfaces.
*
* @author Kohsuke Kawaguchi
*/
final class ContainerElement implements InvocationHandler, TypedXmlWriter {
final Document document;
/**
* Initially, point to the start tag token, but
* once we know we are done with the start tag, we will reset it to null
* so that the token sequence can be GC-ed.
*/
StartTag startTag;
final EndTag endTag = new EndTag();
/**
* Namespace URI of this element.
*/
private final String nsUri;
/**
* When this element can accept more child content, this value
* is non-null and holds the last child {@link Content}.
*
* If this element is committed, this parameter is null.
*/
private Content tail;
/**
* Uncommitted {@link ContainerElement}s form a doubly-linked list,
* so that the parent can close them recursively.
*/
private ContainerElement prevOpen;
private ContainerElement nextOpen;
private final ContainerElement parent;
private ContainerElement lastOpenChild;
/**
* Set to true if the start eleent is blocked.
*/
private boolean blocked;
public ContainerElement(Document document,ContainerElement parent,String nsUri, String localName) {
this.parent = parent;
this.document = document;
this.nsUri = nsUri;
this.startTag = new StartTag(this,nsUri,localName);
tail = startTag;
if(isRoot())
document.setFirstContent(startTag);
}
private boolean isRoot() {
return parent==null;
}
private boolean isCommitted() {
return tail==null;
}
public Document getDocument() {
return document;
}
boolean isBlocked() {
return blocked && !isCommitted();
}
public void block() {
blocked = true;
}
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if(method.getDeclaringClass()==TypedXmlWriter.class || method.getDeclaringClass()==Object.class) {
// forward to myself
try {
return method.invoke(this,args);
} catch (InvocationTargetException e) {
throw e.getTargetException();
}
}
XmlAttribute xa = method.getAnnotation(XmlAttribute.class);
XmlValue xv = method.getAnnotation(XmlValue.class);
XmlElement xe = method.getAnnotation(XmlElement.class);
if(xa!=null) {
if(xv!=null || xe!=null)
throw new IllegalAnnotationException(method.toString());
addAttribute(xa,method,args);
return proxy; // allow method chaining
}
if(xv!=null) {
if(xe!=null)
throw new IllegalAnnotationException(method.toString());
_pcdata(args);
return proxy; // allow method chaining
}
return addElement(xe,method,args);
}
/**
* Writes an attribute.
*/
private void addAttribute(XmlAttribute xa, Method method, Object[] args) {
assert xa!=null;
checkStartTag();
String localName = xa.value();
if(xa.value().length()==0)
localName = method.getName();
_attribute(xa.ns(),localName,args);
}
private void checkStartTag() {
if(startTag==null)
throw new IllegalStateException("start tag has already been written");
}
/**
* Writes a new element.
*/
private Object addElement(XmlElement e, Method method, Object[] args) {
Class> rt = method.getReturnType();
// the last precedence: default name
String nsUri = "##default";
String localName = method.getName();
if(e!=null) {
// then the annotation on this method
if(e.value().length()!=0)
localName = e.value();
nsUri = e.ns();
}
if(nsUri.equals("##default")) {
// look for the annotation on the declaring class
Class> c = method.getDeclaringClass();
XmlElement ce = c.getAnnotation(XmlElement.class);
if(ce!=null) {
nsUri = ce.ns();
}
if(nsUri.equals("##default"))
// then default to the XmlNamespace
nsUri = getNamespace(c.getPackage());
}
if(rt==Void.TYPE) {
// leaf element with just a value
boolean isCDATA = method.getAnnotation(XmlCDATA.class)!=null;
StartTag st = new StartTag(document,nsUri,localName);
addChild(st);
for( Object arg : args ) {
Text text;
if(isCDATA) text = new Cdata(document,st,arg);
else text = new Pcdata(document,st,arg);
addChild(text);
}
addChild(new EndTag());
return null;
}
if(TypedXmlWriter.class.isAssignableFrom(rt)) {
// sub writer
return _element(nsUri,localName,(Class)rt);
}
throw new IllegalSignatureException("Illegal return type: "+rt);
}
/**
* Decides the namespace URI of the given package.
*/
private String getNamespace(Package pkg) {
if(pkg==null) return "";
String nsUri;
XmlNamespace ns = pkg.getAnnotation(XmlNamespace.class);
if(ns!=null)
nsUri = ns.value();
else
nsUri = "";
return nsUri;
}
/**
* Appends this child object to the tail.
*/
private void addChild(Content child) {
tail.setNext(document,child);
tail = child;
}
public void commit() {
commit(true);
}
public void commit(boolean includingAllPredecessors) {
_commit(includingAllPredecessors);
document.flush();
}
private void _commit(boolean includingAllPredecessors) {
if(isCommitted()) return;
addChild(endTag);
if(isRoot())
addChild(new EndDocument());
tail = null;
// _commit predecessors if so told
if(includingAllPredecessors) {
for( ContainerElement e=this; e!=null; e=e.parent ) {
while(e.prevOpen!=null) {
e.prevOpen._commit(false);
// e.prevOpen should change as a result of committing it.
}
}
}
// _commit all children recursively
while(lastOpenChild!=null)
lastOpenChild._commit(false);
// remove this node from the link
if(parent!=null) {
if(parent.lastOpenChild==this) {
assert nextOpen==null : "this must be the last one";
parent.lastOpenChild = prevOpen;
} else {
assert nextOpen.prevOpen==this;
nextOpen.prevOpen = this.prevOpen;
}
if(prevOpen!=null) {
assert prevOpen.nextOpen==this;
prevOpen.nextOpen = this.nextOpen;
}
}
this.nextOpen = null;
this.prevOpen = null;
}
public void _attribute(String localName, Object value) {
_attribute("",localName,value);
}
public void _attribute(String nsUri, String localName, Object value) {
checkStartTag();
startTag.addAttribute(nsUri,localName,value);
}
public void _attribute(QName attributeName, Object value) {
_attribute(attributeName.getNamespaceURI(),attributeName.getLocalPart(),value);
}
public void _namespace(String uri) {
_namespace(uri,false);
}
public void _namespace(String uri, String prefix) {
if(prefix==null)
throw new IllegalArgumentException();
checkStartTag();
startTag.addNamespaceDecl(uri,prefix,false);
}
public void _namespace(String uri, boolean requirePrefix) {
checkStartTag();
startTag.addNamespaceDecl(uri,null,requirePrefix);
}
public void _pcdata(Object value) {
// we need to allow this method even when startTag has already been completed.
// checkStartTag();
addChild(new Pcdata(document,startTag,value));
}
public void _cdata(Object value) {
addChild(new Cdata(document,startTag,value));
}
public void _comment(Object value) throws UnsupportedOperationException {
addChild(new Comment(document,startTag,value));
}
public
* This is so that we can defer the writing as much as possible.
*/
private boolean started=false;
/**
* Currently active writer.
*
*
* This points to the last written token.
*/
private Content current = null;
private final Map
* Applications can add their own {@link DatatypeWriter} so that
* application-specific objects can be turned into {@link String}
* for output.
*
* @param dw
* The {@link DatatypeWriter} to be added. Must not be null.
*/
public void addDatatypeWriter( DatatypeWriter> dw ) {
datatypeWriters.put(dw.getType(),dw);
}
/**
* Performs the output as much as possible
*/
void run() {
while(true) {
Content next = current.getNext();
if(next==null || !next.isReadyToCommit())
return;
next.accept(visitor);
next.written();
current = next;
}
}
/**
* Appends the given object to the end of the given buffer.
*
* @param nsResolver
* use
*/
void writeValue( Object obj, NamespaceResolver nsResolver, StringBuilder buf ) {
if(obj==null)
throw new IllegalArgumentException("argument contains null");
if(obj instanceof Object[]) {
for( Object o : (Object[])obj )
writeValue(o,nsResolver,buf);
return;
}
if(obj instanceof Iterable) {
for( Object o : (Iterable>)obj )
writeValue(o,nsResolver,buf);
return;
}
if(buf.length()>0)
buf.append(' ');
Class c = obj.getClass();
while(c!=null) {
DatatypeWriter dw = datatypeWriters.get(c);
if(dw!=null) {
dw.print(obj,nsResolver,buf);
return;
}
c = c.getSuperclass();
}
// if nothing applies, just use toString
buf.append(obj);
}
// I wanted to hide those write method from users
private final ContentVisitor visitor = new ContentVisitor() {
public void onStartDocument() {
// the startDocument token is used as the sentry, so this method shall never
// be called.
// out.startDocument() is invoked when we write the start tag of the root element.
throw new IllegalStateException();
}
public void onEndDocument() {
out.endDocument();
}
public void onEndTag() {
out.endTag();
inscopeNamespace.popContext();
activeNamespaces = null;
}
public void onPcdata(StringBuilder buffer) {
if(activeNamespaces!=null)
buffer = fixPrefix(buffer);
out.text(buffer);
}
public void onCdata(StringBuilder buffer) {
if(activeNamespaces!=null)
buffer = fixPrefix(buffer);
out.cdata(buffer);
}
public void onComment(StringBuilder buffer) {
if(activeNamespaces!=null)
buffer = fixPrefix(buffer);
out.comment(buffer);
}
public void onStartTag(String nsUri, String localName, Attribute attributes, NamespaceDecl namespaces) {
assert nsUri!=null;
assert localName!=null;
activeNamespaces = namespaces;
if(!started) {
started = true;
out.startDocument();
}
inscopeNamespace.pushContext();
// declare the explicitly bound namespaces
for( NamespaceDecl ns=namespaces; ns!=null; ns=ns.next ) {
ns.declared = false; // reset this flag
if(ns.prefix!=null) {
String uri = inscopeNamespace.getURI(ns.prefix);
if(uri!=null && uri.equals(ns.uri))
; // already declared
else {
// declare this new binding
inscopeNamespace.declarePrefix(ns.prefix,ns.uri);
ns.declared = true;
}
}
}
// then use in-scope namespace to assign prefixes to others
for( NamespaceDecl ns=namespaces; ns!=null; ns=ns.next ) {
if(ns.prefix==null) {
if(inscopeNamespace.getURI("").equals(ns.uri))
ns.prefix="";
else {
String p = inscopeNamespace.getPrefix(ns.uri);
if(p==null) {
// assign a new one
while(inscopeNamespace.getURI(p=newPrefix())!=null)
;
ns.declared = true;
inscopeNamespace.declarePrefix(p,ns.uri);
}
ns.prefix = p;
}
}
}
// the first namespace decl must be the one for the element
assert namespaces.uri.equals(nsUri);
assert namespaces.prefix!=null : "a prefix must have been all allocated";
out.beginStartTag(nsUri,localName,namespaces.prefix);
// declare namespaces
for( NamespaceDecl ns=namespaces; ns!=null; ns=ns.next ) {
if(ns.declared)
out.writeXmlns( ns.prefix, ns.uri );
}
// writeBody attributes
for( Attribute a=attributes; a!=null; a=a.next) {
String prefix;
if(a.nsUri.length()==0) prefix="";
else prefix=inscopeNamespace.getPrefix(a.nsUri);
out.writeAttribute( a.nsUri, a.localName, prefix, fixPrefix(a.value) );
}
out.endStartTag(nsUri,localName,namespaces.prefix);
}
};
/**
* Used by {@link #newPrefix()}.
*/
private final StringBuilder prefixSeed = new StringBuilder("ns");
private int prefixIota = 0;
/**
* Allocates a new unique prefix.
*/
private String newPrefix() {
prefixSeed.setLength(2);
prefixSeed.append(++prefixIota);
return prefixSeed.toString();
}
/**
* Replaces dummy prefixes in the value to the real ones
* by using {@link #activeNamespaces}.
*
* @return
* the buffer passed as the buf parameter.
*/
private StringBuilder fixPrefix(StringBuilder buf) {
assert activeNamespaces!=null;
int i;
int len=buf.length();
for(i=0;is.substring(start)
* will be escaped and copied to the string buffer.
*/
public static void escape(StringBuffer sb, String s, int start) {
int n = s.length();
for (int i = start; i < n; i++) {
char c = s.charAt(i);
if (Character.isJavaIdentifierPart(c))
sb.append(c);
else {
sb.append("_");
if (c <= '\u000f') sb.append("000");
else if (c <= '\u00ff') sb.append("00");
else if (c <= '\u0fff') sb.append("0");
sb.append(Integer.toString(c, 16));
}
}
}
/**
* Escapes characters that are unusable as Java identifiers
* by replacing unsafe characters with safe characters.
*/
private static String escape(String s) {
int n = s.length();
for (int i = 0; i < n; i++)
if (!Character.isJavaIdentifierPart(s.charAt(i))) {
StringBuffer sb = new StringBuffer(s.substring(0, i));
escape(sb, s, i);
return sb.toString();
}
return s;
}
/**
* Escape any characters that would cause the single arg constructor
* of java.net.URI to complain about illegal chars.
*
* @param s source string to be escaped
*/
public static String escapeURI(String s) {
StringBuffer sb = new StringBuffer();
for( int i = 0; i < s.length(); i++ ) {
char c = s.charAt(i);
if(Character.isSpaceChar(c)) {
sb.append("%20");
} else {
sb.append(c);
}
}
return sb.toString();
}
/**
* Calculate the parent URI path of the given URI path.
*
* @param uriPath the uriPath (as returned by java.net.URI#getPath()
* @return the parent URI path of the given URI path
*/
public static String getParentUriPath(String uriPath) {
int idx = uriPath.lastIndexOf('/');
if (uriPath.endsWith("/")) {
uriPath = uriPath.substring(0,idx); // trim trailing slash
idx = uriPath.lastIndexOf('/'); // move idx to parent context
}
return uriPath.substring(0, idx)+"/";
}
/**
* Calculate the normalized form of the given uriPath.
*
* For example:
* /a/b/c/ -> /a/b/c/
* /a/b/c -> /a/b/
* /a/ -> /a/
* /a -> /
*
* @param uriPath path of a URI (as returned by java.net.URI#getPath()
* @return the normalized uri path
*/
public static String normalizeUriPath(String uriPath) {
if (uriPath.endsWith("/"))
return uriPath;
// the uri path should always have at least a leading slash,
// so no need to make sure that ( idx == -1 )
int idx = uriPath.lastIndexOf('/');
return uriPath.substring(0, idx+1);
}
/**
* determine if two Strings are equal ignoring case allowing null values
*
* @param s string 1
* @param t string 2
* @return true iff the given strings are equal ignoring case, false if they aren't
* equal or either of them are null.
*/
public static boolean equalsIgnoreCase(String s, String t) {
if (s == t) return true;
if ((s != null) && (t != null)) {
return s.equalsIgnoreCase(t);
}
return false;
}
/**
* determine if two Strings are iqual allowing null values
*
* @param s string 1
* @param t string 2
* @return true iff the strings are equal, false if they aren't equal or either of
* them are null.
*/
public static boolean equal(String s, String t) {
if (s == t) return true;
if ((s != null) && (t != null)) {
return s.equals(t);
}
return false;
}
public static String toClassName(String s) {
return toMixedCaseName(toWordList(s), true);
}
public static String toVariableName(String s) {
return toMixedCaseName(toWordList(s), false);
}
public static String toMethodName(String s) {
String m = toMixedCaseName(toWordList(s), false);
if(JJavaName.isJavaIdentifier(m))
return m;
else
return '_'+m;
}
public static String toInterfaceName( String token ) {
return toClassName(token);
}
public static String toPropertyName(String s) {
return toClassName(s);
}
public static String toPackageName( String s ) {
return toMixedCaseName(toWordList(s), false );
}
}
txw2-0.1+20070624/src/compiler/com/sun/tools/txw2/RELAXNGLoader.java 0000664 0000000 0000000 00000003675 11571421112 0024306 0 ustar 00root root 0000000 0000000 /*
* The contents of this file are subject to the terms
* of the Common Development and Distribution License
* (the "License"). You may not use this file except
* in compliance with the License.
*
* You can obtain a copy of the license at
* https://jwsdp.dev.java.net/CDDLv1.0.html
* See the License for the specific language governing
* permissions and limitations under the License.
*
* When distributing Covered Code, include this CDDL
* HEADER in each file and include the License file at
* https://jwsdp.dev.java.net/CDDLv1.0.html If applicable,
* add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your
* own identifying information: Portions Copyright [yyyy]
* [name of copyright owner]
*/
package com.sun.tools.txw2;
import org.kohsuke.rngom.parse.Parseable;
import org.kohsuke.rngom.parse.IllegalSchemaException;
import org.kohsuke.rngom.ast.util.CheckingSchemaBuilder;
import org.kohsuke.rngom.dt.CascadingDatatypeLibraryFactory;
import org.kohsuke.rngom.dt.builtin.BuiltinDatatypeLibraryFactory;
import org.relaxng.datatype.helpers.DatatypeLibraryLoader;
import com.sun.tools.txw2.model.NodeSet;
import com.sun.tools.txw2.model.Leaf;
import com.sun.tools.txw2.builder.relaxng.SchemaBuilderImpl;
/**
* @author Kohsuke Kawaguchi
*/
class RELAXNGLoader implements SchemaBuilder {
private final Parseable parseable;
public RELAXNGLoader(Parseable parseable) {
this.parseable = parseable;
}
public NodeSet build(TxwOptions options) throws IllegalSchemaException {
SchemaBuilderImpl stage1 = new SchemaBuilderImpl(options.codeModel);
Leaf pattern = (Leaf)parseable.parse(new CheckingSchemaBuilder(stage1,options.errorListener,
new CascadingDatatypeLibraryFactory(
new BuiltinDatatypeLibraryFactory(new DatatypeLibraryLoader()),
new DatatypeLibraryLoader())));
return new NodeSet(options,pattern);
}
}
txw2-0.1+20070624/src/compiler/com/sun/tools/txw2/SchemaBuilder.java 0000664 0000000 0000000 00000002164 11571421112 0024556 0 ustar 00root root 0000000 0000000 /*
* The contents of this file are subject to the terms
* of the Common Development and Distribution License
* (the "License"). You may not use this file except
* in compliance with the License.
*
* You can obtain a copy of the license at
* https://jwsdp.dev.java.net/CDDLv1.0.html
* See the License for the specific language governing
* permissions and limitations under the License.
*
* When distributing Covered Code, include this CDDL
* HEADER in each file and include the License file at
* https://jwsdp.dev.java.net/CDDLv1.0.html If applicable,
* add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your
* own identifying information: Portions Copyright [yyyy]
* [name of copyright owner]
*/
package com.sun.tools.txw2;
import com.sun.tools.txw2.model.NodeSet;
import org.kohsuke.rngom.parse.IllegalSchemaException;
import org.xml.sax.SAXException;
/**
* Encapsulation of the schema file and the builder.
*
* @author Kohsuke Kawaguchi
*/
public interface SchemaBuilder {
NodeSet build(TxwOptions options) throws IllegalSchemaException, SAXException;
}
txw2-0.1+20070624/src/compiler/com/sun/tools/txw2/TxwOptions.java 0000664 0000000 0000000 00000003712 11571421112 0024205 0 ustar 00root root 0000000 0000000 /*
* The contents of this file are subject to the terms
* of the Common Development and Distribution License
* (the "License"). You may not use this file except
* in compliance with the License.
*
* You can obtain a copy of the license at
* https://jwsdp.dev.java.net/CDDLv1.0.html
* See the License for the specific language governing
* permissions and limitations under the License.
*
* When distributing Covered Code, include this CDDL
* HEADER in each file and include the License file at
* https://jwsdp.dev.java.net/CDDLv1.0.html If applicable,
* add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your
* own identifying information: Portions Copyright [yyyy]
* [name of copyright owner]
*/
package com.sun.tools.txw2;
import com.sun.codemodel.CodeWriter;
import com.sun.codemodel.JCodeModel;
import com.sun.codemodel.JPackage;
import com.sun.xml.txw2.annotation.XmlNamespace;
/**
* Controls the various aspects of the TXW generation.
*
* But this doesn't contain options for the command-line interface
* nor any of the driver-level configuration (such as where to place
* the generated source code, etc.)
*
* @author Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
*/
public class TxwOptions {
public final JCodeModel codeModel = new JCodeModel();
/**
* The package to put the generated code into.
*/
public JPackage _package;
/**
* Always non-null.
*/
public ErrorListener errorListener;
/**
* The generated code will be sent to this.
*/
CodeWriter codeWriter;
/**
* Schema file.
*/
SchemaBuilder source;
/**
* If true, generate attribute/value methods that
* returns the this object for chaining.
*/
public boolean chainMethod;
/**
* If true, the generated code will not use the package-level
* {@link XmlNamespace} annotation.
*/
public boolean noPackageNamespace;
}
txw2-0.1+20070624/src/compiler/com/sun/tools/txw2/TxwTask.java 0000664 0000000 0000000 00000011047 11571421112 0023454 0 ustar 00root root 0000000 0000000 /*
* The contents of this file are subject to the terms
* of the Common Development and Distribution License
* (the "License"). You may not use this file except
* in compliance with the License.
*
* You can obtain a copy of the license at
* https://jwsdp.dev.java.net/CDDLv1.0.html
* See the License for the specific language governing
* permissions and limitations under the License.
*
* When distributing Covered Code, include this CDDL
* HEADER in each file and include the License file at
* https://jwsdp.dev.java.net/CDDLv1.0.html If applicable,
* add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your
* own identifying information: Portions Copyright [yyyy]
* [name of copyright owner]
*/
package com.sun.tools.txw2;
import com.sun.codemodel.writer.FileCodeWriter;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.kohsuke.rngom.parse.compact.CompactParseable;
import org.kohsuke.rngom.parse.xml.SAXParseable;
import org.xml.sax.InputSource;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
/**
* Ant task interface for txw compiler.
*
* @author ryan_shoemaker@dev.java.net
*/
public class TxwTask extends org.apache.tools.ant.Task {
// txw options - reuse command line options from the main driver
private final TxwOptions options = new TxwOptions();
// schema file
private File schemaFile;
// syntax style of RELAX NG source schema - "xml" or "compact"
private static enum Style {
COMPACT, XML, XMLSCHEMA, AUTO_DETECT
}
private Style style = Style.AUTO_DETECT;
public TxwTask() {
// default package
options._package = options.codeModel.rootPackage();
// default codewriter
try {
options.codeWriter = new FileCodeWriter(new File("."));
} catch (IOException e) {
throw new BuildException(e);
}
}
/**
* Parse @package
*
* @param pkg name of the package to generate the java classes into
*/
public void setPackage( String pkg ) {
options._package = options.codeModel._package( pkg );
}
/**
* Parse @syntax
*
* @param style either "compact" for RELAX NG compact syntax or "XML"
* for RELAX NG xml syntax
*/
public void setSyntax( String style ) {
this.style = Style.valueOf(style.toUpperCase());
}
/**
* parse @schema
*
* @param schema the schema file to be processed by txw
*/
public void setSchema( File schema ) {
schemaFile = schema;
}
/**
* parse @destdir
*
* @param dir the directory to produce generated source code in
*/
public void setDestdir( File dir ) {
try {
options.codeWriter = new FileCodeWriter(dir);
} catch (IOException e) {
throw new BuildException(e);
}
}
/**
* parse @methodChaining
*
* @param flg true if the txw should generate api's that allow
* method chaining (when possible, false otherwise
*/
public void setMethodChaining( boolean flg ) {
options.chainMethod = flg;
}
/**
* launch txw
*/
public void execute() throws BuildException {
options.errorListener = new AntErrorListener(getProject());
try {
InputSource in = new InputSource(schemaFile.toURL().toExternalForm());
String msg = "Compiling: " + in.getSystemId();
log( msg, Project.MSG_INFO );
if(style==Style.AUTO_DETECT) {
String fileName = schemaFile.getPath().toLowerCase();
if(fileName.endsWith("rnc"))
style = Style.COMPACT;
else
if(fileName.endsWith("xsd"))
style = Style.XMLSCHEMA;
else
style = Style.XML;
}
switch(style) {
case COMPACT:
options.source = new RELAXNGLoader(new CompactParseable(in,options.errorListener));
break;
case XML:
options.source = new RELAXNGLoader(new SAXParseable(in,options.errorListener));
break;
case XMLSCHEMA:
options.source = new XmlSchemaLoader(in);
break;
}
} catch (MalformedURLException e) {
throw new BuildException(e);
}
// kick off the compiler
Main.run(options);
log( "Compilation complete.", Project.MSG_INFO );
}
}
txw2-0.1+20070624/src/compiler/com/sun/tools/txw2/XmlSchemaLoader.java 0000664 0000000 0000000 00000002577 11571421112 0025067 0 ustar 00root root 0000000 0000000 /*
* The contents of this file are subject to the terms
* of the Common Development and Distribution License
* (the "License"). You may not use this file except
* in compliance with the License.
*
* You can obtain a copy of the license at
* https://jwsdp.dev.java.net/CDDLv1.0.html
* See the License for the specific language governing
* permissions and limitations under the License.
*
* When distributing Covered Code, include this CDDL
* HEADER in each file and include the License file at
* https://jwsdp.dev.java.net/CDDLv1.0.html If applicable,
* add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your
* own identifying information: Portions Copyright [yyyy]
* [name of copyright owner]
*/
package com.sun.tools.txw2;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import com.sun.tools.txw2.model.NodeSet;
import com.sun.tools.txw2.builder.xsd.XmlSchemaBuilder;
import com.sun.xml.xsom.parser.XSOMParser;
/**
* @author Kohsuke Kawaguchi
*/
class XmlSchemaLoader implements SchemaBuilder {
private final InputSource in;
public XmlSchemaLoader(InputSource in) {
this.in = in;
}
public NodeSet build(TxwOptions options) throws SAXException {
XSOMParser xsom = new XSOMParser();
xsom.parse(in);
return XmlSchemaBuilder.build(xsom.getResult(),options);
}
}
txw2-0.1+20070624/src/compiler/com/sun/tools/txw2/builder/ 0000775 0000000 0000000 00000000000 11571421112 0022627 5 ustar 00root root 0000000 0000000 txw2-0.1+20070624/src/compiler/com/sun/tools/txw2/builder/relaxng/ 0000775 0000000 0000000 00000000000 11571421112 0024267 5 ustar 00root root 0000000 0000000 txw2-0.1+20070624/src/compiler/com/sun/tools/txw2/builder/relaxng/AnnotationsImpl.java 0000664 0000000 0000000 00000003071 11571421112 0030252 0 ustar 00root root 0000000 0000000 /*
* The contents of this file are subject to the terms
* of the Common Development and Distribution License
* (the "License"). You may not use this file except
* in compliance with the License.
*
* You can obtain a copy of the license at
* https://jwsdp.dev.java.net/CDDLv1.0.html
* See the License for the specific language governing
* permissions and limitations under the License.
*
* When distributing Covered Code, include this CDDL
* HEADER in each file and include the License file at
* https://jwsdp.dev.java.net/CDDLv1.0.html If applicable,
* add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your
* own identifying information: Portions Copyright [yyyy]
* [name of copyright owner]
*/
package com.sun.tools.txw2.builder.relaxng;
import org.kohsuke.rngom.ast.builder.Annotations;
import org.kohsuke.rngom.ast.builder.BuildException;
import org.kohsuke.rngom.ast.om.ParsedElementAnnotation;
import org.kohsuke.rngom.ast.util.LocatorImpl;
/**
* @author Kohsuke Kawaguchi
*/
final class AnnotationsImpl implements Annotations