Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/nwalsh/saxon/Text.java


1   // Text - Saxon extension element for inserting text
2   
3   package com.nwalsh.saxon;
4   
5   import java.io.BufferedReader;
6   import java.io.InputStreamReader;
7   import java.io.InputStream;
8   import java.io.IOException;
9   import java.io.FileNotFoundException;
10  import java.net.URL;
11  import java.net.MalformedURLException;
12  import javax.xml.transform.TransformerException;
13  import javax.xml.transform.TransformerConfigurationException;
14  import com.icl.saxon.*;
15  import com.icl.saxon.style.*;
16  import com.icl.saxon.expr.*;
17  import com.icl.saxon.output.*;
18  import org.xml.sax.AttributeList;
19  
20  /**
21   * <p>Saxon extension element for inserting text
22   *
23   *
24   * <p>Copyright (C) 2000 Norman Walsh.</p>
25   *
26   * <p>This class provides a
27   * <a href="http://users.iclway.co.uk/mhkay/saxon/">Saxon</a>
28   * extension element for inserting text into a result tree.</p>
29   *
30   * <p><b>Change Log:</b></p>
31   * <dl>
32   * <dt>1.0</dt>
33   * <dd><p>Initial release.</p></dd>
34   * </dl>
35   *
36   * @author Norman Walsh
37   * <a href="mailto:ndw@nwalsh.com">ndw@nwalsh.com</a>
38   *
39   *
40   */
41  public class Text extends StyleElement {
42    /**
43     * <p>Constructor for Text</p>
44     *
45     * <p>Does nothing.</p>
46     */
47    public Text() {
48    }
49  
50    /**
51     * <p>Is this element an instruction?</p>
52     *
53     * <p>Yes, it is.</p>
54     *
55     * @return true
56     */
57    public boolean isInstruction() {
58      return true;
59    }
60  
61      /**
62      * <p>Can this element contain a template-body?</p>
63      *
64      * <p>Yes, it can, but only so that it can contain xsl:fallback.</p>
65      *
66      * @return true
67      */
68    public boolean mayContainTemplateBody() {
69      return true;
70    }
71  
72    /**
73     * <p>Validate the arguments</p>
74     *
75     * <p>The element must have an href attribute.</p>
76     */
77    public void prepareAttributes() throws TransformerConfigurationException {
78      // Get mandatory href attribute
79      String fnAtt = getAttribute("href");
80      if (fnAtt == null) {
81        reportAbsence("href");
82      }
83    }
84  
85    /** Validate that the element occurs in a reasonable place. */
86    public void validate() throws TransformerConfigurationException {
87      checkWithinTemplate();
88    }
89  
90    /**
91     * <p>Insert the text of the file into the result tree</p>
92     *
93     * <p>Processing this element inserts the contents of the URL named
94     * by the href attribute into the result tree as plain text.</p>
95     *
96     */
97    public void process( Context context ) throws TransformerException {
98      Outputter out = context.getOutputter();
99  
100     String hrefAtt = getAttribute("href");
101     Expression hrefExpr = makeAttributeValueTemplate(hrefAtt);
102     String href = hrefExpr.evaluateAsString(context);
103     URL fileURL = null;
104 
105     try {
106       try {
107   fileURL = new URL(href);
108       } catch (MalformedURLException e1) {
109   try {
110     fileURL = new URL("file:" + href);
111   } catch (MalformedURLException e2) {
112     System.out.println("Cannot open " + href);
113     return;
114   }
115       }
116 
117       InputStreamReader isr = new InputStreamReader(fileURL.openStream());
118       BufferedReader is = new BufferedReader(isr);
119 
120       char chars[] = new char[4096];
121       int len = 0;
122       while ((len = is.read(chars)) > 0) {
123   out.writeContent(chars, 0, len);
124       }
125       is.close();
126     } catch (Exception e) {
127       System.out.println("Cannot read " + href);
128     }
129   }
130 }