You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@manifoldcf.apache.org by kw...@apache.org on 2013/02/09 20:48:47 UTC

svn commit: r1444414 - in /manifoldcf/branches/CONNECTORS-633/framework/core/src/main/java/org/apache/manifoldcf/core/fuzzyml: XMLOutputStreamParsingContext.java XMLStringParsingContext.java XMLWriterParsingContext.java

Author: kwright
Date: Sat Feb  9 19:48:46 2013
New Revision: 1444414

URL: http://svn.apache.org/r1444414
Log:
Add similar support for streams etc to new parser that we have for the older SAX parser.

Added:
    manifoldcf/branches/CONNECTORS-633/framework/core/src/main/java/org/apache/manifoldcf/core/fuzzyml/XMLOutputStreamParsingContext.java   (with props)
    manifoldcf/branches/CONNECTORS-633/framework/core/src/main/java/org/apache/manifoldcf/core/fuzzyml/XMLStringParsingContext.java   (with props)
    manifoldcf/branches/CONNECTORS-633/framework/core/src/main/java/org/apache/manifoldcf/core/fuzzyml/XMLWriterParsingContext.java   (with props)

Added: manifoldcf/branches/CONNECTORS-633/framework/core/src/main/java/org/apache/manifoldcf/core/fuzzyml/XMLOutputStreamParsingContext.java
URL: http://svn.apache.org/viewvc/manifoldcf/branches/CONNECTORS-633/framework/core/src/main/java/org/apache/manifoldcf/core/fuzzyml/XMLOutputStreamParsingContext.java?rev=1444414&view=auto
==============================================================================
--- manifoldcf/branches/CONNECTORS-633/framework/core/src/main/java/org/apache/manifoldcf/core/fuzzyml/XMLOutputStreamParsingContext.java (added)
+++ manifoldcf/branches/CONNECTORS-633/framework/core/src/main/java/org/apache/manifoldcf/core/fuzzyml/XMLOutputStreamParsingContext.java Sat Feb  9 19:48:46 2013
@@ -0,0 +1,96 @@
+/* $Id$ */
+
+/**
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements. See the NOTICE file distributed with
+* this work for additional information regarding copyright ownership.
+* The ASF licenses this file to You under the Apache License, Version 2.0
+* (the "License"); you may not use this file except in compliance with
+* the License. You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+package org.apache.manifoldcf.core.fuzzyml;
+
+import org.apache.manifoldcf.core.interfaces.*;
+import java.io.*;
+import java.util.*;
+
+/** An instance of this class represents a parsing context within a node.  Data is written to the supplied output stream in utf-8 format.
+*/
+public class XMLOutputStreamParsingContext extends XMLWriterParsingContext
+{
+  /** The writer */
+  protected OutputStream outputStream;
+
+  /** Full constructor.  Used for individual tags. */
+  public XMLOutputStreamParsingContext(XMLFuzzyHierarchicalParseState theStream, String namespace, String localname, String qname, Map<String,String> theseAttributes, OutputStream os)
+    throws ManifoldCFException, UnsupportedEncodingException
+  {
+    // Construct an appropriate writer
+    super(theStream,namespace,localname,qname,theseAttributes,new OutputStreamWriter(os,"utf-8"));
+    // Save the stream
+    outputStream = os;
+  }
+
+  /** Flush the data to the underlying output stream */
+  public void flush()
+    throws ManifoldCFException
+  {
+    try
+    {
+      if (outputStream != null)
+      {
+        // Do the base class first - this flushes the Writer
+        super.flush();
+        outputStream.flush();
+      }
+    }
+    catch (java.net.SocketTimeoutException e)
+    {
+      throw new ManifoldCFException("Socket timeout exception: "+e.getMessage(),e);
+    }
+    catch (InterruptedIOException e)
+    {
+      throw new ManifoldCFException("Interrupted: "+e.getMessage(),e,ManifoldCFException.INTERRUPTED);
+    }
+    catch (IOException e)
+    {
+      throw new ManifoldCFException("IO exception: "+e.getMessage(),e);
+    }
+  }
+
+  /** Close the underlying stream. */
+  public void close()
+    throws ManifoldCFException
+  {
+    // Now, close.
+    try
+    {
+      if (outputStream != null)
+      {
+        outputStream.close();
+        outputStream = null;
+      }
+    }
+    catch (java.net.SocketTimeoutException e)
+    {
+      throw new ManifoldCFException("Socket timeout exception: "+e.getMessage(),e);
+    }
+    catch (InterruptedIOException e)
+    {
+      throw new ManifoldCFException("Interrupted: "+e.getMessage(),e,ManifoldCFException.INTERRUPTED);
+    }
+    catch (IOException e)
+    {
+      throw new ManifoldCFException("IO exception: "+e.getMessage(),e);
+    }
+  }
+
+}

Propchange: manifoldcf/branches/CONNECTORS-633/framework/core/src/main/java/org/apache/manifoldcf/core/fuzzyml/XMLOutputStreamParsingContext.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: manifoldcf/branches/CONNECTORS-633/framework/core/src/main/java/org/apache/manifoldcf/core/fuzzyml/XMLOutputStreamParsingContext.java
------------------------------------------------------------------------------
    svn:keywords = Id

Added: manifoldcf/branches/CONNECTORS-633/framework/core/src/main/java/org/apache/manifoldcf/core/fuzzyml/XMLStringParsingContext.java
URL: http://svn.apache.org/viewvc/manifoldcf/branches/CONNECTORS-633/framework/core/src/main/java/org/apache/manifoldcf/core/fuzzyml/XMLStringParsingContext.java?rev=1444414&view=auto
==============================================================================
--- manifoldcf/branches/CONNECTORS-633/framework/core/src/main/java/org/apache/manifoldcf/core/fuzzyml/XMLStringParsingContext.java (added)
+++ manifoldcf/branches/CONNECTORS-633/framework/core/src/main/java/org/apache/manifoldcf/core/fuzzyml/XMLStringParsingContext.java Sat Feb  9 19:48:46 2013
@@ -0,0 +1,54 @@
+/* $Id$ */
+
+/**
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements. See the NOTICE file distributed with
+* this work for additional information regarding copyright ownership.
+* The ASF licenses this file to You under the Apache License, Version 2.0
+* (the "License"); you may not use this file except in compliance with
+* the License. You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+package org.apache.manifoldcf.core.fuzzyml;
+
+import org.apache.manifoldcf.core.interfaces.*;
+import java.io.*;
+import java.util.*;
+
+/** An instance of this class represents a parsing context within a node, where the data value is to be recorded as an in-memory string.  The data string is
+* available as a local StringBuilder object, which will be accessible to any class that extends this one.
+*/
+public class XMLStringParsingContext extends XMLParsingContext
+{
+  /** The string buffer */
+  protected StringBuilder value = new StringBuilder();
+
+  /** Full constructor.  Used for individual tags. */
+  public XMLStringParsingContext(XMLFuzzyHierarchicalParseState theStream, String namespace, String localname, String qname, Map<String,String> theseAttributes)
+  {
+    super(theStream,namespace,localname,qname,theseAttributes);
+  }
+
+  /** Get the string value */
+  public String getValue()
+  {
+    return value.toString();
+  }
+
+  /** This method is meant to be extended by classes that extend this class */
+  @Override
+  protected void tagContents(String value)
+    throws ManifoldCFException
+  {
+    // Append the characters to the buffer
+    this.value.append(value);
+  }
+
+}

Propchange: manifoldcf/branches/CONNECTORS-633/framework/core/src/main/java/org/apache/manifoldcf/core/fuzzyml/XMLStringParsingContext.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: manifoldcf/branches/CONNECTORS-633/framework/core/src/main/java/org/apache/manifoldcf/core/fuzzyml/XMLStringParsingContext.java
------------------------------------------------------------------------------
    svn:keywords = Id

Added: manifoldcf/branches/CONNECTORS-633/framework/core/src/main/java/org/apache/manifoldcf/core/fuzzyml/XMLWriterParsingContext.java
URL: http://svn.apache.org/viewvc/manifoldcf/branches/CONNECTORS-633/framework/core/src/main/java/org/apache/manifoldcf/core/fuzzyml/XMLWriterParsingContext.java?rev=1444414&view=auto
==============================================================================
--- manifoldcf/branches/CONNECTORS-633/framework/core/src/main/java/org/apache/manifoldcf/core/fuzzyml/XMLWriterParsingContext.java (added)
+++ manifoldcf/branches/CONNECTORS-633/framework/core/src/main/java/org/apache/manifoldcf/core/fuzzyml/XMLWriterParsingContext.java Sat Feb  9 19:48:46 2013
@@ -0,0 +1,188 @@
+/* $Id$ */
+
+/**
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements. See the NOTICE file distributed with
+* this work for additional information regarding copyright ownership.
+* The ASF licenses this file to You under the Apache License, Version 2.0
+* (the "License"); you may not use this file except in compliance with
+* the License. You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+package org.apache.manifoldcf.core.fuzzyml;
+
+import org.apache.manifoldcf.core.interfaces.*;
+import java.io.*;
+import java.util.*;
+
+/** An instance of this class represents a parsing context within a node.  Data is written to the supplied writer.
+*/
+public class XMLWriterParsingContext extends XMLParsingContext
+{
+  /** The writer */
+  protected Writer theWriter;
+
+  /** Full constructor.  Used for individual tags. */
+  public XMLWriterParsingContext(XMLFuzzyHierarchicalParseState theStream, String namespace, String localname, String qname, Map<String,String> theseAttributes, Writer writer)
+    throws ManifoldCFException
+  {
+    super(theStream,namespace,localname,qname,theseAttributes);
+    theWriter = writer;
+  }
+
+  /** Flush the data to the underlying output stream */
+  public void flush()
+    throws ManifoldCFException
+  {
+    try
+    {
+      // Flush the data to the underlying output stream
+      theWriter.flush();
+    }
+    catch (java.net.SocketTimeoutException e)
+    {
+      throw new ManifoldCFException("Socket timeout exception: "+e.getMessage(),e);
+    }
+    catch (InterruptedIOException e)
+    {
+      throw new ManifoldCFException("Interrupted: "+e.getMessage(),e,ManifoldCFException.INTERRUPTED);
+    }
+    catch (IOException e)
+    {
+      throw new ManifoldCFException("IO exception: "+e.getMessage(),e);
+    }
+  }
+
+  /** This method is meant to be extended by classes that extend this class */
+  @Override
+  protected void tagContents(String body)
+    throws ManifoldCFException
+  {
+    try
+    {
+      escapeCharData(body,theWriter);
+    }
+    catch (java.net.SocketTimeoutException e)
+    {
+      throw new ManifoldCFException("Socket timeout exception: "+e.getMessage(),e);
+    }
+    catch (InterruptedIOException e)
+    {
+      throw new ManifoldCFException("Interrupted: "+e.getMessage(),e,ManifoldCFException.INTERRUPTED);
+    }
+    catch (IOException e)
+    {
+      throw new ManifoldCFException("IO exception: "+e.getMessage(),e);
+    }
+  }
+
+  /** Start a child tag.
+  * The XMLWriterContext accepts all subtags in their text form.
+  */
+  @Override
+  protected XMLParsingContext beginTag(String namespace, String localName, String qName, Map<String,String> atts)
+    throws ManifoldCFException
+  {
+    // First, write out the tag text.  We strip off the namespace.
+    try
+    {
+      theWriter.write("<"+localName);
+      for (String attName : atts.keySet())
+      {
+        String attValue = atts.get(attName);
+        theWriter.write(" ");
+        theWriter.write(attName);
+        theWriter.write("=\"");
+        theWriter.write(escapeAttribute(attValue));
+        theWriter.write("\"");
+      }
+      theWriter.write(">");
+    }
+    catch (java.net.SocketTimeoutException e)
+    {
+      throw new ManifoldCFException("Socket timeout exception: "+e.getMessage(),e);
+    }
+    catch (InterruptedIOException e)
+    {
+      throw new ManifoldCFException("Interrupted: "+e.getMessage(),e,ManifoldCFException.INTERRUPTED);
+    }
+    catch (IOException e)
+    {
+      throw new ManifoldCFException("IO exception: "+e.getMessage(),e);
+    }
+    // Now, start a new context which is also a writer context.
+    super.beginTag(namespace,localName,qName,atts);
+    return new XMLWriterParsingContext(theStream,namespace,localName,qName,atts,theWriter);
+  }
+
+  /** End a child tag.
+  * The XMLWriterContext accepts all subtags in their text form.
+  */
+  @Override
+  protected void endTag()
+    throws ManifoldCFException
+  {
+    // First, write out the tag text.  We strip off the namespace.
+    try
+    {
+      XMLParsingContext context = theStream.getContext();
+      String tagName = context.getLocalname();
+
+      theWriter.write("</"+tagName+">");
+    }
+    catch (java.net.SocketTimeoutException e)
+    {
+      throw new ManifoldCFException("Socket timeout exception: "+e.getMessage(),e);
+    }
+    catch (InterruptedIOException e)
+    {
+      throw new ManifoldCFException("Interrupted: "+e.getMessage(),e,ManifoldCFException.INTERRUPTED);
+    }
+    catch (IOException e)
+    {
+      throw new ManifoldCFException("IO exception: "+e.getMessage(),e);
+    }
+    super.endTag();
+  }
+
+  /** Convert a string to a value that's safe to include inside an attribute value */
+  protected static String escapeAttribute(String value)
+  {
+    StringBuilder rval = new StringBuilder();
+    int i = 0;
+    while (i < value.length())
+    {
+      char x = value.charAt(i++);
+      if (x == '\'' || x == '"' || x == '<' || x == '>' || x == '&'|| (x < ' ' && x >= 0))
+      {
+        rval.append("&#").append(Integer.toString((int)x)).append(";");
+      }
+      else
+        rval.append(x);
+    }
+    return rval.toString();
+  }
+  
+  /** Escapes sequence of characters to output writer */
+  protected static void escapeCharData(String body, Writer out) throws IOException
+  {
+    for (int i=0; i<body.length(); i++) {
+      char x = body.charAt(i);
+      if (x == '<' || x == '>' || x == '&'|| (x < ' ' && x >= 0))
+      {
+        StringBuilder rval = new StringBuilder();
+        rval.append("&#").append(Integer.toString((int)x)).append(";");
+        out.write(rval.toString());
+        continue;
+      }
+      out.write(x);
+    }
+  }
+}

Propchange: manifoldcf/branches/CONNECTORS-633/framework/core/src/main/java/org/apache/manifoldcf/core/fuzzyml/XMLWriterParsingContext.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: manifoldcf/branches/CONNECTORS-633/framework/core/src/main/java/org/apache/manifoldcf/core/fuzzyml/XMLWriterParsingContext.java
------------------------------------------------------------------------------
    svn:keywords = Id