You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@uima.apache.org by al...@apache.org on 2006/11/30 21:33:40 UTC

svn commit: r481060 - in /incubator/uima/uimaj/trunk/uimaj-core/src/main: java/org/apache/uima/util/ resources/org/apache/uima/util/

Author: alally
Date: Thu Nov 30 12:33:37 2006
New Revision: 481060

URL: http://svn.apache.org/viewvc?view=rev&rev=481060
Log:
UIMA-28: Removed SimpleXmlCasInitializer example since CAS Initializers
are now deprecated. Replaced with XmlDetagger.  Rewrote RunAE
to programatically create a CPE descriptor, and also moved it
to uimaj-tools.
https://issues.apache.org/jira/browse/UIMA-28

Added:
    incubator/uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/util/XmlDetagger.java
    incubator/uima/uimaj/trunk/uimaj-core/src/main/resources/org/apache/uima/util/XmlDetagger.xml
Removed:
    incubator/uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/util/RunAE.java
    incubator/uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/util/SimpleXmlCasInitializer.java
    incubator/uima/uimaj/trunk/uimaj-core/src/main/resources/org/apache/uima/util/SimpleXmlCasInitializer.xml
Modified:
    incubator/uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/util/FileSystemCollectionReader.java
    incubator/uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/util/InlineXmlCasConsumer.java
    incubator/uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/util/XCasWriterCasConsumer.java
    incubator/uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/util/XmiWriterCasConsumer.java

Modified: incubator/uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/util/FileSystemCollectionReader.java
URL: http://svn.apache.org/viewvc/incubator/uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/util/FileSystemCollectionReader.java?view=diff&rev=481060&r1=481059&r2=481060
==============================================================================
--- incubator/uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/util/FileSystemCollectionReader.java (original)
+++ incubator/uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/util/FileSystemCollectionReader.java Thu Nov 30 12:33:37 2006
@@ -23,6 +23,7 @@
 import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.net.URL;
 import java.util.ArrayList;
 
 import org.apache.uima.UIMAFramework;
@@ -224,5 +225,8 @@
     return UIMAFramework.getXMLParser().parseCollectionReaderDescription(
             new XMLInputSource(descStream, null));
   }
-
+  
+  public static URL getDescriptorURL() {
+    return FileSystemCollectionReader.class.getResource("FileSystemCollectionReader.xml");
+  }
 }

Modified: incubator/uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/util/InlineXmlCasConsumer.java
URL: http://svn.apache.org/viewvc/incubator/uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/util/InlineXmlCasConsumer.java?view=diff&rev=481060&r1=481059&r2=481060
==============================================================================
--- incubator/uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/util/InlineXmlCasConsumer.java (original)
+++ incubator/uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/util/InlineXmlCasConsumer.java Thu Nov 30 12:33:37 2006
@@ -190,5 +190,8 @@
     return UIMAFramework.getXMLParser().parseCasConsumerDescription(
             new XMLInputSource(descStream, null));
   }
-
+  
+  public static URL getDescriptorURL() {
+    return InlineXmlCasConsumer.class.getResource("InlineXmlCasConsumer.xml");
+  }
 }

Modified: incubator/uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/util/XCasWriterCasConsumer.java
URL: http://svn.apache.org/viewvc/incubator/uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/util/XCasWriterCasConsumer.java?view=diff&rev=481060&r1=481059&r2=481060
==============================================================================
--- incubator/uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/util/XCasWriterCasConsumer.java (original)
+++ incubator/uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/util/XCasWriterCasConsumer.java Thu Nov 30 12:33:37 2006
@@ -169,4 +169,7 @@
             new XMLInputSource(descStream, null));
   }
 
+  public static URL getDescriptorURL() {
+    return XCasWriterCasConsumer.class.getResource("XCasWriterCasConsumer.xml");
+  }
 }

Modified: incubator/uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/util/XmiWriterCasConsumer.java
URL: http://svn.apache.org/viewvc/incubator/uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/util/XmiWriterCasConsumer.java?view=diff&rev=481060&r1=481059&r2=481060
==============================================================================
--- incubator/uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/util/XmiWriterCasConsumer.java (original)
+++ incubator/uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/util/XmiWriterCasConsumer.java Thu Nov 30 12:33:37 2006
@@ -164,4 +164,8 @@
     return UIMAFramework.getXMLParser().parseCasConsumerDescription(
             new XMLInputSource(descStream, null));
   }
+  
+  public static URL getDescriptorURL() {
+    return XmiWriterCasConsumer.class.getResource("XmiWriterCasConsumer.xml");
+  }  
 }

Added: incubator/uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/util/XmlDetagger.java
URL: http://svn.apache.org/viewvc/incubator/uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/util/XmlDetagger.java?view=auto&rev=481060
==============================================================================
--- incubator/uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/util/XmlDetagger.java (added)
+++ incubator/uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/util/XmlDetagger.java Thu Nov 30 12:33:37 2006
@@ -0,0 +1,160 @@
+/*
+ * 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.uima.util;
+
+import java.io.InputStream;
+import java.net.URL;
+import java.util.Iterator;
+
+import javax.xml.parsers.SAXParser;
+import javax.xml.parsers.SAXParserFactory;
+
+import org.apache.uima.UIMAFramework;
+import org.apache.uima.UimaContext;
+import org.apache.uima.analysis_component.CasAnnotator_ImplBase;
+import org.apache.uima.analysis_engine.AnalysisEngineDescription;
+import org.apache.uima.analysis_engine.AnalysisEngineProcessException;
+import org.apache.uima.cas.CAS;
+import org.apache.uima.cas.FeatureStructure;
+import org.apache.uima.cas.Type;
+import org.apache.uima.cas.TypeSystem;
+import org.apache.uima.resource.ResourceInitializationException;
+import org.xml.sax.Attributes;
+import org.xml.sax.SAXException;
+import org.xml.sax.helpers.DefaultHandler;
+
+/**
+ * A multi-sofa annotator that does XML detagging. Reads XML data from the input Sofa (named
+ * "xmlDocument"); this data can be stored in the CAS as a string or array, or it can be a URI to a
+ * remote file. The XML is parsed using the JVM's default parser, and the plain-text content is
+ * written to a new sofa called "plainTextDocument".
+ */
+public class XmlDetagger extends CasAnnotator_ImplBase {
+  /**
+   * Name of optional configuration parameter that contains the name of an XML tag that appears in
+   * the input file. Only text that falls within this XML tag will be considered part of the
+   * "document" that it is added to the CAS by this CAS Initializer. If not specified, the entire
+   * file will be considered the document.
+   */
+  public static final String PARAM_TEXT_TAG = "XmlTagContainingText";
+  
+  private SAXParserFactory parserFactory = SAXParserFactory.newInstance();
+
+  private Type sourceDocInfoType;
+
+  private String mXmlTagContainingText = null;
+
+    
+  public void initialize(UimaContext aContext) throws ResourceInitializationException {
+    super.initialize(aContext);
+    // Get config param setting
+    mXmlTagContainingText  = (String) getContext().getConfigParameterValue(PARAM_TEXT_TAG);
+  }
+
+  public void typeSystemInit(TypeSystem aTypeSystem) throws AnalysisEngineProcessException {
+    sourceDocInfoType = aTypeSystem.getType("org.apache.uima.examples.SourceDocumentInformation");
+  }
+
+  public void process(CAS aCAS) throws AnalysisEngineProcessException {
+    // get handle to CAS view containing XML document
+    CAS xmlCas = aCAS.getView("xmlDocument");
+    InputStream xmlStream = xmlCas.getSofa().getSofaDataStream();
+
+    // parse with detag handler
+    DetagHandler handler = new DetagHandler();
+    try {
+      SAXParser parser = parserFactory.newSAXParser();
+      parser.parse(xmlStream, handler);
+    } catch (Exception e) {
+      throw new AnalysisEngineProcessException(e);
+    }
+
+    // create the plain text view and set its document text
+    CAS plainTextView = aCAS.createView("plainTextDocument");
+    plainTextView.setDocumentText(handler.getDetaggedText());
+
+    // Index the SourceDocumentInformation object, if there is one, in the new sofa.
+    // This is needed by the SemanticSearchCasIndexer
+    Iterator iter = xmlCas.getAnnotationIndex(sourceDocInfoType).iterator();
+    if (iter.hasNext()) {
+      FeatureStructure sourceDocInfoFs = (FeatureStructure) iter.next();
+      plainTextView.getIndexRepository().addFS(sourceDocInfoFs);
+
+    }
+
+  }
+  
+  /**
+   * Parses and returns the descriptor for this Analysis Gnein. The descriptor is stored in the
+   * uima-core.jar file and located using the ClassLoader.
+   * 
+   * @return an object containing all of the information parsed from the descriptor.
+   * 
+   * @throws InvalidXMLException
+   *           if the descriptor is invalid or missing
+   */
+  public static AnalysisEngineDescription getDescription() throws InvalidXMLException {
+    InputStream descStream = XmlDetagger.class
+            .getResourceAsStream("XmlDetagger.xml");
+    return UIMAFramework.getXMLParser().parseAnalysisEngineDescription(
+            new XMLInputSource(descStream, null));
+  }  
+
+  public static URL getDescriptorURL() {
+    return XmlDetagger.class.getResource("XmlDetagger.xml");
+  }  
+
+  class DetagHandler extends DefaultHandler {
+    private StringBuffer detaggedText = new StringBuffer();
+    private boolean insideTextTag;
+
+    public DetagHandler() {
+      insideTextTag = (mXmlTagContainingText == null);
+    }
+        
+    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
+      if (qName.equalsIgnoreCase(mXmlTagContainingText)) {
+        insideTextTag = true;
+      }
+    }
+
+    public void endElement(String uri, String localName, String qName) throws SAXException {
+      if (qName.equalsIgnoreCase(mXmlTagContainingText)) {
+        insideTextTag = false;
+      }
+    }
+
+    public void characters(char[] ch, int start, int length) throws SAXException {
+      if (insideTextTag) {
+        detaggedText.append(ch, start, length);        
+      }
+    }
+    
+    public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
+      if (insideTextTag) {
+        detaggedText.append(ch, start, length);        
+      }
+    }
+
+    String getDetaggedText() {
+      return detaggedText.toString();
+    }
+  }
+}

Added: incubator/uima/uimaj/trunk/uimaj-core/src/main/resources/org/apache/uima/util/XmlDetagger.xml
URL: http://svn.apache.org/viewvc/incubator/uima/uimaj/trunk/uimaj-core/src/main/resources/org/apache/uima/util/XmlDetagger.xml?view=auto&rev=481060
==============================================================================
--- incubator/uima/uimaj/trunk/uimaj-core/src/main/resources/org/apache/uima/util/XmlDetagger.xml (added)
+++ incubator/uima/uimaj/trunk/uimaj-core/src/main/resources/org/apache/uima/util/XmlDetagger.xml Thu Nov 30 12:33:37 2006
@@ -0,0 +1,71 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+	<!--
+	 ***************************************************************
+	 * 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.
+	 ***************************************************************
+   -->
+   
+<analysisEngineDescription xmlns="http://uima.apache.org/resourceSpecifier">
+  <frameworkImplementation>org.apache.uima.java</frameworkImplementation>
+  <primitive>true</primitive>
+  <annotatorImplementationName>org.apache.uima.examples.XmlDetagger</annotatorImplementationName>
+	<analysisEngineMetaData>
+		<name>XML Detagger</name>
+		<description>A multi-sofa annotator that does XML detagging. Reads XML data from
+			the input Sofa (named "xmlDocument"); this data can be stored in the CAS as a
+			string or array, or it can be a URI to a remote file. The XML is parsed using the JVM's
+			default parser, and the plain-text content is written to a new sofa called
+			"plainTextDocument". </description>
+		<version>1.0</version>
+		<vendor>The Apache Software Foundation</vendor>
+		<configurationParameters>
+			<configurationParameter>
+				<name>XmlTagContainingText</name>
+				<description>The name of an XML tag that appears in the files. Only text that
+					falls within this XML tag will be considered part of the "document" that
+					is put into the CAS. If omitted, the entire file will be considered the
+					document.</description>
+				<type>String</type>
+				<multiValued>false</multiValued>
+				<mandatory>false</mandatory>
+			</configurationParameter>
+		</configurationParameters>
+		<typeSystemDescription/>
+		<capabilities>
+			<capability>
+				<inputs/>
+				<outputs/>
+				<inputSofas>
+					<sofaName>xmlDocument</sofaName>
+				</inputSofas>
+				<outputSofas>
+					<sofaName>plainTextDocument</sofaName>
+				</outputSofas>
+				<languagesSupported>
+					<language>en</language>
+				</languagesSupported>
+			</capability>
+		</capabilities>
+		<operationalProperties>
+			<modifiesCas>true</modifiesCas>
+			<multipleDeploymentAllowed>true</multipleDeploymentAllowed>
+			<outputsNewCASes>false</outputsNewCASes>
+		</operationalProperties>
+	</analysisEngineMetaData>
+</analysisEngineDescription>