use of org.apache.xml.utils.XMLString in project robovm by robovm.
the class FuncDocument method execute.
/**
* Execute the function. The function must return
* a valid object.
* @param xctxt The current execution context.
* @return A valid XObject.
*
* @throws javax.xml.transform.TransformerException
*/
public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException {
int context = xctxt.getCurrentNode();
DTM dtm = xctxt.getDTM(context);
int docContext = dtm.getDocumentRoot(context);
XObject arg = (XObject) this.getArg0().execute(xctxt);
String base = "";
Expression arg1Expr = this.getArg1();
if (null != arg1Expr) {
// The URI reference may be relative. The base URI (see [3.2 Base URI])
// of the node in the second argument node-set that is first in document
// order is used as the base URI for resolving the
// relative URI into an absolute URI.
XObject arg2 = arg1Expr.execute(xctxt);
if (XObject.CLASS_NODESET == arg2.getType()) {
int baseNode = arg2.iter().nextNode();
if (baseNode == DTM.NULL) {
// See http://www.w3.org/1999/11/REC-xslt-19991116-errata#E14.
// If the second argument is an empty nodeset, this is an error.
// The processor can recover by returning an empty nodeset.
warn(xctxt, XSLTErrorResources.WG_EMPTY_SECOND_ARG, null);
XNodeSet nodes = new XNodeSet(xctxt.getDTMManager());
return nodes;
} else {
DTM baseDTM = xctxt.getDTM(baseNode);
base = baseDTM.getDocumentBaseURI();
}
// %REVIEW% This doesn't seem to be a problem with the conformance
// suite, but maybe it's just not doing a good test?
// int baseDoc = baseDTM.getDocument();
//
// if (baseDoc == DTM.NULL /* || baseDoc instanceof Stylesheet -->What to do?? */)
// {
//
// // base = ((Stylesheet)baseDoc).getBaseIdentifier();
// base = xctxt.getNamespaceContext().getBaseIdentifier();
// }
// else
// base = xctxt.getSourceTreeManager().findURIFromDoc(baseDoc);
} else {
//Can not convert other type to a node-set!;
arg2.iter();
}
} else {
// If the second argument is omitted, then it defaults to
// the node in the stylesheet that contains the expression that
// includes the call to the document function. Note that a
// zero-length URI reference is a reference to the document
// relative to which the URI reference is being resolved; thus
// document("") refers to the root node of the stylesheet;
// the tree representation of the stylesheet is exactly
// the same as if the XML document containing the stylesheet
// was the initial source document.
assertion(null != xctxt.getNamespaceContext(), "Namespace context can not be null!");
base = xctxt.getNamespaceContext().getBaseIdentifier();
}
XNodeSet nodes = new XNodeSet(xctxt.getDTMManager());
NodeSetDTM mnl = nodes.mutableNodeset();
DTMIterator iterator = (XObject.CLASS_NODESET == arg.getType()) ? arg.iter() : null;
int pos = DTM.NULL;
while ((null == iterator) || (DTM.NULL != (pos = iterator.nextNode()))) {
XMLString ref = (null != iterator) ? xctxt.getDTM(pos).getStringValue(pos) : arg.xstr();
// member.
if (null == arg1Expr && DTM.NULL != pos) {
DTM baseDTM = xctxt.getDTM(pos);
base = baseDTM.getDocumentBaseURI();
}
if (null == ref)
continue;
if (DTM.NULL == docContext) {
//"context does not have an owner document!");
error(xctxt, XSLTErrorResources.ER_NO_CONTEXT_OWNERDOC, null);
}
// From http://www.ics.uci.edu/pub/ietf/uri/rfc1630.txt
// A partial form can be distinguished from an absolute form in that the
// latter must have a colon and that colon must occur before any slash
// characters. Systems not requiring partial forms should not use any
// unencoded slashes in their naming schemes. If they do, absolute URIs
// will still work, but confusion may result.
int indexOfColon = ref.indexOf(':');
int indexOfSlash = ref.indexOf('/');
if ((indexOfColon != -1) && (indexOfSlash != -1) && (indexOfColon < indexOfSlash)) {
// The url (or filename, for that matter) is absolute.
base = null;
}
int newDoc = getDoc(xctxt, context, ref.toString(), base);
// nodes.mutableNodeset().addNode(newDoc);
if (DTM.NULL != newDoc) {
// TODO: mnl.addNodeInDocOrder(newDoc, true, xctxt); ??
if (!mnl.contains(newDoc)) {
mnl.addElement(newDoc);
}
}
if (null == iterator || newDoc == DTM.NULL)
break;
}
return nodes;
}
use of org.apache.xml.utils.XMLString in project robovm by robovm.
the class DOM2DTM method dispatchCharactersEvents.
/**
* Directly call the
* characters method on the passed ContentHandler for the
* string-value of the given node (see http://www.w3.org/TR/xpath#data-model
* for the definition of a node's string-value). Multiple calls to the
* ContentHandler's characters methods may well occur for a single call to
* this method.
*
* @param nodeHandle The node ID.
* @param ch A non-null reference to a ContentHandler.
*
* @throws org.xml.sax.SAXException
*/
public void dispatchCharactersEvents(int nodeHandle, org.xml.sax.ContentHandler ch, boolean normalize) throws org.xml.sax.SAXException {
if (normalize) {
XMLString str = getStringValue(nodeHandle);
str = str.fixWhiteSpace(true, true, false);
str.dispatchCharactersEvents(ch);
} else {
int type = getNodeType(nodeHandle);
Node node = getNode(nodeHandle);
dispatchNodeData(node, ch, 0);
// DOM nodes.
if (TEXT_NODE == type || CDATA_SECTION_NODE == type) {
while (null != (node = logicalNextDOMTextNode(node))) {
dispatchNodeData(node, ch, 0);
}
}
}
}
use of org.apache.xml.utils.XMLString in project robovm by robovm.
the class DTMDocumentImpl method getStringValue.
/**
* Get the string-value of a node as a String object
* (see http://www.w3.org/TR/xpath#data-model
* for the definition of a node's string-value).
*
* @param nodeHandle The node ID.
*
* @return A string object that represents the string-value of the given node.
*/
public XMLString getStringValue(int nodeHandle) {
// ###zaj - researching
nodes.readSlot(nodeHandle, gotslot);
int nodetype = gotslot[0] & 0xFF;
String value = null;
switch(nodetype) {
case TEXT_NODE:
case COMMENT_NODE:
case CDATA_SECTION_NODE:
value = m_char.getString(gotslot[2], gotslot[3]);
break;
case PROCESSING_INSTRUCTION_NODE:
case ATTRIBUTE_NODE:
case ELEMENT_NODE:
case ENTITY_REFERENCE_NODE:
default:
break;
}
return m_xsf.newstr(value);
}
use of org.apache.xml.utils.XMLString in project robovm by robovm.
the class DTMTreeWalker method startNode.
/**
* Start processing given node
*
*
* @param node Node to process
*
* @throws org.xml.sax.SAXException
*/
protected void startNode(int node) throws org.xml.sax.SAXException {
if (m_contentHandler instanceof NodeConsumer) {
// %TBD%
// ((NodeConsumer) m_contentHandler).setOriginatingNode(node);
}
switch(m_dtm.getNodeType(node)) {
case DTM.COMMENT_NODE:
{
XMLString data = m_dtm.getStringValue(node);
if (m_contentHandler instanceof LexicalHandler) {
LexicalHandler lh = ((LexicalHandler) this.m_contentHandler);
data.dispatchAsComment(lh);
}
}
break;
case DTM.DOCUMENT_FRAGMENT_NODE:
// ??;
break;
case DTM.DOCUMENT_NODE:
this.m_contentHandler.startDocument();
break;
case DTM.ELEMENT_NODE:
DTM dtm = m_dtm;
for (int nsn = dtm.getFirstNamespaceNode(node, true); DTM.NULL != nsn; nsn = dtm.getNextNamespaceNode(node, nsn, true)) {
// String prefix = dtm.getPrefix(nsn);
String prefix = dtm.getNodeNameX(nsn);
this.m_contentHandler.startPrefixMapping(prefix, dtm.getNodeValue(nsn));
}
// System.out.println("m_dh.getNamespaceOfNode(node): "+m_dh.getNamespaceOfNode(node));
// System.out.println("m_dh.getLocalNameOfNode(node): "+m_dh.getLocalNameOfNode(node));
String ns = dtm.getNamespaceURI(node);
if (null == ns)
ns = "";
// %OPT% !!
org.xml.sax.helpers.AttributesImpl attrs = new org.xml.sax.helpers.AttributesImpl();
for (int i = dtm.getFirstAttribute(node); i != DTM.NULL; i = dtm.getNextAttribute(i)) {
attrs.addAttribute(dtm.getNamespaceURI(i), dtm.getLocalName(i), dtm.getNodeName(i), "CDATA", dtm.getNodeValue(i));
}
this.m_contentHandler.startElement(ns, m_dtm.getLocalName(node), m_dtm.getNodeName(node), attrs);
break;
case DTM.PROCESSING_INSTRUCTION_NODE:
{
String name = m_dtm.getNodeName(node);
// String data = pi.getData();
if (name.equals("xslt-next-is-raw")) {
nextIsRaw = true;
} else {
this.m_contentHandler.processingInstruction(name, m_dtm.getNodeValue(node));
}
}
break;
case DTM.CDATA_SECTION_NODE:
{
boolean isLexH = (m_contentHandler instanceof LexicalHandler);
LexicalHandler lh = isLexH ? ((LexicalHandler) this.m_contentHandler) : null;
if (isLexH) {
lh.startCDATA();
}
dispatachChars(node);
{
if (isLexH) {
lh.endCDATA();
}
}
}
break;
case DTM.TEXT_NODE:
{
if (nextIsRaw) {
nextIsRaw = false;
m_contentHandler.processingInstruction(javax.xml.transform.Result.PI_DISABLE_OUTPUT_ESCAPING, "");
dispatachChars(node);
m_contentHandler.processingInstruction(javax.xml.transform.Result.PI_ENABLE_OUTPUT_ESCAPING, "");
} else {
dispatachChars(node);
}
}
break;
case DTM.ENTITY_REFERENCE_NODE:
{
if (m_contentHandler instanceof LexicalHandler) {
((LexicalHandler) this.m_contentHandler).startEntity(m_dtm.getNodeName(node));
} else {
// warning("Can not output entity to a pure SAX ContentHandler");
}
}
break;
default:
}
}
use of org.apache.xml.utils.XMLString in project robovm by robovm.
the class ClonerToResultTree method cloneToResultTree.
// /**
// * Clone an element with or without children.
// * TODO: Fix or figure out node clone failure!
// * the error condition is severe enough to halt processing.
// *
// * @param node The node to clone
// * @param shouldCloneAttributes Flag indicating whether to
// * clone children attributes
// *
// * @throws TransformerException
// */
// public void cloneToResultTree(int node, boolean shouldCloneAttributes)
// throws TransformerException
// {
//
// try
// {
// XPathContext xctxt = m_transformer.getXPathContext();
// DTM dtm = xctxt.getDTM(node);
//
// int type = dtm.getNodeType(node);
// switch (type)
// {
// case DTM.TEXT_NODE :
// dtm.dispatchCharactersEvents(node, m_rth, false);
// break;
// case DTM.DOCUMENT_FRAGMENT_NODE :
// case DTM.DOCUMENT_NODE :
//
// // Can't clone a document, but refrain from throwing an error
// // so that copy-of will work
// break;
// case DTM.ELEMENT_NODE :
// {
// Attributes atts;
//
// if (shouldCloneAttributes)
// {
// m_rth.addAttributes(node);
// m_rth.processNSDecls(node, type, dtm);
// }
//
// String ns = dtm.getNamespaceURI(node);
// String localName = dtm.getLocalName(node);
//
// m_rth.startElement(ns, localName, dtm.getNodeNameX(node), null);
// }
// break;
// case DTM.CDATA_SECTION_NODE :
// m_rth.startCDATA();
// dtm.dispatchCharactersEvents(node, m_rth, false);
// m_rth.endCDATA();
// break;
// case DTM.ATTRIBUTE_NODE :
// m_rth.addAttribute(node);
// break;
// case DTM.COMMENT_NODE :
// XMLString xstr = dtm.getStringValue (node);
// xstr.dispatchAsComment(m_rth);
// break;
// case DTM.ENTITY_REFERENCE_NODE :
// m_rth.entityReference(dtm.getNodeNameX(node));
// break;
// case DTM.PROCESSING_INSTRUCTION_NODE :
// {
// // %REVIEW% Is the node name the same as the "target"?
// m_rth.processingInstruction(dtm.getNodeNameX(node),
// dtm.getNodeValue(node));
// }
// break;
// default :
// //"Can not create item in result tree: "+node.getNodeName());
// m_transformer.getMsgMgr().error(null,
// XSLTErrorResources.ER_CANT_CREATE_ITEM,
// new Object[]{ dtm.getNodeName(node) });
// }
// }
// catch(org.xml.sax.SAXException se)
// {
// throw new TransformerException(se);
// }
// } // end cloneToResultTree function
/**
* Clone an element with or without children.
* TODO: Fix or figure out node clone failure!
* the error condition is severe enough to halt processing.
*
* @param node The node to clone
* @param shouldCloneAttributes Flag indicating whether to
* clone children attributes
*
* @throws TransformerException
*/
public static void cloneToResultTree(int node, int nodeType, DTM dtm, SerializationHandler rth, boolean shouldCloneAttributes) throws TransformerException {
try {
switch(nodeType) {
case DTM.TEXT_NODE:
dtm.dispatchCharactersEvents(node, rth, false);
break;
case DTM.DOCUMENT_FRAGMENT_NODE:
case DTM.DOCUMENT_NODE:
// so that copy-of will work
break;
case DTM.ELEMENT_NODE:
{
// Note: SAX apparently expects "no namespace" to be
// represented as "" rather than null.
String ns = dtm.getNamespaceURI(node);
if (ns == null)
ns = "";
String localName = dtm.getLocalName(node);
// rth.startElement(ns, localName, dtm.getNodeNameX(node), null);
// don't call a real SAX startElement (as commented out above),
// call a SAX-like startElement, to be able to add attributes after this call
rth.startElement(ns, localName, dtm.getNodeNameX(node));
// xsl:attribute directive.)
if (shouldCloneAttributes) {
SerializerUtils.addAttributes(rth, node);
SerializerUtils.processNSDecls(rth, node, nodeType, dtm);
}
}
break;
case DTM.CDATA_SECTION_NODE:
rth.startCDATA();
dtm.dispatchCharactersEvents(node, rth, false);
rth.endCDATA();
break;
case DTM.ATTRIBUTE_NODE:
SerializerUtils.addAttribute(rth, node);
break;
case DTM.NAMESPACE_NODE:
// %REVIEW% Normally, these should have been handled with element.
// It's possible that someone may write a stylesheet that tries to
// clone them explicitly. If so, we need the equivalent of
// rth.addAttribute().
SerializerUtils.processNSDecls(rth, node, DTM.NAMESPACE_NODE, dtm);
break;
case DTM.COMMENT_NODE:
XMLString xstr = dtm.getStringValue(node);
xstr.dispatchAsComment(rth);
break;
case DTM.ENTITY_REFERENCE_NODE:
rth.entityReference(dtm.getNodeNameX(node));
break;
case DTM.PROCESSING_INSTRUCTION_NODE:
{
// %REVIEW% Is the node name the same as the "target"?
rth.processingInstruction(dtm.getNodeNameX(node), dtm.getNodeValue(node));
}
break;
default:
//"Can not create item in result tree: "+node.getNodeName());
throw new TransformerException("Can't clone node: " + dtm.getNodeName(node));
}
} catch (org.xml.sax.SAXException se) {
throw new TransformerException(se);
}
}
Aggregations