Package org.apache.jetspeed.om.page

Examples of org.apache.jetspeed.om.page.Document


    }

    protected Object unmarshallDocument( Class clazz, String path, String extension ) throws NodeNotFoundException,
            DocumentException
    {
        Document document = null;
        File f = null;
        if (path.endsWith(extension))
        {
            f = new File(this.documentRootDir, path);
        }
        else
        {
            f = new File(this.documentRootDir, path + extension);
        }

        if (!f.exists())
        {
            throw new PageNotFoundException("Document not found: " + path);
        }

        try
        {
            // unmarshal: use SAX I parser to read document XML, filtering
            // for page and folder menu definition menu elements ordered
            // polymorphic collection to insert artifical <menu-element>
            // tags enabling Castor XML binding; see JETSPEED-INF/castor/page-mapping.xml
            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser parser = factory.newSAXParser();
            XMLReader reader = parser.getXMLReader();
            final XMLReaderAdapter readerAdapter = new XMLReaderAdapter(reader);
            final InputSource readerInput = new InputSource(new InputStreamReader(new FileInputStream(f), PSML_DOCUMENT_ENCODING));
            Unmarshaller unmarshaller = new Unmarshaller(this.mapping);
            document = (Document) unmarshaller.unmarshal(new EventProducer()
                {
                    public void setDocumentHandler(final DocumentHandler handler)
                    {
                        readerAdapter.setDocumentHandler(new DocumentHandler()
                            {
                                private int menuDepth = 0;

                                public void characters(char[] ch, int start, int length) throws SAXException
                                {
                                    handler.characters(ch, start, length);
                                }

                                public void endDocument() throws SAXException
                                {
                                    handler.endDocument();
                                }

                                public void endElement(String name) throws SAXException
                                {
                                    // always include all elements
                                    handler.endElement(name);

                                    // track menu depth and insert menu-element nodes
                                    // to encapsulate menu elements to support collection
                                    // polymorphism in Castor
                                    if (name.equals("menu"))
                                    {
                                        menuDepth--;
                                        if (menuDepth > 0)
                                        {
                                            handler.endElement("menu-element");
                                        }
                                    }
                                    else if ((menuDepth > 0) &&
                                             (name.equals("options") || name.equals("separator") ||
                                              name.equals("include") || name.equals("exclude")))
                                    {
                                        handler.endElement("menu-element");
                                    }
                                }

                                public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException
                                {
                                    handler.ignorableWhitespace(ch, start, length);
                                }

                                public void processingInstruction(String target, String data) throws SAXException
                                {
                                    handler.processingInstruction(target, data);
                                }

                                public void setDocumentLocator(Locator locator)
                                {
                                    handler.setDocumentLocator(locator);
                                }

                                public void startDocument() throws SAXException
                                {
                                    handler.startDocument();
                                }

                                public void startElement(String name, AttributeList atts) throws SAXException
                                {
                                    // track menu depth and insert menu-element nodes
                                    // to encapsulate menu elements to support collection
                                    // polymorphism in Castor
                                    if (name.equals("menu"))
                                    {
                                        if (menuDepth > 0)
                                        {
                                            handler.startElement("menu-element", null);
                                        }
                                        menuDepth++;
                                    }
                                    else if ((menuDepth > 0) &&
                                             (name.equals("options") || name.equals("separator") ||
                                              name.equals("include") || name.equals("exclude")))
                                    {
                                        handler.startElement("menu-element", null);
                                    }

                                    // always include all elements
                                    handler.startElement(name, atts);
                                }
                            });
                    }
                    public void start() throws SAXException
                    {
                        try
                        {
                            readerAdapter.parse(readerInput);
                        }
                        catch (IOException ioe)
                        {
                            throw new SAXException(ioe);
                        }
                    }
                });

            document.setPath(path);
            AbstractBaseElement documentImpl = (AbstractBaseElement)document;
            documentImpl.setHandlerFactory(handlerFactory);
            documentImpl.setPermissionsEnabled(handlerFactory.getPermissionsEnabled());
            documentImpl.setConstraintsEnabled(handlerFactory.getConstraintsEnabled());
            documentImpl.unmarshalled();
        }
        catch (IOException e)
        {
            throw new PageNotFoundException("Could not load the file " + f.getAbsolutePath(), e);
        }
        catch (MarshalException e)
        {
            throw new PageNotFoundException("Could not unmarshal the file " + f.getAbsolutePath(), e);
        }
        catch (MappingException e)
        {
            throw new PageNotFoundException("Could not unmarshal the file " + f.getAbsolutePath(), e);
        }
        catch (ValidationException e)
        {
            throw new DocumentNotFoundException("Document " + f.getAbsolutePath() + " is not valid", e);
        }
        catch (SAXException e)
            {
            throw new PageNotFoundException("Could not unmarshal the file " + f.getAbsolutePath(), e);
            }
        catch (ParserConfigurationException e)
            {
            throw new PageNotFoundException("Could not unmarshal the file " + f.getAbsolutePath(), e);
            }
       

        if (document == null)
        {
            throw new DocumentNotFoundException("Document not found: " + path);
        }
        else
        {
            if (!clazz.isAssignableFrom(document.getClass()))
            {
                throw new ClassCastException(document.getClass().getName() + " must implement or extend "
                        + clazz.getName());
            }
            return document;
        }
    }
View Full Code Here


     * @return @throws
     *         DocumentNotFoundException
     */
    public Document getDocument( String name, boolean fromCache ) throws NodeException
    {
        Document document = null;
        if (fromCache)
        {
            Object obj = fileCache.getDocument(name);
            document = (Document) obj;
            if (document == null)
View Full Code Here

    {
        log.debug("Entry is refreshing: " + entry.getFile().getName());

        if (entry.getDocument() instanceof Document && ((Document) entry.getDocument()).getPath().endsWith(documentType))
        {
            Document document = (Document) entry.getDocument();
            Document freshDoc = getDocument(document.getPath(), false);
            Node parent = ((AbstractNode)document).getParent(false);
            freshDoc.setParent(parent);
            if(parent instanceof FolderImpl)
            {
                FolderImpl folder = (FolderImpl) parent;
                folder.getAllNodes().add(freshDoc);
            }
           
            freshDoc.setPath(document.getPath());
            entry.setDocument(freshDoc);           
        }

    }
View Full Code Here

                refresh(parentEntry);               
            }
        }
        else if(entry.getDocument() instanceof Document)
        {
            Document doc = (Document) entry.getDocument();
            if (doc.getType().equals(FolderMetaData.DOCUMENT_TYPE))
            {
                FileCacheEntry folderEntry = fileCache.get(((AbstractNode)doc).getParent().getPath());
                refresh(folderEntry);
            }
        }
View Full Code Here

                refresh(parentEntry);               
            }
        }
        else if (entry.getDocument() instanceof Document)
        {
            Document doc = (Document) entry.getDocument();
            if (doc.getType().equals(FolderMetaDataImpl.DOCUMENT_TYPE))
            {
                Node folderNode = ((AbstractNode)doc).getParent(false);
                if (folderNode != null)
                {               
                    FileCacheEntry folderEntry = fileCache.get(folderNode.getPath());
View Full Code Here

    }

    protected Object unmarshallDocument( Class clazz, String path, String extension ) throws DocumentNotFoundException,
            DocumentException
    {
        Document document = null;
        File f = null;
        if (path.endsWith(extension))
        {
            f = new File(this.documentRootDir, path);
        }
        else
        {
            f = new File(this.documentRootDir, path + extension);
        }

        if (!f.exists())
        {
            throw new PageNotFoundException("Document not found: " + path);
        }

        try
        {
            // unmarshal: use SAX II parser to read document XML, filtering
            // for page and folder menu definition menu elements ordered
            // polymorphic collection to insert artifical <menu-element>
            // tags enabling Castor XML binding; see JETSPEED-INF/castor/page-mapping.xml
           
            final InputSource readerInput = new InputSource(new InputStreamReader(new FileInputStream(f), PSML_DOCUMENT_ENCODING));
            Unmarshaller unmarshaller = new Unmarshaller();
            unmarshaller.setResolver((XMLClassDescriptorResolver) classDescriptorResolver);           
            unmarshaller.setValidation(false); // results in better performance
           
            synchronized (xmlReader)
            {
                document = (Document) unmarshaller.unmarshal(new SAX2EventProducer()
                    {
                        public void setContentHandler(final ContentHandler handler)
                        {
                          xmlReader.setContentHandler(new ContentHandler()
                                {
                                    private int menuDepth = 0;

                                    public void characters(char[] ch, int start, int length) throws SAXException
                                    {
                                        handler.characters(ch, start, length);
                                    }

                                    public void endDocument() throws SAXException
                                    {
                                        handler.endDocument();
                                    }

                                    public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException
                                    {
                                        handler.ignorableWhitespace(ch, start, length);
                                    }

                                    public void processingInstruction(String target, String data) throws SAXException
                                    {
                                        handler.processingInstruction(target, data);
                                    }

                                    public void setDocumentLocator(Locator locator)
                                    {
                                        handler.setDocumentLocator(locator);
                                    }

                                    public void startDocument() throws SAXException
                                    {
                                        handler.startDocument();
                                    }

                    public void endElement(String uri, String localName, String qName) throws SAXException {
                                        // always include all elements
                                        handler.endElement(uri,localName,qName);
                                        // track menu depth and insert menu-element nodes
                                        // to encapsulate menu elements to support collection
                                        // polymorphism in Castor
                                        if (qName.equals("menu"))
                                        {
                                            menuDepth--;
                                            if (menuDepth > 0)
                                            {
                                                handler.endElement(null,null,"menu-element");
                                            }
                                        }
                                        else if ((menuDepth > 0) &&
                                                 (qName.equals("options") || qName.equals("separator") ||
                                                     qName.equals("include") || qName.equals("exclude")))
                                        {
                                            handler.endElement(null,null,"menu-element");
                                        }
                    }

                    public void endPrefixMapping(String prefix) throws SAXException {
                    }

                    public void skippedEntity(String name) throws SAXException {
                      handler.skippedEntity(name);
                    }

                    public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
                                        // track menu depth and insert menu-element nodes
                                        // to encapsulate menu elements to support collection
                                        // polymorphism in Castor
                     
                      if (qName.equals("menu"))
                                        {
                                            if (menuDepth > 0)
                                            {
                                                handler.startElement(null,null,"menu-element", null);
                                            }
                                            menuDepth++;
                                        }
                                        else if ((menuDepth > 0) &&
                                                 (qName.equals("options") || qName.equals("separator") ||
                                                  qName.equals("include") || qName.equals("exclude")))
                                        {
                                            handler.startElement(null,null,"menu-element", null);
                                        }

                                        // always include all elements
                                        handler.startElement(null,null, qName, atts);
                    }

                    public void startPrefixMapping(String prefix, String uri) throws SAXException {
                    }
                                });
                        }
                        public void start() throws SAXException
                        {
                            try
                            {
                              xmlReader.parse(readerInput);
                            }
                            catch (IOException ioe)
                            {
                                throw new SAXException(ioe);
                            }
                        }
                    });
            }
           
            document.setPath(path);
            AbstractBaseElement documentImpl = (AbstractBaseElement)document;
            documentImpl.setHandlerFactory(handlerFactory);
            documentImpl.setPermissionsEnabled(handlerFactory.getPermissionsEnabled());
            documentImpl.setConstraintsEnabled(handlerFactory.getConstraintsEnabled());
            documentImpl.unmarshalled();
            if (document.isDirty()){
                updateDocument(document, true);
                document.setDirty(false);
            }
        }
        catch (IOException e)
        {
          log.error("Could not load the file " + f.getAbsolutePath(), e);
            throw new PageNotFoundException("Could not load the file " + f.getAbsolutePath(), e);
        }
        catch (MarshalException e)
        {
          log.error("Could not unmarshal the file " + f.getAbsolutePath(), e);
            throw new PageNotFoundException("Could not unmarshal the file " + f.getAbsolutePath(), e);
        }
        catch (ValidationException e)
        {
          log.error("Document " + f.getAbsolutePath() + " is not valid", e);
            throw new DocumentNotFoundException("Document " + f.getAbsolutePath() + " is not valid", e);
        }
       

        if (document == null)
        {
            throw new DocumentNotFoundException("Document not found: " + path);
        }
        else
        {
            if (!clazz.isAssignableFrom(document.getClass()))
            {
                throw new ClassCastException(document.getClass().getName() + " must implement or extend "
                        + clazz.getName());
            }
            return document;
        }
    }
View Full Code Here

     * @return @throws
     *         DocumentNotFoundException
     */
    public Document getDocument( String name, boolean fromCache ) throws DocumentNotFoundException, NodeException
    {
        Document document = null;
        if (fromCache)
        {
            Object obj = fileCache.getDocument(name);
            document = (Document) obj;
            if (document == null)
View Full Code Here

    {
        log.debug("Entry is refreshing: " + entry.getFile().getName());

        if (entry.getDocument() instanceof Document && ((Document) entry.getDocument()).getPath().endsWith(documentType))
        {
            Document document = (Document) entry.getDocument();
            Node parent = ((AbstractNode)document).getParent(false);
            if (parent instanceof FolderImpl)
            {
                Document freshDoc = getDocument(document.getPath(), false);
                freshDoc.setParent(parent);
                ((FolderImpl)parent).getAllNodes().add(freshDoc);
                freshDoc.setPath(document.getPath());
                entry.setDocument(freshDoc);
            }
        }
    }
View Full Code Here

        return new TestSuite(TestCastorFileSystemDocumentHandler.class);
    }
   
    public void testFolderMetaData() throws Exception
    {
        Document doc = folderMetaDataDocumentHandler.getDocument("/folder1/folder.metadata", false);
        assertNotNull(doc);
        String title = doc.getTitle();
        assertEquals("Default Title for Folder 1", title);
    }
View Full Code Here

                {
                    public void run()
                    {
                        try
                        {
                            Document doc = folderMetaDataDocumentHandler.getDocument("/folder1/folder.metadata", false);
                        }
                        catch (Exception e)
                        {
                            e.printStackTrace(System.out);
                            exceptions.add(e);
View Full Code Here

TOP

Related Classes of org.apache.jetspeed.om.page.Document

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.