svn commit: r617035 [22/22] - in /lenya/branches/revolution/1.3.x: ./ src/java/org/apache/lenya/ac/ src/java/org/apache/lenya/ac/cache/ src/java/org/apache/lenya/ac/cifs/ src/java/org/apache/lenya/ac/file/ src/java/org/apache/lenya/ac/impl/ src/java/or...
[email protected] Thu, 31 Jan 2008 07:44:36 -0000
Newsgroups
gmane.comp.cms.lenya.cvs
Message-ID
<[email protected] >
Modified: lenya/branches/revolution/1.3.x/src/java/org/apache/lenya/xml/XPSAssembler.java
URL: http://svn.apache.org/viewvc/lenya/branches/revolution/1.3.x/src/java/org/apache/lenya/xml/XPSAssembler.java?rev=617035&r1=617034&r2=617035&view=diff
==============================================================================
--- lenya/branches/revolution/1.3.x/src/java/org/apache/lenya/xml/XPSAssembler.java (original)
+++ lenya/branches/revolution/1.3.x/src/java/org/apache/lenya/xml/XPSAssembler.java Wed Jan 30 23:44:03 2008
@@ -14,12 +14,8 @@
* limitations under the License.
*
*/
-
/* $Id$ */
-
package org.apache.lenya.xml;
-
-
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
@@ -29,606 +25,519 @@
import java.util.Properties;
import java.util.StringTokenizer;
import java.util.Vector;
-
import org.apache.lenya.net.ProxyManager;
-import org.apache.log4j.Category;
+import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
-
-
/**
* XLink/XInclude Processor (Nesting, Caching, Java, Exceptions)
*/
public class XPSAssembler implements XPSInclude {
- static Category log = Category.getInstance(XPSAssembler.class);
- DOMParserFactory dpf = null;
- XPointerFactory xpf = null;
- Configuration conf = null;
- ProxyManager pm = null;
- String XPSEXCEPTION_ELEMENT_NAME = "XPSEXCEPTION"; // better would be a namespace xps:XLinkException
-
- /**
- * Creates a new XPSAssembler object.
- */
- public XPSAssembler() {
- dpf = new DOMParserFactory();
- xpf = new XPointerFactory();
- conf = new Configuration();
- pm = new ProxyManager();
-
- if ((conf.proxyHost != null) && (conf.proxyPort != null)) {
- Properties sp = System.getProperties();
- sp.put("proxySet", "true");
- sp.put("proxyHost", conf.proxyHost);
- sp.put("proxyPort", conf.proxyPort);
- }
- }
-
- /**
- * Creates a new XPSAssembler object.
- *
- * @param includeoption DOCUMENT ME!
- */
- public XPSAssembler(String includeoption) {
- this();
- conf.INCLUDE = includeoption;
- }
-
- /**
- * Usage of XPSAssembler
- *
- * @param args URI
- */
- public static void main(String[] args) {
- XPSAssembler xpsa = new XPSAssembler();
-
- if (args.length != 1) {
- System.err.println("Usage:");
- System.err.println(" java " + xpsa.getClass().getName() + " \"../x/xps/samples/tbs/xml/invoices/invoice.xml\"");
- System.err.println(" java " + xpsa.getClass().getName() + " \"file:/...\"");
- System.err.println(" java " + xpsa.getClass().getName() + " \"http://localhost/...\"");
-
- return;
- }
-
- String cocoon = null; // e.g. http://127.0.0.1:8080/lenya/nwt
- Document document = xpsa.assemble(args[0], cocoon);
-
- try {
- OutputStream out = System.out;
- new DOMWriter(out, "utf-8").printWithoutFormatting(document);
-
- System.out.print("\n");
- out.flush();
- } catch (Exception e) {
- System.err.println(xpsa.getClass().getName() + ".main(): " + e);
- }
- }
-
- /**
- * DOCUMENT ME!
- *
- * @param reference DOCUMENT ME!
- * @param cocoon DOCUMENT ME!
- *
- * @return DOCUMENT ME!
- */
- public Document assemble(String reference, String cocoon) {
- File workingDirectory = new File(System.getProperty("user.dir"));
- XPSSourceInformation sourceInfo = new XPSSourceInformation("file:" + workingDirectory +
- "/dummy.xml", cocoon);
- String[] args = new String[1];
- args[0] = reference;
-
- XPSSourceInformation currentInfo = new XPSSourceInformation(args[0], sourceInfo, cocoon);
- deleteFromCache(currentInfo.url);
-
- Vector nodes = include(args, sourceInfo);
- log.debug(sourceInfo);
-
- Node node = (Node) nodes.elementAt(0);
-
- return node.getOwnerDocument();
- }
-
- /**
- * Remove file from cache
- *
- * @param url DOCUMENT ME!
- */
- public void deleteFromCache(URL url) {
- if (conf.cacheFolder != null) {
- File cacheFile = getCacheFile(url);
-
- if (cacheFile.isFile()) {
- log.info(".deleteFromCache(): " + cacheFile.getAbsolutePath());
- cacheFile.delete();
- } else {
- log.warn(".deleteFromCache(): No such file in cache: " +
- cacheFile.getAbsolutePath());
- }
- }
- }
-
- /**
- * DOCUMENT ME!
- *
- * @param document DOCUMENT ME!
- * @param reference DOCUMENT ME!
- * @param cocoon DOCUMENT ME!
- *
- * @return DOCUMENT ME!
- */
- public Document assemble(Document document, String reference, String cocoon) {
- //return document;
- Element root = document.getDocumentElement();
- Document assembledDocument = dpf.getDocument();
- Element assembledRoot = (Element) dpf.cloneNode(assembledDocument, root, false);
- assembledDocument.appendChild(assembledRoot);
-
- File workingDirectory = new File(System.getProperty("user.dir"));
- XPSSourceInformation sourceInfo = new XPSSourceInformation("file:" + workingDirectory + "/dummy.xml", cocoon);
- XPSSourceInformation currentInfo = new XPSSourceInformation(reference, sourceInfo, cocoon);
- NodeList nl = root.getChildNodes();
-
- for (int i = 0; i < nl.getLength(); i++) {
- traverse(assembledRoot, nl.item(i), sourceInfo, currentInfo);
- }
-
- return assembledDocument;
- }
-
- /**
- * DOCUMENT ME!
- *
- * @param currentInfo DOCUMENT ME!
- *
- * @return DOCUMENT ME!
- *
- * @throws Exception DOCUMENT ME!
- */
- public InputStream readXML(XPSSourceInformation currentInfo)
- throws Exception {
- InputStream is = null;
- File cacheFile = null;
- long originalFileLastModified = 0;
-
- String protocol = currentInfo.url.getProtocol();
-
- URL url = null;
-
- if (conf.cacheFolder != null) // Check cache
- {
- cacheFile = getCacheFile(currentInfo.url);
-
- if (protocol.equals("file")) {
- File originalFile = new File(currentInfo.url.getFile());
- originalFileLastModified = originalFile.lastModified();
- } else if (protocol.equals("http")) {
- pm.set(currentInfo.url.getHost()); // install proxy if necessary
- originalFileLastModified = currentInfo.url.openConnection().getLastModified();
- } else {
- log.error("No such protocol: " + protocol);
- }
-
- if (cacheFile.isFile() && (cacheFile.lastModified() >= originalFileLastModified)) {
- // File already exists in cache and is newer than original File
- url = new URL("file:" + cacheFile.getAbsolutePath());
- } else { // File does not exist in cache
- url = new URL(currentInfo.url.toString());
- }
- } else { // No cache folder specified
+ private static Logger log = Logger.getLogger(XPSAssembler.class);
+ DOMParserFactory dpf = null;
+ XPointerFactory xpf = null;
+ Configuration conf = null;
+ ProxyManager pm = null;
+ String XPSEXCEPTION_ELEMENT_NAME = "XPSEXCEPTION"; // better would be a namespace xps:XLinkException
+ /**
+ * Creates a new XPSAssembler object.
+ */
+ public XPSAssembler() {
+ dpf = new DOMParserFactory();
+ xpf = new XPointerFactory();
+ conf = new Configuration();
+ pm = new ProxyManager();
+ if((conf.proxyHost != null) && (conf.proxyPort != null)){
+ Properties sp = System.getProperties();
+ sp.put("proxySet", "true");
+ sp.put("proxyHost", conf.proxyHost);
+ sp.put("proxyPort", conf.proxyPort);
+ }
+ }
+ /**
+ * Creates a new XPSAssembler object.
+ *
+ * @param includeoption
+ * DOCUMENT ME!
+ */
+ public XPSAssembler(String includeoption) {
+ this();
+ conf.INCLUDE = includeoption;
+ }
+ /**
+ * Usage of XPSAssembler
+ *
+ * @param args
+ * URI
+ */
+ public static void main(String[] args) {
+ XPSAssembler xpsa = new XPSAssembler();
+ if(args.length != 1){
+ System.err.println("Usage:");
+ System.err.println(" java " + xpsa.getClass().getName() + " \"../x/xps/samples/tbs/xml/invoices/invoice.xml\"");
+ System.err.println(" java " + xpsa.getClass().getName() + " \"file:/...\"");
+ System.err.println(" java " + xpsa.getClass().getName() + " \"http://localhost/...\"");
+ return;
+ }
+ String cocoon = null; // e.g. http://127.0.0.1:8080/lenya/nwt
+ Document document = xpsa.assemble(args[0], cocoon);
+ try{
+ OutputStream out = System.out;
+ new DOMWriter(out, "utf-8").printWithoutFormatting(document);
+ System.out.print("\n");
+ out.flush();
+ }catch(Exception e){
+ System.err.println(xpsa.getClass().getName() + ".main(): " + e);
+ }
+ }
+ /**
+ * DOCUMENT ME!
+ *
+ * @param reference
+ * DOCUMENT ME!
+ * @param cocoon
+ * DOCUMENT ME!
+ *
+ * @return DOCUMENT ME!
+ */
+ public Document assemble(String reference, String cocoon) {
+ File workingDirectory = new File(System.getProperty("user.dir"));
+ XPSSourceInformation sourceInfo = new XPSSourceInformation("file:" + workingDirectory + "/dummy.xml", cocoon);
+ String[] args = new String[1];
+ args[0] = reference;
+ XPSSourceInformation currentInfo = new XPSSourceInformation(args[0], sourceInfo, cocoon);
+ deleteFromCache(currentInfo.url);
+ Vector nodes = include(args, sourceInfo);
+ log.debug(sourceInfo);
+ Node node = (Node) nodes.elementAt(0);
+ return node.getOwnerDocument();
+ }
+ /**
+ * Remove file from cache
+ *
+ * @param url
+ * DOCUMENT ME!
+ */
+ public void deleteFromCache(URL url) {
+ if(conf.cacheFolder != null){
+ File cacheFile = getCacheFile(url);
+ if(cacheFile.isFile()){
+ log.info(".deleteFromCache(): " + cacheFile.getAbsolutePath());
+ cacheFile.delete();
+ }else{
+ log.warn(".deleteFromCache(): No such file in cache: " + cacheFile.getAbsolutePath());
+ }
+ }
+ }
+ /**
+ * DOCUMENT ME!
+ *
+ * @param document
+ * DOCUMENT ME!
+ * @param reference
+ * DOCUMENT ME!
+ * @param cocoon
+ * DOCUMENT ME!
+ *
+ * @return DOCUMENT ME!
+ */
+ public Document assemble(Document document, String reference, String cocoon) {
+ // return document;
+ Element root = document.getDocumentElement();
+ Document assembledDocument = dpf.getDocument();
+ Element assembledRoot = (Element) dpf.cloneNode(assembledDocument, root, false);
+ assembledDocument.appendChild(assembledRoot);
+ File workingDirectory = new File(System.getProperty("user.dir"));
+ XPSSourceInformation sourceInfo = new XPSSourceInformation("file:" + workingDirectory + "/dummy.xml", cocoon);
+ XPSSourceInformation currentInfo = new XPSSourceInformation(reference, sourceInfo, cocoon);
+ NodeList nl = root.getChildNodes();
+ for(int i = 0; i < nl.getLength(); i++){
+ traverse(assembledRoot, nl.item(i), sourceInfo, currentInfo);
+ }
+ return assembledDocument;
+ }
+ /**
+ * DOCUMENT ME!
+ *
+ * @param currentInfo
+ * DOCUMENT ME!
+ *
+ * @return DOCUMENT ME!
+ *
+ * @throws Exception
+ * DOCUMENT ME!
+ */
+ public InputStream readXML(XPSSourceInformation currentInfo) throws Exception {
+ InputStream is = null;
+ File cacheFile = null;
+ long originalFileLastModified = 0;
+ String protocol = currentInfo.url.getProtocol();
+ URL url = null;
+ if(conf.cacheFolder != null) // Check cache
+ {
+ cacheFile = getCacheFile(currentInfo.url);
+ if(protocol.equals("file")){
+ File originalFile = new File(currentInfo.url.getFile());
+ originalFileLastModified = originalFile.lastModified();
+ }else if(protocol.equals("http")){
+ pm.set(currentInfo.url.getHost()); // install proxy if necessary
+ originalFileLastModified = currentInfo.url.openConnection().getLastModified();
+ }else{
+ log.error("No such protocol: " + protocol);
+ }
+ if(cacheFile.isFile() && (cacheFile.lastModified() >= originalFileLastModified)){
+ // File already exists in cache and is newer than original File
+ url = new URL("file:" + cacheFile.getAbsolutePath());
+ }else{ // File does not exist in cache
url = new URL(currentInfo.url.toString());
- }
-
- // Read Document
- is = url.openStream();
-
- return is;
- }
-
- /**
- * DOCUMENT ME!
- *
- * @param currentInfo DOCUMENT ME!
- * @param newDocument DOCUMENT ME!
- *
- * @return DOCUMENT ME!
- */
- public boolean tryWritingToCache(XPSSourceInformation currentInfo, Document newDocument) {
- File cacheFile = null;
-
- if (conf.cacheFolder != null) // Check cache
- {
- cacheFile = getCacheFile(currentInfo.url);
- }
-
- if (cacheFile != null) {
- String protocol = currentInfo.url.getProtocol();
-
- if (!cacheFile.exists()) { // Write "Xlink" to cache
-
- return writeToCache(protocol, cacheFile, newDocument);
- } else { // cacheFile exists
-
- long originalFileLastModified = 0;
- String p = currentInfo.url.getProtocol();
-
- if (p.equals("file")) {
- File originalFile = new File(currentInfo.url.getFile());
- originalFileLastModified = originalFile.lastModified();
- } else if (p.equals("http")) {
- try {
- originalFileLastModified = currentInfo.url.openConnection().getLastModified();
- } catch (IOException e) {
- log.error("originalFileLastModified: " + e);
- }
- } else {
- log.error("No such protocol: " + p);
- }
-
- if (cacheFile.lastModified() < originalFileLastModified) {
- // File in cache is older than original File
- return writeToCache(protocol, cacheFile, newDocument);
- }
- }
- }
-
- return false;
- }
-
- /**
- * param args args[0]=url
- *
- * @param args DOCUMENT ME!
- * @param sourceInfo DOCUMENT ME!
- *
- * @return DOCUMENT ME!
- */
- public Vector include(String[] args, XPSSourceInformation sourceInfo) {
- XPSSourceInformation currentInfo = new XPSSourceInformation(args[0], sourceInfo, sourceInfo.cocoon);
-
- sourceInfo.addChild(currentInfo);
-
- if (currentInfo.checkLoop(sourceInfo, currentInfo.url)) {
- log.warn("Loop detected: " + sourceInfo.url.getFile() + " " + currentInfo.url.getFile());
- return null;
- }
-
- Document document = null;
- Vector nodes = new Vector();
- Document newDocument = dpf.getDocument();
-
- try {
- InputStream is = readXML(currentInfo);
- document = dpf.getDocument(is);
- } catch (Exception e) {
- log.warn(e + ", currentInfo: " + currentInfo.url.getFile() + " , sourceInfo: " + sourceInfo.url.getFile());
-
- Element newRoot = dpf.newElementNode(newDocument, XPSEXCEPTION_ELEMENT_NAME);
- newRoot.appendChild(dpf.newTextNode(newDocument, "" + e));
- nodes.addElement(newRoot);
-
- return nodes;
- }
-
- Element root = document.getDocumentElement();
- Element newRoot = (Element) dpf.cloneNode(newDocument, root, false);
- newDocument.appendChild(newRoot);
-
- NodeList nl = root.getChildNodes();
-
- for (int i = 0; i < nl.getLength(); i++) {
- traverse(newRoot, nl.item(i), sourceInfo, currentInfo);
- }
-
- boolean writtenToCache = tryWritingToCache(currentInfo, newDocument);
-
- if (currentInfo.url.getRef() == null) {
- log.debug("No XPointer. Return the root node in order to add the whole document.");
- nodes.addElement(newRoot);
- } else {
- log.debug("XPointer: " + currentInfo.url.getRef());
- try {
- nodes = xpf.select(newRoot, currentInfo.url.getRef());
-
- for (int i = 0; i < nodes.size(); i++) {
- short nodeType = ((Node) nodes.elementAt(i)).getNodeType();
-
- switch (nodeType) {
- case Node.ELEMENT_NODE:
- break;
-
- case Node.ATTRIBUTE_NODE: {
- Node attribute = (Node) nodes.elementAt(i);
- nodes.removeElementAt(i);
- nodes.insertElementAt(dpf.newTextNode(attribute.getOwnerDocument(),
- attribute.getNodeValue()), i);
-
- break;
- }
-
- default: {
- log.error(".include(): Node Type (" + nodeType + ") can't be a child of Element");
- nodes.removeElementAt(i);
-
- break;
- }
- }
- }
- } catch (Exception e) {
- log.error("", e);
- }
- }
-
- return nodes;
- }
-
- /**
- * Traverses recursively and looks for XLinks and includes the returned NodeList
- *
- * @param newParent DOCUMENT ME!
- * @param orgChild DOCUMENT ME!
- * @param sourceInfo DOCUMENT ME!
- * @param currentInfo DOCUMENT ME!
- */
- public void traverse(Node newParent, Node orgChild, XPSSourceInformation sourceInfo,
- XPSSourceInformation currentInfo) {
- Vector newChildren = new Vector();
- short nodeType = orgChild.getNodeType();
- boolean noXLink = true;
-
- switch (nodeType) {
- case Node.ELEMENT_NODE: {
+ }
+ }else{ // No cache folder specified
+ url = new URL(currentInfo.url.toString());
+ }
+ // Read Document
+ is = url.openStream();
+ return is;
+ }
+ /**
+ * DOCUMENT ME!
+ *
+ * @param currentInfo
+ * DOCUMENT ME!
+ * @param newDocument
+ * DOCUMENT ME!
+ *
+ * @return DOCUMENT ME!
+ */
+ public boolean tryWritingToCache(XPSSourceInformation currentInfo, Document newDocument) {
+ File cacheFile = null;
+ if(conf.cacheFolder != null) // Check cache
+ {
+ cacheFile = getCacheFile(currentInfo.url);
+ }
+ if(cacheFile != null){
+ String protocol = currentInfo.url.getProtocol();
+ if(!cacheFile.exists()){ // Write "Xlink" to cache
+ return writeToCache(protocol, cacheFile, newDocument);
+ }else{ // cacheFile exists
+ long originalFileLastModified = 0;
+ String p = currentInfo.url.getProtocol();
+ if(p.equals("file")){
+ File originalFile = new File(currentInfo.url.getFile());
+ originalFileLastModified = originalFile.lastModified();
+ }else if(p.equals("http")){
+ try{
+ originalFileLastModified = currentInfo.url.openConnection().getLastModified();
+ }catch(IOException e){
+ log.error("originalFileLastModified: " + e);
+ }
+ }else{
+ log.error("No such protocol: " + p);
+ }
+ if(cacheFile.lastModified() < originalFileLastModified){
+ // File in cache is older than original File
+ return writeToCache(protocol, cacheFile, newDocument);
+ }
+ }
+ }
+ return false;
+ }
+ /**
+ * param args args[0]=url
+ *
+ * @param args
+ * DOCUMENT ME!
+ * @param sourceInfo
+ * DOCUMENT ME!
+ *
+ * @return DOCUMENT ME!
+ */
+ public Vector include(String[] args, XPSSourceInformation sourceInfo) {
+ XPSSourceInformation currentInfo = new XPSSourceInformation(args[0], sourceInfo, sourceInfo.cocoon);
+ sourceInfo.addChild(currentInfo);
+ if(currentInfo.checkLoop(sourceInfo, currentInfo.url)){
+ log.warn("Loop detected: " + sourceInfo.url.getFile() + " " + currentInfo.url.getFile());
+ return null;
+ }
+ Document document = null;
+ Vector nodes = new Vector();
+ Document newDocument = dpf.getDocument();
+ try{
+ InputStream is = readXML(currentInfo);
+ document = dpf.getDocument(is);
+ }catch(Exception e){
+ log.warn(e + ", currentInfo: " + currentInfo.url.getFile() + " , sourceInfo: " + sourceInfo.url.getFile());
+ Element newRoot = dpf.newElementNode(newDocument, XPSEXCEPTION_ELEMENT_NAME);
+ newRoot.appendChild(dpf.newTextNode(newDocument, "" + e));
+ nodes.addElement(newRoot);
+ return nodes;
+ }
+ Element root = document.getDocumentElement();
+ Element newRoot = (Element) dpf.cloneNode(newDocument, root, false);
+ newDocument.appendChild(newRoot);
+ NodeList nl = root.getChildNodes();
+ for(int i = 0; i < nl.getLength(); i++){
+ traverse(newRoot, nl.item(i), sourceInfo, currentInfo);
+ }
+ // boolean writtenToCache =
+ tryWritingToCache(currentInfo, newDocument);
+ if(currentInfo.url.getRef() == null){
+ log.debug("No XPointer. Return the root node in order to add the whole document.");
+ nodes.addElement(newRoot);
+ }else{
+ log.debug("XPointer: " + currentInfo.url.getRef());
+ try{
+ nodes = xpf.select(newRoot, currentInfo.url.getRef());
+ for(int i = 0; i < nodes.size(); i++){
+ short nodeType = ((Node) nodes.elementAt(i)).getNodeType();
+ switch(nodeType){
+ case Node.ELEMENT_NODE:
+ break;
+ case Node.ATTRIBUTE_NODE: {
+ Node attribute = (Node) nodes.elementAt(i);
+ nodes.removeElementAt(i);
+ nodes.insertElementAt(dpf.newTextNode(attribute.getOwnerDocument(), attribute.getNodeValue()), i);
+ break;
+ }
+ default: {
+ log.error(".include(): Node Type (" + nodeType + ") can't be a child of Element");
+ nodes.removeElementAt(i);
+ break;
+ }
+ }
+ }
+ }catch(Exception e){
+ log.error("", e);
+ }
+ }
+ return nodes;
+ }
+ /**
+ * Traverses recursively and looks for XLinks and includes the returned NodeList
+ *
+ * @param newParent
+ * DOCUMENT ME!
+ * @param orgChild
+ * DOCUMENT ME!
+ * @param sourceInfo
+ * DOCUMENT ME!
+ * @param currentInfo
+ * DOCUMENT ME!
+ */
+ public void traverse(Node newParent, Node orgChild, XPSSourceInformation sourceInfo, XPSSourceInformation currentInfo) {
+ Vector newChildren = new Vector();
+ short nodeType = orgChild.getNodeType();
+ boolean noXLink = true;
+ switch(nodeType){
+ case Node.ELEMENT_NODE: {
XLink xlink = new XLink((Element) orgChild);
-
- if (xlink.href == null) {
- Element newElement = (Element) dpf.cloneNode(newParent.getOwnerDocument(), orgChild, false);
- newChildren.addElement(newElement);
- } else {
- noXLink = false;
- log.debug(".traverse(): xlink:href=\"" + xlink.href + "\"");
-
- NodeList nl = processXLink(xlink, (Element) orgChild, currentInfo);
-
- for (int i = 0; i < nl.getLength(); i++) {
- newChildren.addElement(dpf.cloneNode(newParent.getOwnerDocument(), nl.item(i), true));
- }
+ if(xlink.href == null){
+ Element newElement = (Element) dpf.cloneNode(newParent.getOwnerDocument(), orgChild, false);
+ newChildren.addElement(newElement);
+ }else{
+ noXLink = false;
+ log.debug(".traverse(): xlink:href=\"" + xlink.href + "\"");
+ NodeList nl = processXLink(xlink, (Element) orgChild, currentInfo);
+ for(int i = 0; i < nl.getLength(); i++){
+ newChildren.addElement(dpf.cloneNode(newParent.getOwnerDocument(), nl.item(i), true));
+ }
}
-
break;
- }
-
- case Node.COMMENT_NODE: {
+ }
+ case Node.COMMENT_NODE: {
newChildren.addElement(dpf.newCommentNode(newParent.getOwnerDocument(), orgChild.getNodeValue()));
-
break;
- }
-
- case Node.TEXT_NODE: {
+ }
+ case Node.TEXT_NODE: {
newChildren.addElement(dpf.newTextNode(newParent.getOwnerDocument(), orgChild.getNodeValue()));
-
break;
- }
-
- case Node.CDATA_SECTION_NODE: {
+ }
+ case Node.CDATA_SECTION_NODE: {
newChildren.addElement(dpf.newCDATASection(newParent.getOwnerDocument(), orgChild.getNodeValue()));
-
break;
- }
-
- default: {
+ }
+ default: {
log.error(".traverse(): Node type not implemented: " + nodeType + " (" + currentInfo.url + ")");
break;
- }
- }
-
- for (int i = 0; i < newChildren.size(); i++) {
- newParent.appendChild((Node) newChildren.elementAt(i));
- }
-
- if (orgChild.hasChildNodes() && noXLink) {
- NodeList nl = orgChild.getChildNodes();
-
- for (int i = 0; i < nl.getLength(); i++) {
- traverse((Node) newChildren.elementAt(0), nl.item(i), sourceInfo, currentInfo);
+ }
+ }
+ for(int i = 0; i < newChildren.size(); i++){
+ newParent.appendChild((Node) newChildren.elementAt(i));
+ }
+ if(orgChild.hasChildNodes() && noXLink){
+ NodeList nl = orgChild.getChildNodes();
+ for(int i = 0; i < nl.getLength(); i++){
+ traverse((Node) newChildren.elementAt(0), nl.item(i), sourceInfo, currentInfo);
+ }
+ }
+ }
+ /**
+ * Process XLink
+ *
+ * @param xlink
+ * DOCUMENT ME!
+ * @param orgChild
+ * DOCUMENT ME!
+ * @param currentInfo
+ * DOCUMENT ME!
+ *
+ * @return DOCUMENT ME!
+ */
+ public NodeList processXLink(XLink xlink, Element orgChild, XPSSourceInformation currentInfo) {
+ NodeList nl = null;
+ // NOTE: if no show attribute is specified, then the value will be set to "undefined"
+ if(!(xlink.show.equals("embed") || xlink.show.equals("enclose") || xlink.show.equals("replace"))){
+ log.warn("No such value of attribute \"show\" implemented: " + xlink.show);
+ nl = noNodesReturnedFromXLink(xlink);
+ }else{
+ Vector args = new Vector();
+ String includeClassName = includeClassName(xlink.href, args);
+ String[] arguments = new String[args.size()];
+ for(int i = 0; i < args.size(); i++){
+ arguments[i] = (String) args.elementAt(i);
+ log.debug("Arguments: " + arguments[i]);
+ }
+ Vector newChildren = null;
+ try{
+ if(includeClassName.equals(this.getClass().getName())){
+ newChildren = include(arguments, currentInfo);
+ }else{
+ Class includeClass = Class.forName(includeClassName);
+ XPSInclude xpsInclude = (XPSInclude) includeClass.newInstance();
+ newChildren = xpsInclude.include(arguments, currentInfo);
+ }
+ }catch(Exception e){
+ log.error(".processXLink(): " + e);
+ }
+ if(newChildren != null) // Returned nodes from XLink
+ {
+ if(newChildren.size() > 0){
+ Node firstChild = (Node) newChildren.elementAt(0);
+ Document xlinkedDocument = firstChild.getOwnerDocument();
+ Element dummyRoot = dpf.newElementNode(xlinkedDocument, "DummyRoot");
+ if(xlink.show.equals("embed")){
+ // if (conf.INCLUDE.equals("embed")) {
+ // WARNING: embed was actually meant to also include the actual xlink, but
+ // it was never really implemented and hence led to the misinterpretation of replace
+ // Therefore we treat it the same as replace
+ // dummyRoot.appendChild(xlink.getXLink(xlinkedDocument, dpf));
+ for(int i = 0; i < newChildren.size(); i++){
+ dummyRoot.appendChild((Node) newChildren.elementAt(i));
+ }
+ }else if(xlink.show.equals("replace")){
+ // } else if (conf.INCLUDE.equals("replace")) {
+ for(int i = 0; i < newChildren.size(); i++){
+ dummyRoot.appendChild((Node) newChildren.elementAt(i));
+ }
+ }else if(xlink.show.equals("enclose")){
+ // } else if (conf.INCLUDE.equals("enclose")) {
+ Element xlinkCopy = xlink.getXLink(xlinkedDocument, dpf);
+ for(int i = 0; i < newChildren.size(); i++){
+ xlinkCopy.appendChild((Node) newChildren.elementAt(i));
+ }
+ dummyRoot.appendChild(xlinkCopy);
+ }else{
+ log.warn("No such attribute \"show\" or such value of attribute \"show\" implemented");
+ }
+ nl = dummyRoot.getChildNodes();
}
- }
- }
-
- /**
- * Process XLink
- *
- * @param xlink DOCUMENT ME!
- * @param orgChild DOCUMENT ME!
- * @param currentInfo DOCUMENT ME!
- *
- * @return DOCUMENT ME!
- */
- public NodeList processXLink(XLink xlink, Element orgChild, XPSSourceInformation currentInfo) {
- NodeList nl = null;
-
- // NOTE: if no show attribute is specified, then the value will be set to "undefined"
- if (!(xlink.show.equals("embed") || xlink.show.equals("enclose") || xlink.show.equals("replace"))) {
- log.warn("No such value of attribute \"show\" implemented: " + xlink.show);
+ }
+ if(nl == null){
nl = noNodesReturnedFromXLink(xlink);
- } else {
- Vector args = new Vector();
- String includeClassName = includeClassName(xlink.href, args);
- String[] arguments = new String[args.size()];
-
- for (int i = 0; i < args.size(); i++) {
- arguments[i] = (String) args.elementAt(i);
- log.debug("Arguments: " + arguments[i]);
- }
-
- Vector newChildren = null;
-
- try {
- if (includeClassName.equals(this.getClass().getName())) {
- newChildren = include(arguments, currentInfo);
- } else {
- Class includeClass = Class.forName(includeClassName);
- XPSInclude xpsInclude = (XPSInclude) includeClass.newInstance();
- newChildren = xpsInclude.include(arguments, currentInfo);
- }
- } catch (Exception e) {
- log.error(".processXLink(): " + e);
- }
-
- if (newChildren != null) // Returned nodes from XLink
- {
- if (newChildren.size() > 0) {
- Node firstChild = (Node) newChildren.elementAt(0);
- Document xlinkedDocument = firstChild.getOwnerDocument();
- Element dummyRoot = dpf.newElementNode(xlinkedDocument, "DummyRoot");
-
- if (xlink.show.equals("embed")) {
- //if (conf.INCLUDE.equals("embed")) {
- // WARNING: embed was actually meant to also include the actual xlink, but
- // it was never really implemented and hence led to the misinterpretation of replace
- // Therefore we treat it the same as replace
- //dummyRoot.appendChild(xlink.getXLink(xlinkedDocument, dpf));
-
- for (int i = 0; i < newChildren.size(); i++) {
- dummyRoot.appendChild((Node) newChildren.elementAt(i));
- }
- } else if (xlink.show.equals("replace")) {
- //} else if (conf.INCLUDE.equals("replace")) {
- for (int i = 0; i < newChildren.size(); i++) {
- dummyRoot.appendChild((Node) newChildren.elementAt(i));
- }
- } else if (xlink.show.equals("enclose")) {
- //} else if (conf.INCLUDE.equals("enclose")) {
- Element xlinkCopy = xlink.getXLink(xlinkedDocument, dpf);
-
- for (int i = 0; i < newChildren.size(); i++) {
- xlinkCopy.appendChild((Node) newChildren.elementAt(i));
- }
- dummyRoot.appendChild(xlinkCopy);
- } else {
- log.warn("No such attribute \"show\" or such value of attribute \"show\" implemented");
- }
-
- nl = dummyRoot.getChildNodes();
- }
- }
-
- if (nl == null) {
- nl = noNodesReturnedFromXLink(xlink);
- }
-
- if (nl.getLength() == 0) {
- nl = noNodesReturnedFromXLink(xlink);
- }
- }
-
- return nl;
- }
-
- /**
- * DOCUMENT ME!
- *
- * @param xlink DOCUMENT ME!
- *
- * @return DOCUMENT ME!
- */
- public NodeList noNodesReturnedFromXLink(XLink xlink) {
- log.warn("No nodes returned from XLink: " + xlink);
-
- Document dummyDocument = dpf.getDocument();
- Element dummyRoot = dpf.newElementNode(dummyDocument, "DummyRoot");
- Element element = xlink.getXLink(dummyDocument, dpf);
- dummyRoot.appendChild(element);
-
- Element exceptionElement = dpf.newElementNode(dummyDocument, XPSEXCEPTION_ELEMENT_NAME);
- exceptionElement.appendChild(dpf.newElementNode(dummyDocument, "NoNodesReturnedFromXLink"));
- dummyRoot.appendChild(exceptionElement);
-
- return dummyRoot.getChildNodes();
- }
-
- /**
- * DOCUMENT ME!
- *
- * @param href DOCUMENT ME!
- * @param args DOCUMENT ME!
- *
- * @return DOCUMENT ME!
- */
- public String includeClassName(String href, Vector args) {
- String icn = null;
-
- if (href.indexOf(conf.JAVA_ZONE) == 0) {
- log.debug(".includeClassName(): java class: " + href);
- icn = href.substring(conf.JAVA_ZONE.length(), href.length());
-
- StringTokenizer st = new StringTokenizer(icn, "?");
- icn = st.nextToken();
-
- if (st.countTokens() == 1) {
- args.addElement(st.nextToken());
- }
- } else {
- icn = this.getClass().getName();
- args.addElement(href);
- }
-
- log.debug(".includeClassName(): class name: " + icn);
-
- return icn;
- }
-
- /**
- * DOCUMENT ME!
- *
- * @param url DOCUMENT ME!
- *
- * @return DOCUMENT ME!
- */
- public File getCacheFile(URL url) {
- String cacheFile = null;
- String protocol = url.getProtocol();
-
- if (protocol.equals("file")) {
- cacheFile = protocol + "/" + url.getFile();
- } else if (protocol.equals("http")) {
- cacheFile = protocol + "/" + url.getHost() + "/" + url.getPort() + "/" + url.getFile();
- } else {
- log.error("No such protocol: " + protocol);
- }
-
- return new File(conf.cacheFolder + "/" + cacheFile);
- }
-
- /**
- *
- */
- public boolean writeToCache(String protocol, File cacheFile, Document newDocument) {
- if (protocol.equals("http") && !conf.cacheHTTP) {
- // Do not cache HTTP
- return false;
- }
-
- File cacheFileParent = new File(cacheFile.getParent());
-
- if (!cacheFileParent.isDirectory()) {
- cacheFileParent.mkdirs();
- }
-
- try {
- OutputStream out = new FileOutputStream(cacheFile.getAbsolutePath());
-
- new DOMWriter(out, "iso-8859-1").printWithoutFormatting(newDocument);
- out.close();
-
- return true;
- } catch (Exception e) {
- log.error(".include(): " + e);
- }
-
- return false;
- }
+ }
+ if(nl.getLength() == 0){
+ nl = noNodesReturnedFromXLink(xlink);
+ }
+ }
+ return nl;
+ }
+ /**
+ * DOCUMENT ME!
+ *
+ * @param xlink
+ * DOCUMENT ME!
+ *
+ * @return DOCUMENT ME!
+ */
+ public NodeList noNodesReturnedFromXLink(XLink xlink) {
+ log.warn("No nodes returned from XLink: " + xlink);
+ Document dummyDocument = dpf.getDocument();
+ Element dummyRoot = dpf.newElementNode(dummyDocument, "DummyRoot");
+ Element element = xlink.getXLink(dummyDocument, dpf);
+ dummyRoot.appendChild(element);
+ Element exceptionElement = dpf.newElementNode(dummyDocument, XPSEXCEPTION_ELEMENT_NAME);
+ exceptionElement.appendChild(dpf.newElementNode(dummyDocument, "NoNodesReturnedFromXLink"));
+ dummyRoot.appendChild(exceptionElement);
+ return dummyRoot.getChildNodes();
+ }
+ /**
+ * DOCUMENT ME!
+ *
+ * @param href
+ * DOCUMENT ME!
+ * @param args
+ * DOCUMENT ME!
+ *
+ * @return DOCUMENT ME!
+ */
+ public String includeClassName(String href, Vector args) {
+ String icn = null;
+ if(href.indexOf(conf.JAVA_ZONE) == 0){
+ log.debug(".includeClassName(): java class: " + href);
+ icn = href.substring(conf.JAVA_ZONE.length(), href.length());
+ StringTokenizer st = new StringTokenizer(icn, "?");
+ icn = st.nextToken();
+ if(st.countTokens() == 1){
+ args.addElement(st.nextToken());
+ }
+ }else{
+ icn = this.getClass().getName();
+ args.addElement(href);
+ }
+ log.debug(".includeClassName(): class name: " + icn);
+ return icn;
+ }
+ /**
+ * DOCUMENT ME!
+ *
+ * @param url
+ * DOCUMENT ME!
+ *
+ * @return DOCUMENT ME!
+ */
+ public File getCacheFile(URL url) {
+ String cacheFile = null;
+ String protocol = url.getProtocol();
+ if(protocol.equals("file")){
+ cacheFile = protocol + "/" + url.getFile();
+ }else if(protocol.equals("http")){
+ cacheFile = protocol + "/" + url.getHost() + "/" + url.getPort() + "/" + url.getFile();
+ }else{
+ log.error("No such protocol: " + protocol);
+ }
+ return new File(conf.cacheFolder + "/" + cacheFile);
+ }
+ /**
+ *
+ */
+ public boolean writeToCache(String protocol, File cacheFile, Document newDocument) {
+ if(protocol.equals("http") && !conf.cacheHTTP){
+ // Do not cache HTTP
+ return false;
+ }
+ File cacheFileParent = new File(cacheFile.getParent());
+ if(!cacheFileParent.isDirectory()){
+ cacheFileParent.mkdirs();
+ }
+ try{
+ OutputStream out = new FileOutputStream(cacheFile.getAbsolutePath());
+ new DOMWriter(out, "iso-8859-1").printWithoutFormatting(newDocument);
+ out.close();
+ return true;
+ }catch(Exception e){
+ log.error(".include(): " + e);
+ }
+ return false;
+ }
}
Modified: lenya/branches/revolution/1.3.x/src/java/org/apache/lenya/xml/XPSSourceInformation.java
URL: http://svn.apache.org/viewvc/lenya/branches/revolution/1.3.x/src/java/org/apache/lenya/xml/XPSSourceInformation.java?rev=617035&r1=617034&r2=617035&view=diff
==============================================================================
--- lenya/branches/revolution/1.3.x/src/java/org/apache/lenya/xml/XPSSourceInformation.java (original)
+++ lenya/branches/revolution/1.3.x/src/java/org/apache/lenya/xml/XPSSourceInformation.java Wed Jan 30 23:44:03 2008
@@ -14,205 +14,184 @@
* limitations under the License.
*
*/
-
/* $Id$ */
-
package org.apache.lenya.xml;
-
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Vector;
-
-import org.apache.log4j.Category;
-
+import org.apache.log4j.Logger;
public class XPSSourceInformation {
- static Category log = Category.getInstance(XPSSourceInformation.class);
- public int lineNumber = -1;
- public URL url = null;
- public XPSSourceInformation parentInfo = null;
- public Vector children = null;
- String offset = null;
- String cocoon = null;
-
- /**
- * Creates a new XPSSourceInformation object.
- *
- * @param fileURL DOCUMENT ME!
- * @param cocoon DOCUMENT ME!
- */
- public XPSSourceInformation(String fileURL, String cocoon) {
- this.cocoon = cocoon;
-
- parentInfo = null;
- offset = "++";
- children = new Vector();
-
- try {
- url = new URL(fileURL);
- } catch (MalformedURLException e) {
- log.error(e);
- }
- }
-
- /**
- * Creates a new XPSSourceInformation object.
- *
- * @param urlString DOCUMENT ME!
- * @param parentInfo DOCUMENT ME!
- * @param cocoon DOCUMENT ME!
- */
- public XPSSourceInformation(String urlString, XPSSourceInformation parentInfo, String cocoon) {
- this.cocoon = cocoon;
-
- this.parentInfo = parentInfo;
- offset = "++";
- children = new Vector();
-
- try {
- if (urlString.indexOf("/") == 0) {
- url = new URL("file:" + urlString);
- } else if (urlString.indexOf("cocoon:") == 0) {
- if (cocoon != null) {
- url = new URL(cocoon + "/" + urlString.substring(7)); // remove "cocoon:" protocol
- log.warn("Protocol 7789: COCOON (" + urlString +
- ") -- will be transformed into http: " + url);
- } else {
- log.error("No cocoon base set!");
- }
- } else {
- url = new URL(urlString);
+ private static Logger log = Logger.getLogger(XPSSourceInformation.class);
+ public int lineNumber = -1;
+ public URL url = null;
+ public XPSSourceInformation parentInfo = null;
+ public Vector children = null;
+ String offset = null;
+ String cocoon = null;
+ /**
+ * Creates a new XPSSourceInformation object.
+ *
+ * @param fileURL
+ * DOCUMENT ME!
+ * @param cocoon
+ * DOCUMENT ME!
+ */
+ public XPSSourceInformation(String fileURL, String cocoon) {
+ this.cocoon = cocoon;
+ parentInfo = null;
+ offset = "++";
+ children = new Vector();
+ try{
+ url = new URL(fileURL);
+ }catch(MalformedURLException e){
+ log.error(e);
+ }
+ }
+ /**
+ * Creates a new XPSSourceInformation object.
+ *
+ * @param urlString
+ * DOCUMENT ME!
+ * @param parentInfo
+ * DOCUMENT ME!
+ * @param cocoon
+ * DOCUMENT ME!
+ */
+ public XPSSourceInformation(String urlString, XPSSourceInformation parentInfo, String cocoon) {
+ this.cocoon = cocoon;
+ this.parentInfo = parentInfo;
+ offset = "++";
+ children = new Vector();
+ try{
+ if(urlString.indexOf("/") == 0){
+ url = new URL("file:" + urlString);
+ }else if(urlString.indexOf("cocoon:") == 0){
+ if(cocoon != null){
+ url = new URL(cocoon + "/" + urlString.substring(7)); // remove "cocoon:" protocol
+ log.warn("Protocol 7789: COCOON (" + urlString + ") -- will be transformed into http: " + url);
+ }else{
+ log.error("No cocoon base set!");
}
-
- String p = url.getProtocol();
-
- // Does not make sense, because it will be either file or http, and else an Exception will be thrown!
- if (!(p.equals("http") || p.equals("file") || p.equals("class"))) {
- log.error("This type of protocol is not supported yet: " + p);
+ }else{
+ url = new URL(urlString);
+ }
+ String p = url.getProtocol();
+ // Does not make sense, because it will be either file or http, and else an Exception will be thrown!
+ if(!(p.equals("http") || p.equals("file") || p.equals("class"))){
+ log.error("This type of protocol is not supported yet: " + p);
+ }
+ }catch(MalformedURLException e) // let's hope it's a relative path
+ {
+ log.debug("1079: " + e + " -- Let's hope it's a relative path!");
+ File parent = new File(parentInfo.url.getFile());
+ // transform URI to system-independent path and create absolute path
+ try{
+ int index = urlString.indexOf("#");
+ String xpointer = "";
+ String relativePath = urlString;
+ if(index != -1){
+ relativePath = urlString.substring(0, index);
+ xpointer = urlString.substring(index);
}
- } catch (MalformedURLException e) // let's hope it's a relative path
- {
- log.debug("1079: " + e + " -- Let's hope it's a relative path!");
-
- File parent = new File(parentInfo.url.getFile());
-
- // transform URI to system-independent path and create absolute path
- try {
- int index = urlString.indexOf("#");
- String xpointer = "";
- String relativePath = urlString;
-
- if (index != -1) {
- relativePath = urlString.substring(0, index);
- xpointer = urlString.substring(index);
- }
-
- relativePath = relativePath.replace('/', File.separatorChar);
-
- File file = new File(parent.getParentFile(), relativePath);
-
- url = new URL("file", null, -1, file.getCanonicalPath() + xpointer);
-
- log.info("Concatenated URL: " + url);
- } catch (MalformedURLException exception) {
- log.error(exception);
- } catch (IOException exception) {
- log.error(exception);
+ relativePath = relativePath.replace('/', File.separatorChar);
+ File file = new File(parent.getParentFile(), relativePath);
+ url = new URL("file", null, -1, file.getCanonicalPath() + xpointer);
+ log.info("Concatenated URL: " + url);
+ }catch(MalformedURLException exception){
+ log.error(exception);
+ }catch(IOException exception){
+ log.error(exception);
+ }
+ }
+ if(url.getProtocol().equals("http")){
+ }else if(url.getProtocol().equals("file")){
+ if(parentInfo.url.getProtocol().equals("http")){
+ String protocol = parentInfo.url.getProtocol();
+ String host = parentInfo.url.getHost();
+ int port = parentInfo.url.getPort();
+ try{
+ if(url.getRef() != null){
+ url = new URL(protocol, host, port, url.getFile() + "#" + url.getRef());
+ }else{
+ url = new URL(protocol, host, port, url.getFile());
+ }
+ }catch(MalformedURLException e){
+ log.error(e);
}
- }
-
- if (url.getProtocol().equals("http")) {
- } else if (url.getProtocol().equals("file")) {
- if (parentInfo.url.getProtocol().equals("http")) {
- String protocol = parentInfo.url.getProtocol();
- String host = parentInfo.url.getHost();
- int port = parentInfo.url.getPort();
-
- try {
- if (url.getRef() != null) {
- url = new URL(protocol, host, port, url.getFile() + "#" + url.getRef());
- } else {
- url = new URL(protocol, host, port, url.getFile());
- }
- } catch (MalformedURLException e) {
- log.error(e);
- }
- }
- } else {
- log.error("EXCEPTION: 0.2.21");
- }
- }
-
- /**
- * DOCUMENT ME!
- *
- * @param args DOCUMENT ME!
- */
- public static void main(String[] args) {
- String cocoon = null;
- XPSSourceInformation xpssf = new XPSSourceInformation(args[0], cocoon);
- System.out.println(xpssf);
- }
-
- /**
- * DOCUMENT ME!
- *
- * @param child DOCUMENT ME!
- */
- public void addChild(XPSSourceInformation child) {
- children.addElement(child);
- }
-
- /**
- * DOCUMENT ME!
- *
- * @return DOCUMENT ME!
- */
- public String toString() {
- return toString("", offset);
- }
-
- /**
- * DOCUMENT ME!
- *
- * @param index DOCUMENT ME!
- * @param offset DOCUMENT ME!
- *
- * @return DOCUMENT ME!
- */
- public String toString(String index, String offset) {
- String s = index + url.toString() + "\n";
-
- for (int i = 0; i < children.size(); i++) {
- XPSSourceInformation child = (XPSSourceInformation) children.elementAt(i);
- s = s + child.toString(index + offset, offset);
- }
-
- return s;
- }
-
- /**
- * Check for XInclude Loops
- *
- * @param xpssf DOCUMENT ME!
- * @param url DOCUMENT ME!
- *
- * @return DOCUMENT ME!
- */
- public boolean checkLoop(XPSSourceInformation xpssf, URL url) {
- if (xpssf.url.getFile().equals(url.getFile())) {
- if (xpssf.parentInfo != null) {
- return true;
- } else {
- return false; // This is just the request (it can be dummy.xml but also something real)
- }
- }
-
- if (xpssf.parentInfo != null) {
- return checkLoop(xpssf.parentInfo, url);
- }
-
- return false;
- }
+ }
+ }else{
+ log.error("EXCEPTION: 0.2.21");
+ }
+ }
+ /**
+ * DOCUMENT ME!
+ *
+ * @param args
+ * DOCUMENT ME!
+ */
+ public static void main(String[] args) {
+ String cocoon = null;
+ XPSSourceInformation xpssf = new XPSSourceInformation(args[0], cocoon);
+ System.out.println(xpssf);
+ }
+ /**
+ * DOCUMENT ME!
+ *
+ * @param child
+ * DOCUMENT ME!
+ */
+ public void addChild(XPSSourceInformation child) {
+ children.addElement(child);
+ }
+ /**
+ * DOCUMENT ME!
+ *
+ * @return DOCUMENT ME!
+ */
+ public String toString() {
+ return toString("", offset);
+ }
+ /**
+ * DOCUMENT ME!
+ *
+ * @param index
+ * DOCUMENT ME!
+ * @param offset
+ * DOCUMENT ME!
+ *
+ * @return DOCUMENT ME!
+ */
+ public String toString(String index, String offset) {
+ String s = index + url.toString() + "\n";
+ for(int i = 0; i < children.size(); i++){
+ XPSSourceInformation child = (XPSSourceInformation) children.elementAt(i);
+ s = s + child.toString(index + offset, offset);
+ }
+ return s;
+ }
+ /**
+ * Check for XInclude Loops
+ *
+ * @param xpssf
+ * DOCUMENT ME!
+ * @param url
+ * DOCUMENT ME!
+ *
+ * @return DOCUMENT ME!
+ */
+ public boolean checkLoop(XPSSourceInformation xpssf, URL url) {
+ if(xpssf.url.getFile().equals(url.getFile())){
+ if(xpssf.parentInfo != null){
+ return true;
+ }else{
+ return false; // This is just the request (it can be dummy.xml but also something real)
+ }
+ }
+ if(xpssf.parentInfo != null){
+ return checkLoop(xpssf.parentInfo, url);
+ }
+ return false;
+ }
}
Modified: lenya/branches/revolution/1.3.x/src/java/org/apache/lenya/xml/XPointerFactory.java
URL: http://svn.apache.org/viewvc/lenya/branches/revolution/1.3.x/src/java/org/apache/lenya/xml/XPointerFactory.java?rev=617035&r1=617034&r2=617035&view=diff
==============================================================================
--- lenya/branches/revolution/1.3.x/src/java/org/apache/lenya/xml/XPointerFactory.java (original)
+++ lenya/branches/revolution/1.3.x/src/java/org/apache/lenya/xml/XPointerFactory.java Wed Jan 30 23:44:03 2008
@@ -14,319 +14,280 @@
* limitations under the License.
*
*/
-
/* $Id$ */
-
package org.apache.lenya.xml;
-
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Properties;
import java.util.Vector;
-
import org.apache.lenya.xml.xpointer.XPointer;
-import org.apache.log4j.Category;
+import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
-
public class XPointerFactory {
- static Category log = Category.getInstance(XPointerFactory.class);
- XPointer xpointer = null;
-
- /**
- * Creates a new XPointerFactory object.
- */
- public XPointerFactory() {
- Properties properties = new Properties();
- String propertiesFileName = "conf.properties";
-
- try {
- properties.load(XPointerFactory.class.getResourceAsStream(propertiesFileName));
- } catch (Exception e) {
- log.fatal(": Failed to load properties from resource: " + propertiesFileName);
- }
-
- String xpointerName = properties.getProperty("XPointer");
-
- if (xpointerName == null) {
- log.fatal(": No XPointer specified in " + propertiesFileName);
- }
-
- try {
- Class xpointerClass = Class.forName(xpointerName);
- xpointer = (XPointer) xpointerClass.newInstance();
- } catch (Exception e) {
- log.fatal(": " + e);
- }
- }
-
- /**
- * DOCUMENT ME!
- *
- * @param args DOCUMENT ME!
- */
- public static void main(String[] args) {
- XPointerFactory xpf = new XPointerFactory();
- DOMParserFactory dpf = new DOMParserFactory();
-
- if (args.length != 2) {
- System.err.println("Usage: java " + xpf.getClass().getName() +
- " example.xml \"/Example/People/Person[1]/Name\"");
-
+ private static Logger log = Logger.getLogger(XPointerFactory.class);
+ XPointer xpointer = null;
+ /**
+ * Creates a new XPointerFactory object.
+ */
+ public XPointerFactory() {
+ Properties properties = new Properties();
+ String propertiesFileName = "conf.properties";
+ try{
+ properties.load(XPointerFactory.class.getResourceAsStream(propertiesFileName));
+ }catch(Exception e){
+ log.fatal(": Failed to load properties from resource: " + propertiesFileName);
+ }
+ String xpointerName = properties.getProperty("XPointer");
+ if(xpointerName == null){
+ log.fatal(": No XPointer specified in " + propertiesFileName);
+ }
+ try{
+ Class xpointerClass = Class.forName(xpointerName);
+ xpointer = (XPointer) xpointerClass.newInstance();
+ }catch(Exception e){
+ log.fatal(": " + e);
+ }
+ }
+ /**
+ * DOCUMENT ME!
+ *
+ * @param args
+ * DOCUMENT ME!
+ */
+ public static void main(String[] args) {
+ XPointerFactory xpf = new XPointerFactory();
+ DOMParserFactory dpf = new DOMParserFactory();
+ if(args.length != 2){
+ System.err.println("Usage: java " + xpf.getClass().getName() + " example.xml \"/Example/People/Person[1]/Name\"");
+ return;
+ }
+ Document document = null;
+ try{
+ document = dpf.getDocument(args[0]);
+ }catch(FileNotFoundException e){
+ System.err.println("No such file or directory: " + e.getMessage());
+ }catch(Exception e){
+ System.err.println(e.getMessage());
+ }
+ String xpath = args[1];
+ try{
+ Vector nodes = xpf.select(document.getDocumentElement(), "xpointer(" + xpath + ")");
+ String[] values = xpf.getNodeValues(nodes);
+ for(int i = 0; i < nodes.size(); i++){
+ System.out.println(((Node) nodes.elementAt(i)).getNodeName() + ": " + values[i]);
+ }
+ }catch(Exception e){
+ System.err.println(xpf.getClass().getName() + ".main(): " + e);
+ }
+ Document doc = xpf.employees();
+ try{
+ Vector nodes = xpf.select(doc.getDocumentElement(), "xpointer(/Employees/Employee[2])");
+ String[] values = xpf.getNodeValues(nodes);
+ for(int i = 0; i < nodes.size(); i++){
+ System.out.println(((Node) nodes.elementAt(i)).getNodeName() + ": " + values[i]);
+ }
+ Element leviElement = (Element) nodes.elementAt(0);
+ leviElement.appendChild(dpf.newTextNode(doc, " Brucker"));
+ }catch(Exception e){
+ System.err.println(xpf.getClass().getName() + ".main(): " + e);
+ }
+ new DOMWriter(new PrintWriter(System.out)).print(doc);
+ System.out.println("");
+ }
+ /**
+ * Parse reference for xpointer and namespaces
+ *
+ * @param reference
+ * xmlns(...)xpointer(...)xpointer(...)
+ *
+ * @exception MalformedXPointerException
+ * xpointer(xpath)
+ */
+ public void parse(String reference, Vector xpaths, Vector namespaces) throws MalformedXPointerException {
+ tokenize(reference, xpaths, namespaces);
+ }
+ /**
+ * Select nodes by xpointer
+ *
+ * @param node
+ * Document Node
+ * @param reference
+ * xmls(...)xpointer(...)
+ *
+ * @return nodes
+ *
+ * @exception Exception
+ * ...
+ */
+ public Vector select(Node node, String reference) throws Exception {
+ Vector xpaths = new Vector();
+ Vector namespaces = new Vector();
+ parse(reference, xpaths, namespaces);
+ Vector nodes = new Vector();
+ for(int i = 0; i < xpaths.size(); i++){
+ Vector n = xpointer.select(node, (String) xpaths.elementAt(i), namespaces);
+ for(int j = 0; j < n.size(); j++){
+ nodes.addElement(n.elementAt(j));
+ }
+ }
+ return nodes;
+ }
+ /**
+ * Select nodes by xpointer and return node at specific position
+ *
+ * @param node
+ * Document Node
+ * @param reference
+ * xmls(...)xpointer(...)
+ *
+ * @return node
+ *
+ * @exception Exception
+ * ...
+ */
+ public Node selectAt(Node node, String reference, int i) throws Exception {
+ return (Node) select(node, reference).elementAt(i);
+ }
+ /**
+ * DOCUMENT ME!
+ *
+ * @param xpointer
+ * DOCUMENT ME!
+ * @param xpaths
+ * DOCUMENT ME!
+ *
+ * @exception MalformedXPointerException
+ * xpointer(xpath)xpointer(xpath)
+ */
+ public void tokenize(String xpointer, Vector xpaths, Vector namespaces) throws MalformedXPointerException {
+ if((xpointer.indexOf("xpointer(") == 0) && (xpointer.charAt(xpointer.length() - 1) == ')')){
+ String substring = xpointer.substring(9, xpointer.length());
+ int i = substring.indexOf(")");
+ if(i >= 0){
+ log.debug("XPath: " + substring.substring(0, i));
+ xpaths.addElement(substring.substring(0, i));
+ tokenize(substring.substring(i + 1, substring.length()), xpaths, namespaces);
+ }else{
+ xpaths.addElement(substring.substring(0, substring.length() - 1));
return;
- }
-
- Document document = null;
-
- try {
- document = dpf.getDocument(args[0]);
- } catch (FileNotFoundException e) {
- System.err.println("No such file or directory: " + e.getMessage());
- } catch (Exception e) {
- System.err.println(e.getMessage());
- }
-
- String xpath = args[1];
-
- try {
- Vector nodes = xpf.select(document.getDocumentElement(), "xpointer(" + xpath + ")");
- String[] values = xpf.getNodeValues(nodes);
-
- for (int i = 0; i < nodes.size(); i++) {
- System.out.println(((Node) nodes.elementAt(i)).getNodeName() + ": " + values[i]);
- }
- } catch (Exception e) {
- System.err.println(xpf.getClass().getName() + ".main(): " + e);
- }
-
- Document doc = xpf.employees();
-
- try {
- Vector nodes = xpf.select(doc.getDocumentElement(), "xpointer(/Employees/Employee[2])");
- String[] values = xpf.getNodeValues(nodes);
-
- for (int i = 0; i < nodes.size(); i++) {
- System.out.println(((Node) nodes.elementAt(i)).getNodeName() + ": " + values[i]);
- }
-
- Element leviElement = (Element) nodes.elementAt(0);
- leviElement.appendChild(dpf.newTextNode(doc, " Brucker"));
- } catch (Exception e) {
- System.err.println(xpf.getClass().getName() + ".main(): " + e);
- }
-
- new DOMWriter(new PrintWriter(System.out)).print(doc);
- System.out.println("");
- }
-
- /**
- * Parse reference for xpointer and namespaces
- *
- * @param reference xmlns(...)xpointer(...)xpointer(...)
- *
- * @exception MalformedXPointerException xpointer(xpath)
- */
- public void parse(String reference, Vector xpaths, Vector namespaces) throws MalformedXPointerException {
- tokenize(reference, xpaths, namespaces);
- }
-
- /**
- * Select nodes by xpointer
- *
- * @param node Document Node
- * @param reference xmls(...)xpointer(...)
- *
- * @return nodes
- *
- * @exception Exception ...
- */
- public Vector select(Node node, String reference) throws Exception {
- Vector xpaths = new Vector();
- Vector namespaces = new Vector();
- parse(reference, xpaths, namespaces);
-
- Vector nodes = new Vector();
-
- for (int i = 0; i < xpaths.size(); i++) {
- Vector n = xpointer.select(node, (String) xpaths.elementAt(i), namespaces);
-
- for (int j = 0; j < n.size(); j++) {
- nodes.addElement(n.elementAt(j));
- }
- }
-
- return nodes;
- }
-
- /**
- * Select nodes by xpointer and return node at specific position
- *
- * @param node Document Node
- * @param reference xmls(...)xpointer(...)
- *
- * @return node
- *
- * @exception Exception ...
- */
- public Node selectAt(Node node, String reference, int i) throws Exception {
- return (Node)select(node, reference).elementAt(i);
- }
-
- /**
- * DOCUMENT ME!
- *
- * @param xpointer DOCUMENT ME!
- * @param xpaths DOCUMENT ME!
- *
- * @exception MalformedXPointerException xpointer(xpath)xpointer(xpath)
- */
- public void tokenize(String xpointer, Vector xpaths, Vector namespaces) throws MalformedXPointerException {
- if ((xpointer.indexOf("xpointer(") == 0) && (xpointer.charAt(xpointer.length() - 1) == ')')) {
-
- String substring = xpointer.substring(9, xpointer.length());
- int i = substring.indexOf(")");
-
- if (i >= 0) {
- log.debug("XPath: " + substring.substring(0, i));
- xpaths.addElement(substring.substring(0, i));
- tokenize(substring.substring(i + 1, substring.length()), xpaths, namespaces);
- } else {
- xpaths.addElement(substring.substring(0, substring.length() - 1));
- return;
- }
- } else if ((xpointer.indexOf("xmlns(") == 0) && (xpointer.charAt(xpointer.length() - 1) == ')')) {
- String substring = xpointer.substring(6, xpointer.length());
- int i = substring.indexOf(")");
-
- if (i >= 0) {
- log.debug("Namespace: " + substring.substring(0, i));
- namespaces.addElement(substring.substring(0, i));
- tokenize(substring.substring(i + 1, substring.length()), xpaths, namespaces);
- } else {
- xpaths.addElement(substring.substring(0, substring.length() - 1));
- return;
- }
- } else if (xpointer.equals("")) {
- return;
- } else {
- throw new MalformedXPointerException(xpointer);
- }
- }
-
- /**
- * DOCUMENT ME!
- *
- * @param nodes DOCUMENT ME!
- *
- * @return DOCUMENT ME!
- *
- * @throws Exception DOCUMENT ME!
- */
- public String[] getNodeValues(Vector nodes) throws Exception {
- String[] values = new String[nodes.size()];
-
- for (int i = 0; i < values.length; i++) {
- Node node = (Node) nodes.elementAt(i);
- short type = node.getNodeType();
-
- switch (type) {
+ }
+ }else if((xpointer.indexOf("xmlns(") == 0) && (xpointer.charAt(xpointer.length() - 1) == ')')){
+ String substring = xpointer.substring(6, xpointer.length());
+ int i = substring.indexOf(")");
+ if(i >= 0){
+ log.debug("Namespace: " + substring.substring(0, i));
+ namespaces.addElement(substring.substring(0, i));
+ tokenize(substring.substring(i + 1, substring.length()), xpaths, namespaces);
+ }else{
+ xpaths.addElement(substring.substring(0, substring.length() - 1));
+ return;
+ }
+ }else if(xpointer.equals("")){
+ return;
+ }else{
+ throw new MalformedXPointerException(xpointer);
+ }
+ }
+ /**
+ * DOCUMENT ME!
+ *
+ * @param nodes
+ * DOCUMENT ME!
+ *
+ * @return DOCUMENT ME!
+ *
+ * @throws Exception
+ * DOCUMENT ME!
+ */
+ public String[] getNodeValues(Vector nodes) throws Exception {
+ String[] values = new String[nodes.size()];
+ for(int i = 0; i < values.length; i++){
+ Node node = (Node) nodes.elementAt(i);
+ short type = node.getNodeType();
+ switch(type){
case Node.ELEMENT_NODE: {
- values[i] = getElementValue((Element) node);
-
- break;
+ values[i] = getElementValue((Element) node);
+ break;
}
-
case Node.ATTRIBUTE_NODE: {
- values[i] = node.getNodeValue();
-
- break;
+ values[i] = node.getNodeValue();
+ break;
}
-
default:
- values[i] = "";
- throw new Exception("Neither ELEMENT nor ATTRIBUTE: " + type);
- }
- }
-
- return values;
- }
-
- /**
- * DOCUMENT ME!
- *
- * @param element DOCUMENT ME!
- *
- * @return DOCUMENT ME!
- */
- public String getElementValue(Element element) {
- String value = "";
- NodeList nl = element.getChildNodes();
-
- for (int k = 0; k < nl.getLength(); k++) {
- short nodeType = nl.item(k).getNodeType();
-
- if (nodeType == Node.TEXT_NODE) {
- value = value + nl.item(k).getNodeValue();
- } else if (nodeType == Node.ELEMENT_NODE) {
- value = value + getElementValue((Element) nl.item(k));
- } else {
- System.err.println("EXCEPTION: " + this.getClass().getName() +
- ".getElementValue(): No TEXT_NODE");
- }
- }
-
- return value;
- }
-
- /**
- * DOCUMENT ME!
- *
- * @param element DOCUMENT ME!
- * @param text DOCUMENT ME!
- */
- public void getElementValue(Element element, Vector text) {
- NodeList nl = element.getChildNodes();
-
- for (int k = 0; k < nl.getLength(); k++) {
- short nodeType = nl.item(k).getNodeType();
-
- if (nodeType == Node.TEXT_NODE) {
- text.addElement(nl.item(k).getNodeValue());
- } else if (nodeType == Node.ELEMENT_NODE) {
- getElementValue((Element) nl.item(k), text);
- } else {
- System.err.println("EXCEPTION: " + this.getClass().getName() +
- ".getElementValue(): No TEXT_NODE");
- }
- }
- }
-
- /**
- * DOCUMENT ME!
- *
- * @return DOCUMENT ME!
- */
- public Document employees() {
- DOMParserFactory dpf = new DOMParserFactory();
- Document doc = dpf.getDocument();
- Element michi = dpf.newElementNode(doc, "Employee");
- michi.setAttribute("Id", "0");
- michi.appendChild(dpf.newTextNode(doc, "Michi"));
-
- Element levi = dpf.newElementNode(doc, "Employee");
- levi.setAttribute("Id", "1");
- levi.appendChild(dpf.newTextNode(doc, "Levi"));
-
- Element employees = dpf.newElementNode(doc, "Employees");
- employees.appendChild(dpf.newTextNode(doc, "\n"));
- employees.appendChild(michi);
- employees.appendChild(dpf.newTextNode(doc, "\n"));
- employees.appendChild(levi);
- employees.appendChild(dpf.newTextNode(doc, "\n"));
- doc.appendChild(employees);
-
- return doc;
- }
+ values[i] = "";
+ throw new Exception("Neither ELEMENT nor ATTRIBUTE: " + type);
+ }
+ }
+ return values;
+ }
+ /**
+ * DOCUMENT ME!
+ *
+ * @param element
+ * DOCUMENT ME!
+ *
+ * @return DOCUMENT ME!
+ */
+ public String getElementValue(Element element) {
+ String value = "";
+ NodeList nl = element.getChildNodes();
+ for(int k = 0; k < nl.getLength(); k++){
+ short nodeType = nl.item(k).getNodeType();
+ if(nodeType == Node.TEXT_NODE){
+ value = value + nl.item(k).getNodeValue();
+ }else if(nodeType == Node.ELEMENT_NODE){
+ value = value + getElementValue((Element) nl.item(k));
+ }else{
+ System.err.println("EXCEPTION: " + this.getClass().getName() + ".getElementValue(): No TEXT_NODE");
+ }
+ }
+ return value;
+ }
+ /**
+ * DOCUMENT ME!
+ *
+ * @param element
+ * DOCUMENT ME!
+ * @param text
+ * DOCUMENT ME!
+ */
+ public void getElementValue(Element element, Vector text) {
+ NodeList nl = element.getChildNodes();
+ for(int k = 0; k < nl.getLength(); k++){
+ short nodeType = nl.item(k).getNodeType();
+ if(nodeType == Node.TEXT_NODE){
+ text.addElement(nl.item(k).getNodeValue());
+ }else if(nodeType == Node.ELEMENT_NODE){
+ getElementValue((Element) nl.item(k), text);
+ }else{
+ System.err.println("EXCEPTION: " + this.getClass().getName() + ".getElementValue(): No TEXT_NODE");
+ }
+ }
+ }
+ /**
+ * DOCUMENT ME!
+ *
+ * @return DOCUMENT ME!
+ */
+ public Document employees() {
+ DOMParserFactory dpf = new DOMParserFactory();
+ Document doc = dpf.getDocument();
+ Element michi = dpf.newElementNode(doc, "Employee");
+ michi.setAttribute("Id", "0");
+ michi.appendChild(dpf.newTextNode(doc, "Michi"));
+ Element levi = dpf.newElementNode(doc, "Employee");
+ levi.setAttribute("Id", "1");
+ levi.appendChild(dpf.newTextNode(doc, "Levi"));
+ Element employees = dpf.newElementNode(doc, "Employees");
+ employees.appendChild(dpf.newTextNode(doc, "\n"));
+ employees.appendChild(michi);
+ employees.appendChild(dpf.newTextNode(doc, "\n"));
+ employees.appendChild(levi);
+ employees.appendChild(dpf.newTextNode(doc, "\n"));
+ doc.appendChild(employees);
+ return doc;
+ }
}
Modified: lenya/branches/revolution/1.3.x/src/java/org/apache/lenya/xml/xpointer/XalanXPointer.java
URL: http://svn.apache.org/viewvc/lenya/branches/revolution/1.3.x/src/java/org/apache/lenya/xml/xpointer/XalanXPointer.java?rev=617035&r1=617034&r2=617035&view=diff
==============================================================================
--- lenya/branches/revolution/1.3.x/src/java/org/apache/lenya/xml/xpointer/XalanXPointer.java (original)
+++ lenya/branches/revolution/1.3.x/src/java/org/apache/lenya/xml/xpointer/XalanXPointer.java Wed Jan 30 23:44:03 2008
@@ -14,119 +14,97 @@
* limitations under the License.
*
*/
-
/* $Id$ */
-
package org.apache.lenya.xml.xpointer;
-
import java.util.Vector;
-
import org.apache.lenya.xml.DOMParserFactory;
-import org.apache.log4j.Category;
+import org.apache.log4j.Logger;
import org.apache.xpath.XPathAPI;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
-
-
/**
* XPointer implementation
*/
public class XalanXPointer implements XPointer {
- private static Category log = Category.getInstance(XalanXPointer.class);
-
- /**
- * DOCUMENT ME!
- *
- * @param args DOCUMENT ME!
- */
- public static void main(String[] args) {
- XPointer xpointer = new XalanXPointer();
-
- if (args.length != 2) {
- System.err.println("Usage: java " + xpointer.getClass().getName() +
- " example.xml \"/Example/People/Person[position() < 2]/Street/@Number\"");
-
- return;
- }
-
- DOMParserFactory dpf = new DOMParserFactory();
- Document document = null;
-
- try {
- document = dpf.getDocument(args[0]);
- } catch (Exception e) {
- System.err.println(xpointer.getClass().getName() + ".main(): " + e);
- }
-
- Element root = document.getDocumentElement();
- String xpath = args[1];
-
- try {
- Vector namespaces = new Vector();
- Vector nodes = xpointer.select(root, xpath, namespaces);
-
- for (int i = 0; i < nodes.size(); i++) {
- Node node = (Node) nodes.elementAt(i);
- short type = node.getNodeType();
-
- if (type == Node.ATTRIBUTE_NODE) {
- System.out.println("Attribute (" + node.getNodeName() + "): " +
- node.getNodeValue());
- } else if (type == Node.ELEMENT_NODE) {
- System.out.println("Element (" + node.getNodeName() + "): " +
- node.getFirstChild().getNodeValue());
- }
+ private static Logger log = Logger.getLogger(XalanXPointer.class);
+ /**
+ * DOCUMENT ME!
+ *
+ * @param args
+ * DOCUMENT ME!
+ */
+ public static void main(String[] args) {
+ XPointer xpointer = new XalanXPointer();
+ if(args.length != 2){
+ System.err.println("Usage: java " + xpointer.getClass().getName() + " example.xml \"/Example/People/Person[position() < 2]/Street/@Number\"");
+ return;
+ }
+ DOMParserFactory dpf = new DOMParserFactory();
+ Document document = null;
+ try{
+ document = dpf.getDocument(args[0]);
+ }catch(Exception e){
+ System.err.println(xpointer.getClass().getName() + ".main(): " + e);
+ }
+ Element root = document.getDocumentElement();
+ String xpath = args[1];
+ try{
+ Vector namespaces = new Vector();
+ Vector nodes = xpointer.select(root, xpath, namespaces);
+ for(int i = 0; i < nodes.size(); i++){
+ Node node = (Node) nodes.elementAt(i);
+ short type = node.getNodeType();
+ if(type == Node.ATTRIBUTE_NODE){
+ System.out.println("Attribute (" + node.getNodeName() + "): " + node.getNodeValue());
+ }else if(type == Node.ELEMENT_NODE){
+ System.out.println("Element (" + node.getNodeName() + "): " + node.getFirstChild().getNodeValue());
}
- } catch (Exception e) {
- System.err.println(e);
- }
- }
-
- /**
- * Select node by specified XPath
- *
- * @param node Node to select from
- * @param xpath XPath to select nodes
- *
- * @return Selected nodes
- *
- * @exception Exception ...
- */
- public Vector select(Node node, String xpath, Vector namespaces) throws Exception {
- NodeList children = node.getChildNodes();
-
- log.debug("Select " + xpath + " from node " + node.getNodeName());
-
- NodeList nl = null;
- if (namespaces.size() > 0) {
- org.w3c.dom.Document doc = org.apache.lenya.xml.DocumentHelper.createDocument("", "foo", null);
- for (int i = 0; i < namespaces.size(); i++) {
- String namespace = (String)namespaces.elementAt(i);
- String prefix = namespace.substring(0, namespace.indexOf("="));
- String namespaceURI = namespace.substring(namespace.indexOf("=") + 1);
- log.debug("Namespace: " + prefix + " " + namespaceURI);
-
- doc.getDocumentElement().setAttribute("xmlns:" + prefix, namespaceURI);
- }
- nl = XPathAPI.selectNodeList(node, xpath, doc.getDocumentElement());
- } else {
- nl = XPathAPI.selectNodeList(node, xpath);
- }
-
-
- if (nl != null && nl.getLength() == 0) {
- log.info("No such nodes: " + xpath);
- return new Vector();
- }
-
- Vector nodes = new Vector();
-
- for (int i = 0; i < nl.getLength(); i++) {
- nodes.addElement(nl.item(i));
- }
-
- return nodes;
- }
+ }
+ }catch(Exception e){
+ System.err.println(e);
+ }
+ }
+ /**
+ * Select node by specified XPath
+ *
+ * @param node
+ * Node to select from
+ * @param xpath
+ * XPath to select nodes
+ *
+ * @return Selected nodes
+ *
+ * @exception Exception
+ * ...
+ */
+ public Vector select(Node node, String xpath, Vector namespaces) throws Exception {
+ // NodeList children =
+ node.getChildNodes();
+ log.debug("Select " + xpath + " from node " + node.getNodeName());
+ NodeList nl = null;
+ if(namespaces.size() > 0){
+ org.w3c.dom.Document doc = org.apache.lenya.xml.DocumentHelper.createDocument("", "foo", null);
+ for(int i = 0; i < namespaces.size(); i++){
+ String namespace = (String) namespaces.elementAt(i);
+ String prefix = namespace.substring(0, namespace.indexOf("="));
+ String namespaceURI = namespace.substring(namespace.indexOf("=") + 1);
+ log.debug("Namespace: " + prefix + " " + namespaceURI);
+ doc.getDocumentElement().setAttribute("xmlns:" + prefix, namespaceURI);
+ }
+ nl = XPathAPI.selectNodeList(node, xpath, doc.getDocumentElement());
+ }else{
+ nl = XPathAPI.selectNodeList(node, xpath);
+ }
+ if(nl != null && nl.getLength() == 0){
+ log.info("No such nodes: " + xpath);
+ return new Vector();
+ }
+ Vector nodes = new Vector();
+ for(int i = 0; i < nl.getLength(); i++){
+ nodes.addElement(nl.item(i));
+ }
+ return nodes;
+ }
}