You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lenya.apache.org by an...@apache.org on 2003/11/05 15:01:52 UTC

cvs commit: cocoon-lenya/src/java/org/apache/lenya/cms/cocoon/transformation DocumentIndexTransformer.java

andreas     2003/11/05 06:01:52

  Modified:    src/java/org/apache/lenya/cms/cocoon/transformation
                        DocumentIndexTransformer.java
  Log:
  - using namespace
  - some code simplifications
  
  Revision  Changes    Path
  1.4       +216 -246  cocoon-lenya/src/java/org/apache/lenya/cms/cocoon/transformation/DocumentIndexTransformer.java
  
  Index: DocumentIndexTransformer.java
  ===================================================================
  RCS file: /home/cvs/cocoon-lenya/src/java/org/apache/lenya/cms/cocoon/transformation/DocumentIndexTransformer.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- DocumentIndexTransformer.java	21 Oct 2003 09:51:55 -0000	1.3
  +++ DocumentIndexTransformer.java	5 Nov 2003 14:01:52 -0000	1.4
  @@ -57,6 +57,7 @@
   import java.io.File;
   import java.io.IOException;
   import java.util.ArrayList;
  +import java.util.List;
   import java.util.Map;
   
   import org.apache.cocoon.ProcessingException;
  @@ -95,251 +96,220 @@
    * 
    * @author edith
    */
  -public class DocumentIndexTransformer
  -	extends AbstractSAXTransformer
  -	implements Parameterizable {
  -
  -	private String namespace;
  -	private String cIncludeNamespace;
  -
  -	public static final String INDEX_ELEMENT = "index";
  -	public static final String ABSTRACT_ATTRIBUTE = "abstract";
  -
  -	/** (non-Javadoc)
  -		 * @see org.apache.avalon.framework.parameters.Parameterizable#parameterize(org.apache.avalon.framework.parameters.Parameters)
  -		 */
  -	public void parameterize(Parameters parameters) throws ParameterException {
  -		this.namespace = parameters.getParameter("namespace", null);
  -		this.cIncludeNamespace =
  -			parameters.getParameter("cIncludeNamespace", null);
  -	}
  -
  -	private Document document;
  -
  -	private Publication publication;
  -
  -	private String area;
  -
  -	private DocumentBuilder builder;
  -
  -	private SiteTree siteTree;
  -
  -	/** (non-Javadoc)
  -	 * @see org.apache.cocoon.sitemap.SitemapModelComponent#setup(org.apache.cocoon.environment.SourceResolver, java.util.Map, java.lang.String, org.apache.avalon.framework.parameters.Parameters)
  -	 */
  -	public void setup(
  -		SourceResolver resolver,
  -		Map objectModel,
  -		String src,
  -		Parameters parameters)
  -		throws ProcessingException, SAXException, IOException {
  -		try {
  -			parameterize(parameters);
  -
  -			PageEnvelope envelope = null;
  -			envelope =
  -				PageEnvelopeFactory.getInstance().getPageEnvelope(objectModel);
  -
  -			setDocument(envelope.getDocument());
  -			setPublication(document.getPublication());
  -			setArea(document.getArea());
  -			setBuilder(document.getPublication().getDocumentBuilder());
  -			setSiteTree(publication.getSiteTree(area));
  -
  -		} catch (Exception e) {
  -			throw new ProcessingException(e);
  -		}
  -
  -	}
  -
  -	/** (non-Javadoc)
  -	 * @see org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
  -	 */
  -	public void startElement(
  -		String uri,
  -		String localName,
  -		String raw,
  -		Attributes attr)
  -		throws SAXException {
  -
  -		if (uri != null
  -			&& cIncludeNamespace != null
  -			&& uri.equals(namespace)) {
  -			if (localName.equals(INDEX_ELEMENT) == true) {
  -
  -				String cIncludePrefix = "";
  -				if (!this.cIncludeNamespace.equals("")) {
  -					cIncludePrefix = "ci:";
  -				}
  -
  -				String documentId = document.getId();
  -				String language = document.getLanguage();
  -				String defaultLanguage = publication.getDefaultLanguage();
  -				SiteTreeNode[] children =
  -					siteTree.getNode(documentId).getChildren();
  -
  -				super.startElement(uri, localName, raw, attr);
  -
  -				for (int i = 0; i < children.length; i++) {
  -					String childId =
  -						documentId + File.separator + children[i].getId();
  -
  -					//get child document with the same language than the parent document
  -					String url =
  -						builder.buildCanonicalUrl(
  -							publication,
  -							area,
  -							childId,
  -							language);
  -					Document doc;
  -					try {
  -						doc = builder.buildDocument(publication, url);
  -					} catch (DocumentBuildException e) {
  -						throw new SAXException(e);
  -					}
  -					File file = doc.getFile();
  -
  -					if (!file.exists()) {
  -						//get first the child document in the default language and then in any other existent language
  -						getLogger().debug(
  -							"There are no child file "
  -								+ file.getAbsolutePath()
  -								+ " in the same language than the parent document"
  -								+ language);
  -
  -						//available language    
  -						String[] availableLanguages = null;
  -						try {
  -							availableLanguages = doc.getLanguages();
  -						} catch (DocumentException e) {
  -							throw new SAXException(e);
  -						}
  -
  -						ArrayList languages = new ArrayList();
  -						for (int l = 0; l < availableLanguages.length; l++) {
  -							if (availableLanguages[l].equals(language)) {
  -								getLogger().debug(
  -									"do nothing because language was already tested"
  -										+ availableLanguages[l]);
  -							} else if (
  -								availableLanguages[l].equals(
  -									defaultLanguage)) {
  -								languages.add(0, availableLanguages[l]);
  -							} else {
  -								languages.add(availableLanguages[l]);
  -
  -							}
  -						}
  -
  -						int j = 0;
  -						while (!file.exists() && j < languages.size()) {
  -							String newlanguage = (String) languages.get(j);
  -							url =
  -								builder.buildCanonicalUrl(
  -									publication,
  -									area,
  -									childId,
  -									newlanguage);
  -							try {
  -								doc = builder.buildDocument(publication, url);
  -							} catch (DocumentBuildException e) {
  -								throw new SAXException(e);
  -							}
  -							file = doc.getFile();
  -
  -							j = j + 1;
  -						}
  -					}
  -
  -					if (file.exists()) {
  -						//create the tags for the child
  -						String path;
  -						try {
  -							path = file.getCanonicalPath();
  -						} catch (IOException e) {
  -							throw new SAXException(e);
  -						}
  -
  -						AttributesImpl attribute = new AttributesImpl();
  -						attribute.addAttribute("", "href", "href", "", url);
  -						super.startElement("", "child", "child", attribute);
  -
  -						AttributesImpl attributes = new AttributesImpl();
  -						attributes.addAttribute("", "src", "src", "", path);
  -						attributes.addAttribute(
  -							"",
  -							"element",
  -							"element",
  -							"",
  -							"included");
  -
  -						super.startElement(
  -							this.cIncludeNamespace,
  -							"include",
  -							cIncludePrefix + "include",
  -							attributes);
  -						super.endElement(
  -							this.cIncludeNamespace,
  -							"include",
  -							cIncludePrefix + "include");
  -						super.endElement("", "child", "child");
  -					} else {
  -						//do nothing for this child
  -						getLogger().warn(
  -							"There are no existing file for the child with id "
  -								+ childId);
  -					}
  -
  -				}
  -			} else {
  -				super.startElement(uri, localName, raw, attr);
  -			}
  -		} else {
  -			super.startElement(uri, localName, raw, attr);
  -		}
  -
  -	}
  -
  -	/**
  -	 * @return SiteTree The siteTree belonging to the area of the document
  -	 */
  -	public SiteTree getSiteTree() {
  -		return siteTree;
  -	}
  -
  -	/**
  -	 * @param tree The siteTree of the area, which the document belongs.
  -	 */
  -	public void setSiteTree(SiteTree tree) {
  -		siteTree = tree;
  -	}
  -
  -	/**
  -	 * @param string The area, which the document belongs.
  -	 */
  -	public void setArea(String string) {
  -		area = string;
  -	}
  -
  -	/**
  -	 * @param builder The document builder.
  -	 */
  -	public void setBuilder(DocumentBuilder builder) {
  -		this.builder = builder;
  -	}
  -
  -	/**
  -	 * @param document The document.
  -	 */
  -	public void setDocument(Document document) {
  -		this.document = document;
  -	}
  -
  -	/**
  -	 * @param publication The publication, which the document belongs.
  -	 */
  -	public void setPublication(Publication publication) {
  -		this.publication = publication;
  -	}
  +public class DocumentIndexTransformer extends AbstractSAXTransformer implements Parameterizable {
  +
  +    private String namespace;
  +    private String cIncludeNamespace;
  +
  +    public static final String INDEX_ELEMENT = "index";
  +    public static final String ABSTRACT_ATTRIBUTE = "abstract";
  +    
  +    public static final String NAMESPACE = "http://apache.org/cocoon/lenya/documentindex/1.0";
  +    public static final String PREFIX = "index:";
  +
  +    /** (non-Javadoc)
  +    	 * @see org.apache.avalon.framework.parameters.Parameterizable#parameterize(org.apache.avalon.framework.parameters.Parameters)
  +    	 */
  +    public void parameterize(Parameters parameters) throws ParameterException {
  +        this.namespace = parameters.getParameter("namespace", null);
  +        this.cIncludeNamespace = parameters.getParameter("cIncludeNamespace", null);
  +    }
  +
  +    private Document document;
  +
  +    private Publication publication;
  +
  +    private String area;
  +
  +    private DocumentBuilder builder;
  +
  +    private SiteTree siteTree;
  +
  +    /** (non-Javadoc)
  +     * @see org.apache.cocoon.sitemap.SitemapModelComponent#setup(org.apache.cocoon.environment.SourceResolver, java.util.Map, java.lang.String, org.apache.avalon.framework.parameters.Parameters)
  +     */
  +    public void setup(SourceResolver resolver, Map objectModel, String src, Parameters parameters)
  +        throws ProcessingException, SAXException, IOException {
  +        try {
  +            parameterize(parameters);
  +
  +            PageEnvelope envelope = null;
  +            envelope = PageEnvelopeFactory.getInstance().getPageEnvelope(objectModel);
  +
  +            setDocument(envelope.getDocument());
  +            setPublication(document.getPublication());
  +            setArea(document.getArea());
  +            setBuilder(document.getPublication().getDocumentBuilder());
  +            setSiteTree(publication.getSiteTree(area));
  +
  +        } catch (Exception e) {
  +            throw new ProcessingException(e);
  +        }
  +
  +    }
  +
  +    /** (non-Javadoc)
  +     * @see org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
  +     */
  +    public void startElement(String uri, String localName, String raw, Attributes attr)
  +        throws SAXException {
  +
  +        if (uri != null
  +            && uri.equals(namespace)
  +            && cIncludeNamespace != null
  +            && localName.equals(INDEX_ELEMENT)) {
  +                
  +            if (getLogger().isInfoEnabled()) {
  +                getLogger().info("Inserting index");
  +            }
  +
  +            String cIncludePrefix = "";
  +            if (!this.cIncludeNamespace.equals("")) {
  +                cIncludePrefix = "ci:";
  +            }
  +
  +            String documentId = document.getId();
  +            String language = document.getLanguage();
  +            String defaultLanguage = publication.getDefaultLanguage();
  +            SiteTreeNode[] children = siteTree.getNode(documentId).getChildren();
  +
  +            super.startElement(uri, localName, raw, attr);
  +
  +            for (int i = 0; i < children.length; i++) {
  +                String childId = documentId + "/" + children[i].getId();
  +
  +                //get child document with the same language than the parent document
  +                String url = builder.buildCanonicalUrl(publication, area, childId, language);
  +                Document doc;
  +                try {
  +                    doc = builder.buildDocument(publication, url);
  +                } catch (DocumentBuildException e) {
  +                    throw new SAXException(e);
  +                }
  +                File file = doc.getFile();
  +
  +                if (!file.exists()) {
  +                    //get first the child document in the default language and then in any other existent language
  +                    getLogger().debug(
  +                        "There is no child file "
  +                            + file.getAbsolutePath()
  +                            + " in the same language as the parent document ["
  +                            + language
  +                            + "]");
  +
  +                    //available language    
  +                    String[] availableLanguages = null;
  +                    try {
  +                        availableLanguages = doc.getLanguages();
  +                    } catch (DocumentException e) {
  +                        throw new SAXException(e);
  +                    }
  +
  +                    List languages = new ArrayList();
  +                    for (int l = 0; l < availableLanguages.length; l++) {
  +                        if (availableLanguages[l].equals(language)) {
  +                            getLogger().debug(
  +                                "Do nothing because language was already tested: ["
  +                                    + availableLanguages[l]
  +                                    + "]");
  +                        } else if (availableLanguages[l].equals(defaultLanguage)) {
  +                            languages.add(0, availableLanguages[l]);
  +                        } else {
  +                            languages.add(availableLanguages[l]);
  +                        }
  +                    }
  +
  +                    int j = 0;
  +                    while (!file.exists() && j < languages.size()) {
  +                        String newlanguage = (String) languages.get(j);
  +                        url = builder.buildCanonicalUrl(publication, area, childId, newlanguage);
  +                        try {
  +                            doc = builder.buildDocument(publication, url);
  +                        } catch (DocumentBuildException e) {
  +                            throw new SAXException(e);
  +                        }
  +                        file = doc.getFile();
  +
  +                        j++;
  +                    }
  +                }
  +
  +                if (file.exists()) {
  +                    //create the tags for the child
  +                    String path;
  +                    try {
  +                        path = file.getCanonicalPath();
  +                    } catch (IOException e) {
  +                        throw new SAXException(e);
  +                    }
  +
  +                    AttributesImpl attribute = new AttributesImpl();
  +                    attribute.addAttribute("", "href", "href", "", url);
  +                    super.startElement(NAMESPACE, "child", PREFIX + "child", attribute);
  +
  +                    AttributesImpl attributes = new AttributesImpl();
  +                    attributes.addAttribute("", "src", "src", "", path);
  +                    attributes.addAttribute("", "element", "element", "", "included");
  +
  +                    super.startElement(
  +                        this.cIncludeNamespace,
  +                        "include",
  +                        cIncludePrefix + "include",
  +                        attributes);
  +                    super.endElement(this.cIncludeNamespace, "include", cIncludePrefix + "include");
  +                    super.endElement(NAMESPACE, "child", PREFIX + "child");
  +                } else {
  +                    //do nothing for this child
  +                    getLogger().warn("There are no existing file for the child with id " + childId);
  +                }
  +
  +            }
  +        } else {
  +            super.startElement(uri, localName, raw, attr);
  +        }
  +
  +    }
  +
  +    /**
  +     * @return SiteTree The siteTree belonging to the area of the document
  +     */
  +    public SiteTree getSiteTree() {
  +        return siteTree;
  +    }
  +
  +    /**
  +     * @param tree The siteTree of the area, which the document belongs.
  +     */
  +    public void setSiteTree(SiteTree tree) {
  +        siteTree = tree;
  +    }
  +
  +    /**
  +     * @param string The area, which the document belongs.
  +     */
  +    public void setArea(String string) {
  +        area = string;
  +    }
  +
  +    /**
  +     * @param builder The document builder.
  +     */
  +    public void setBuilder(DocumentBuilder builder) {
  +        this.builder = builder;
  +    }
  +
  +    /**
  +     * @param document The document.
  +     */
  +    public void setDocument(Document document) {
  +        this.document = document;
  +    }
  +
  +    /**
  +     * @param publication The publication, which the document belongs.
  +     */
  +    public void setPublication(Publication publication) {
  +        this.publication = publication;
  +    }
   
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: lenya-cvs-unsubscribe@cocoon.apache.org
For additional commands, e-mail: lenya-cvs-help@cocoon.apache.org