You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by bd...@apache.org on 2003/10/14 13:53:18 UTC

cvs commit: cocoon-2.1/src/blocks/slop/java/org/apache/cocoon/slop/parsing SimpleSlopParser.java

bdelacretaz    2003/10/14 04:53:18

  Modified:    src/blocks/slop/java/org/apache/cocoon/slop/generation
                        SlopGenerator.java
               src/blocks/slop/java/org/apache/cocoon/slop/interfaces
                        SlopConstants.java
               src/blocks/slop/java/org/apache/cocoon/slop/parsing
                        SimpleSlopParser.java
  Log:
  New parameters for SlopGenerator: preserve-space and valid-tagname-chars
  
  Revision  Changes    Path
  1.4       +12 -2     cocoon-2.1/src/blocks/slop/java/org/apache/cocoon/slop/generation/SlopGenerator.java
  
  Index: SlopGenerator.java
  ===================================================================
  RCS file: /home/cvs/cocoon-2.1/src/blocks/slop/java/org/apache/cocoon/slop/generation/SlopGenerator.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- SlopGenerator.java	4 Sep 2003 09:38:32 -0000	1.3
  +++ SlopGenerator.java	14 Oct 2003 11:53:18 -0000	1.4
  @@ -84,6 +84,8 @@
       private Source inputSource = null;
       private String encoding = null;
       private SlopParser parser = null;
  +    private boolean preserveSpace = false;
  +    private String validTagnameChars = null;
   
       /**
        * Recycle this component.
  @@ -95,7 +97,9 @@
           }
           inputSource = null;
           encoding = null;
  +        preserveSpace = false;
           parser = null;
  +        validTagnameChars = null;
   
           super.recycle();
       }
  @@ -118,8 +122,14 @@
           super.setup(resolver, objectmodel, src, parameters);
           try {
               encoding = parameters.getParameter("encoding", null);
  +            preserveSpace = parameters.getParameterAsBoolean("preserve-space",false);
  +            validTagnameChars = parameters.getParameter("valid-tagname-chars",null);
               inputSource = resolver.resolveURI(src);
  -            parser = new SimpleSlopParser();
  +
  +            final SimpleSlopParser ssp = new SimpleSlopParser();
  +            parser = ssp;
  +            ssp.setPreserveWhitespace(preserveSpace);
  +            ssp.setValidTagnameChars(validTagnameChars);
           } catch (SourceException se) {
               throw new ProcessingException("Error during resolving of '" + src + "'.", se);
           }
  
  
  
  1.2       +4 -1      cocoon-2.1/src/blocks/slop/java/org/apache/cocoon/slop/interfaces/SlopConstants.java
  
  Index: SlopConstants.java
  ===================================================================
  RCS file: /home/cvs/cocoon-2.1/src/blocks/slop/java/org/apache/cocoon/slop/interfaces/SlopConstants.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- SlopConstants.java	6 Aug 2003 12:59:13 -0000	1.1
  +++ SlopConstants.java	14 Oct 2003 11:53:18 -0000	1.2
  @@ -64,4 +64,7 @@
       String SLOP_ROOT_ELEMENT = "parsed-text";
       String SLOP_LINE_ELEMENT = "line";
       String SLOP_EMPTY_LINE_ELEMENT = "empty-line";
  +
  +    // attribute names
  +    String SLOP_ATTR_LINENUMBER = "line-number";
   }
  
  
  
  1.2       +55 -6     cocoon-2.1/src/blocks/slop/java/org/apache/cocoon/slop/parsing/SimpleSlopParser.java
  
  Index: SimpleSlopParser.java
  ===================================================================
  RCS file: /home/cvs/cocoon-2.1/src/blocks/slop/java/org/apache/cocoon/slop/parsing/SimpleSlopParser.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- SimpleSlopParser.java	6 Aug 2003 12:59:13 -0000	1.1
  +++ SimpleSlopParser.java	14 Oct 2003 11:53:18 -0000	1.2
  @@ -78,7 +78,18 @@
       private ContentHandler contentHandler;
   
       /** chars that can be part of a field name (other than letters) */
  -    private final static String FIELD_CHARS = "-_";
  +    private final static String DEFAULT_TAGNAME_CHARS = "-_";
  +    private String tagnameChars = DEFAULT_TAGNAME_CHARS;
  +
  +    /** valid characters in an XML element name (in addition to letters and digits) */
  +    final static String VALID_TAGNAME_CHARS = "_-";
  +    final static String TAGNAME_REPLACEMENT_CHAR = "_";
  +
  +    /** optionally preserve whitespace in input */
  +    private boolean preserveSpace = false;
  +
  +    /** count lines */
  +    private int lineCounter;
   
       /** result of parsing a line */
       static class ParsedLine {
  @@ -86,11 +97,39 @@
           final String contents;
   
           ParsedLine(String elementName, String elementContents) {
  -            name = elementName;
  +            name = filterElementName(elementName);
               contents = elementContents;
           }
       }
   
  +    /** make sure element names are valid XML */
  +    static String filterElementName(String str) {
  +        final StringBuffer sb = new StringBuffer();
  +        for(int i=0; i < str.length(); i++) {
  +            final char c = str.charAt(i);
  +            if(Character.isLetter(c)) {
  +                sb.append(c);
  +            } else if(Character.isDigit(c) && i > 0) {
  +                sb.append(c);
  +            } else if(VALID_TAGNAME_CHARS.indexOf(c) >= 0) {
  +                sb.append(c);
  +            } else {
  +                sb.append(TAGNAME_REPLACEMENT_CHAR);
  +            }
  +        }
  +        return sb.toString();
  +    }
  +
  +    /** set the list of valid chars for tag names (in addition to letters) */
  +    public void setValidTagnameChars(String str) {
  +        tagnameChars = (str == null ? DEFAULT_TAGNAME_CHARS : str.trim());
  +    }
  +
  +    /** optionally preserve whitespace in input */
  +    public void setPreserveWhitespace(boolean b) {
  +        preserveSpace = b;
  +    }
  +
       /** must be called before any call to processLine() */
       public void startDocument(ContentHandler destination)
           throws SAXException, ProcessingException {
  @@ -110,6 +149,12 @@
           contentHandler = null;
       }
   
  +    /** add simple name-value attribute to attr */
  +    private void setAttribute(AttributesImpl attr,String name,String value) {
  +        final String ATTR_TYPE = "NMTOKEN";
  +        attr.addAttribute("",name,name,ATTR_TYPE,value);
  +    }
  +
       /** call this to process input lines, does the actual parsing */
       public void processLine(String line)
           throws SAXException, ProcessingException {
  @@ -121,7 +166,9 @@
           final ParsedLine p = parseLine(line);
   
           // generate the element and its contents
  +        lineCounter++;
           final AttributesImpl atts = new AttributesImpl();
  +        setAttribute(atts,SLOP_ATTR_LINENUMBER,String.valueOf(lineCounter));
           contentHandler.startElement(SLOP_NAMESPACE_URI, p.name, p.name, atts);
           contentHandler.characters(p.contents.toCharArray(),0,p.contents.length());
           contentHandler.endElement(SLOP_NAMESPACE_URI, p.name, p.name);
  @@ -149,7 +196,7 @@
                   boolean fieldFound = true;
                   for(int i=0; i < colonPos; i++) {
                       final char c = line.charAt(i);
  -                    final boolean isFieldChar = Character.isLetter(c) || FIELD_CHARS.indexOf(c) >= 0;
  +                    final boolean isFieldChar = Character.isLetter(c) || tagnameChars.indexOf(c) >= 0;
                       if(!isFieldChar) {
                           fieldFound = false;
                           break;
  @@ -159,7 +206,8 @@
                   if(fieldFound) {
                       String contents = "";
                       if(line.length() > colonPos + 1) {
  -                        contents = line.substring(colonPos+1).trim();
  +                        final String str = line.substring(colonPos+1);
  +                        contents = (preserveSpace ? str : str.trim());
                       }
                       result = new ParsedLine(line.substring(0,colonPos),contents);
                   }
  @@ -168,7 +216,8 @@
   
           // default: output a line element
           if(result == null) {
  -            result = new ParsedLine(SLOP_LINE_ELEMENT,line.trim());
  +            final String str = (preserveSpace ? line : line.trim());
  +            result = new ParsedLine(SLOP_LINE_ELEMENT,str);
           }
   
           return result;