abdera-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jmsn...@apache.org
Subject svn commit: r548134 - in /incubator/abdera/java/trunk: core/src/main/java/org/apache/abdera/model/ core/src/main/java/org/apache/abdera/parser/ core/src/main/java/org/apache/abdera/util/ parser/src/main/java/org/apache/abdera/parser/stax/
Date Sun, 17 Jun 2007 23:12:15 GMT
Author: jmsnell
Date: Sun Jun 17 16:12:12 2007
New Revision: 548134

URL: http://svn.apache.org/viewvc?view=rev&rev=548134
Log:
Support for xml:space="preserve".  By default, the parser will preserve whitespace in elements.
 This adds a new option to ParserOptions to trim leading and trailing
whitespace off element text and attribute values.  This option will only have an effect if
there is not an in-scope xml:space="preserve".

For instance,

  ParserOptions options = parser.getDefaultParserOptions();
  options.setMustPreserveWhitespace(false);

  Document<Entry> doc = parser.parse(in, null, options);
  Entry entry = doc.getRoot();
  System.out.println(entry.getTitle()); 
 
   // unless xml:space is specified, leading and trailing whitespace will be trimmed from
the title
 

Modified:
    incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/model/Document.java
    incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/model/Element.java
    incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/model/ElementWrapper.java
    incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/model/ExtensibleElementWrapper.java
    incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/parser/ParserOptions.java
    incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/AbstractParserOptions.java
    incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/Constants.java
    incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMDocument.java
    incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMElement.java
    incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMParser.java

Modified: incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/model/Document.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/model/Document.java?view=diff&rev=548134&r1=548133&r2=548134
==============================================================================
--- incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/model/Document.java (original)
+++ incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/model/Document.java Sun
Jun 17 16:12:12 2007
@@ -161,4 +161,8 @@
    */
   void setSlug(String slug);
   
+  boolean getMustPreserveWhitespace();
+  
+  void setMustPreserveWhitespace(boolean preserve);
+  
 }

Modified: incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/model/Element.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/model/Element.java?view=diff&rev=548134&r1=548133&r2=548134
==============================================================================
--- incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/model/Element.java (original)
+++ incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/model/Element.java Sun
Jun 17 16:12:12 2007
@@ -220,4 +220,7 @@
   
   <T extends Element>List<T> getElements();
   
+  boolean getMustPreserveWhitespace();
+  
+  void setMustPreserveWhitespace(boolean preserve);
 }

Modified: incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/model/ElementWrapper.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/model/ElementWrapper.java?view=diff&rev=548134&r1=548133&r2=548134
==============================================================================
--- incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/model/ElementWrapper.java
(original)
+++ incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/model/ElementWrapper.java
Sun Jun 17 16:12:12 2007
@@ -214,5 +214,14 @@
   public Map<String, String> getNamespaces() {
     return internal.getNamespaces();
   }
+
+  public boolean getMustPreserveWhitespace() {
+    return internal.getMustPreserveWhitespace();
+  }
+
+  public void setMustPreserveWhitespace(boolean preserve) {
+    internal.setMustPreserveWhitespace(preserve);
+  }
+  
   
 }

Modified: incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/model/ExtensibleElementWrapper.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/model/ExtensibleElementWrapper.java?view=diff&rev=548134&r1=548133&r2=548134
==============================================================================
--- incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/model/ExtensibleElementWrapper.java
(original)
+++ incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/model/ExtensibleElementWrapper.java
Sun Jun 17 16:12:12 2007
@@ -86,5 +86,13 @@
   public String getSimpleExtension(String namespace, String localPart, String prefix) {
     return getExtInternal().getSimpleExtension(namespace, localPart, prefix);
   }
+
+  public boolean getMustPreserveWhitespace() {
+    return getExtInternal().getMustPreserveWhitespace();
+  }
+
+  public void setMustPreserveWhitespace(boolean preserve) {
+    getExtInternal().setMustPreserveWhitespace(preserve);
+  }
   
 }

Modified: incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/parser/ParserOptions.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/parser/ParserOptions.java?view=diff&rev=548134&r1=548133&r2=548134
==============================================================================
--- incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/parser/ParserOptions.java
(original)
+++ incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/parser/ParserOptions.java
Sun Jun 17 16:12:12 2007
@@ -76,4 +76,15 @@
    */
   void setAutodetectCharset(boolean detect);
   
+  /**
+   * If false, the parser will trim leading and trailing whitespace in element
+   * and attribute values unless there is an in-scope xml:space="preserve".
+   */
+  boolean getMustPreserveWhitespace();
+  
+  /**
+   * If false, the parser will trim leading and trailing whitespace in element
+   * and attribute values unless there is an in-scope xml:space="preserve".
+   */
+  void setMustPreserveWhitespace(boolean preserve);
 }

Modified: incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/AbstractParserOptions.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/AbstractParserOptions.java?view=diff&rev=548134&r1=548133&r2=548134
==============================================================================
--- incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/AbstractParserOptions.java
(original)
+++ incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/AbstractParserOptions.java
Sun Jun 17 16:12:12 2007
@@ -31,6 +31,7 @@
   protected String charset = null;
   protected ParseFilter parseFilter = null;
   protected boolean detect = false;
+  protected boolean preserve = true;
 
   protected abstract void initFactory();
   protected abstract void checkFactory(Factory factory);
@@ -80,4 +81,13 @@
   public void setAutodetectCharset(boolean detect) {
     this.detect = detect;
   }
+  
+  public boolean getMustPreserveWhitespace() {
+    return preserve;
+  }
+  
+  public void setMustPreserveWhitespace(boolean preserve) {
+    this.preserve = preserve;
+  }
+  
 }

Modified: incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/Constants.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/Constants.java?view=diff&rev=548134&r1=548133&r2=548134
==============================================================================
--- incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/Constants.java (original)
+++ incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/Constants.java Sun
Jun 17 16:12:12 2007
@@ -85,6 +85,7 @@
   public static final String LN_HREF              = "href";
   public static final String LN_LANG              = "lang";
   public static final String LN_BASE              = "base";
+  public static final String LN_SPACE             = "space";
   public static final String LN_URI               = "uri";
   public static final String LN_VERSION           = "version";
   public static final String LN_REL               = "rel";
@@ -156,6 +157,7 @@
   public static final QName HREF                  = new QName(LN_HREF);
   public static final QName LANG                  = new QName(XML_NS, LN_LANG, "xml");
   public static final QName BASE                  = new QName(XML_NS, LN_BASE, "xml");
+  public static final QName SPACE                 = new QName(XML_NS, LN_SPACE, "xml");
   public static final QName AURI                  = new QName(LN_URI);
   public static final QName VERSION               = new QName(LN_VERSION);
   public static final QName REL                   = new QName(LN_REL);

Modified: incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMDocument.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMDocument.java?view=diff&rev=548134&r1=548133&r2=548134
==============================================================================
--- incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMDocument.java
(original)
+++ incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMDocument.java
Sun Jun 17 16:12:12 2007
@@ -61,6 +61,7 @@
   protected EntityTag etag = null;
   protected String language = null;
   protected String slug = null;
+  protected boolean preserve = true;
 
   public FOMDocument() {
     super();
@@ -271,5 +272,13 @@
   
   public void setSlug(String slug) {
     this.slug = slug;
+  }
+  
+  public boolean getMustPreserveWhitespace() {
+    return preserve;
+  }
+  
+  public void setMustPreserveWhitespace(boolean preserve) {
+    this.preserve = preserve;
   }
 }

Modified: incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMElement.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMElement.java?view=diff&rev=548134&r1=548133&r2=548134
==============================================================================
--- incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMElement.java
(original)
+++ incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMElement.java
Sun Jun 17 16:12:12 2007
@@ -269,7 +269,8 @@
   
   public String getAttributeValue(QName qname) {
     OMAttribute attr = getAttribute(qname);
-    return (attr != null) ? attr.getAttributeValue() : null;    
+    String value = (attr != null) ? attr.getAttributeValue() : null;
+    return getMustPreserveWhitespace() || value == null ? value : value.trim();
   }
   
   public void setAttributeValue(QName qname, String value) {
@@ -394,7 +395,7 @@
     OMElement element = this.getFirstChildWithName(qname);
     if (element != null)
       value = element.getText();
-    return value;
+    return getMustPreserveWhitespace() || value == null ? value : value.trim();
   }
 
   @SuppressWarnings("unchecked")
@@ -472,7 +473,8 @@
         // out all markup but leaving all text, even in child nodes
       }
     }
-    return buf.toString();
+    String value = buf.toString();
+    return getMustPreserveWhitespace() || value == null ? value : value.trim();
   }
   
   protected String getText(QName qname) {
@@ -717,5 +719,25 @@
     return new FOMList<T>(
         new FOMElementIteratorWrapper(
           (FOMFactory)factory,getChildElements()));
+  }
+  
+  public boolean getMustPreserveWhitespace() {
+    OMAttribute attr = getAttribute(SPACE);
+    String space = (attr != null) ? attr.getAttributeValue() : null;
+    Base parent = this.getParentElement();
+    
+    return space != null && space.equalsIgnoreCase("preserve") ? true :
+      (parent != null && parent instanceof Element) ? 
+        ((Element)parent).getMustPreserveWhitespace() : 
+      (parent != null && parent instanceof Document) ? 
+        ((Document)parent).getMustPreserveWhitespace() : true;
+  }
+  
+  public void setMustPreserveWhitespace(boolean preserve) {
+    if (preserve && !getMustPreserveWhitespace()) {
+      setAttributeValue(SPACE, "preserve");
+    } else if (!preserve && getMustPreserveWhitespace()) {
+      setAttributeValue(SPACE, "default");
+    }
   }
 }

Modified: incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMParser.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMParser.java?view=diff&rev=548134&r1=548133&r2=548134
==============================================================================
--- incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMParser.java
(original)
+++ incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMParser.java
Sun Jun 17 16:12:12 2007
@@ -70,6 +70,7 @@
         document.setBaseUri(base.toString());
       if (options != null && options.getCharset() != null)
         ((OMDocument)document).setCharsetEncoding(options.getCharset());
+      if (options != null) document.setMustPreserveWhitespace(options.getMustPreserveWhitespace());
     } catch (Exception e) {
       if (!(e instanceof ParseException))
         e = new ParseException(e);



Mime
View raw message