Search in sources :

Example 1 with XarException

use of org.xwiki.xar.XarException in project xwiki-platform by xwiki.

the class Packager method importDocumentToWiki.

private XarEntryMergeResult importDocumentToWiki(String comment, WikiReference wikiReference, InputStream inputStream, PackageConfiguration configuration) throws XWikiException, XarException, IOException {
    XWikiContext xcontext = this.xcontextProvider.get();
    XWikiDocument nextDocument;
    try {
        nextDocument = getXWikiDocument(inputStream, wikiReference);
    } catch (Exception e) {
        this.logger.error("Failed to parse document", e);
        return null;
    }
    DocumentReference reference = nextDocument.getDocumentReferenceWithLocale();
    XWikiDocument currentDocument = xcontext.getWiki().getDocument(reference, xcontext);
    currentDocument.loadAttachmentsContentSafe(xcontext);
    XWikiDocument previousDocument;
    XarExtensionPlan xarExtensionPlan = configuration.getXarExtensionPlan();
    if (xarExtensionPlan != null) {
        previousDocument = xarExtensionPlan.getPreviousXWikiDocument(reference, this);
    } else {
        previousDocument = null;
    }
    if (configuration.isVerbose()) {
        this.logger.info(LOG_INSTALLDOCUMENT_BEGIN, "Installing document [{}]", nextDocument.getDocumentReferenceWithLocale());
    }
    try {
        XarEntryMergeResult entityMergeResult = this.importer.saveDocument(comment, previousDocument, currentDocument, nextDocument, configuration);
        if (configuration.isVerbose()) {
            this.logger.info(LOG_INSTALLDOCUMENT_SUCCESS_END, "Done installing document [{}]", nextDocument.getDocumentReferenceWithLocale());
        }
        return entityMergeResult;
    } catch (Exception e) {
        if (configuration.isVerbose()) {
            this.logger.error(LOG_INSTALLDOCUMENT_FAILURE_END, "Failed to install document [{}]", nextDocument.getDocumentReferenceWithLocale(), e);
        }
    }
    return null;
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XarExtensionPlan(org.xwiki.extension.xar.internal.handler.XarExtensionPlan) XWikiContext(com.xpn.xwiki.XWikiContext) XWikiException(com.xpn.xwiki.XWikiException) WikiManagerException(org.xwiki.wiki.manager.WikiManagerException) FilterException(org.xwiki.filter.FilterException) XarException(org.xwiki.xar.XarException) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) IOException(java.io.IOException) LocalDocumentReference(org.xwiki.model.reference.LocalDocumentReference) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 2 with XarException

use of org.xwiki.xar.XarException in project xwiki-platform by xwiki.

the class DiffXarJob method diff.

private void diff(InstalledExtension installedExtension, String namespace, Set<LocalDocumentReference> alreadydone) {
    Collection<ExtensionId> excludedExtensions = getRequest().getExcludedExtensions();
    if (XarExtensionHandler.TYPE.equals(installedExtension.getType()) && (excludedExtensions == null || !excludedExtensions.contains(installedExtension.getId()))) {
        if (getRequest().isVerbose()) {
            this.logger.info("Computing differences for [{}] on namespace [{}]", installedExtension.getId(), namespace);
        }
        try {
            WikiReference wikiReference = new WikiReference(XarHandlerUtils.getWikiFromNamespace(namespace));
            diff(new XarFile(new File(installedExtension.getFile().getAbsolutePath())), wikiReference, installedExtension.getId(), alreadydone);
        } catch (UnsupportedNamespaceException e) {
            this.logger.error("Failed to extract the wiki id from the namespace [{}].", namespace, e);
        } catch (IOException e) {
            this.logger.error("Failed to read the XAR file of the extension [{}].", installedExtension.getId(), e);
        } catch (XarException e) {
            this.logger.error("Failed to parse the XAR file of the extension [{}].", installedExtension.getId(), e);
        }
    }
}
Also used : UnsupportedNamespaceException(org.xwiki.extension.xar.internal.handler.UnsupportedNamespaceException) XarFile(org.xwiki.xar.XarFile) XarException(org.xwiki.xar.XarException) ExtensionId(org.xwiki.extension.ExtensionId) IOException(java.io.IOException) WikiReference(org.xwiki.model.reference.WikiReference) XarFile(org.xwiki.xar.XarFile) File(java.io.File)

Example 3 with XarException

use of org.xwiki.xar.XarException in project xwiki-platform by xwiki.

the class XarUtils method getReference.

/**
 * Extract {@link LocalDocumentReference} from a XAR document XML stream.
 *
 * @param documentStream the stream to parse
 * @return the reference extracted from the stream
 * @throws XarException when failing to parse the document stream
 * @since 5.4M1
 */
public static LocalDocumentReference getReference(InputStream documentStream) throws XarException {
    XMLStreamReader xmlReader;
    try {
        xmlReader = XML_INPUT_FACTORY.createXMLStreamReader(documentStream);
    } catch (XMLStreamException e) {
        throw new XarException("Failed to create a XML read", e);
    }
    EntityReference reference = null;
    Locale locale = null;
    String legacySpace = null;
    String legacyPage = null;
    try {
        // <xwikidoc>
        xmlReader.nextTag();
        xmlReader.require(XMLStreamReader.START_ELEMENT, null, XarDocumentModel.ELEMENT_DOCUMENT);
        // Reference
        String referenceString = xmlReader.getAttributeValue(null, XarDocumentModel.ATTRIBUTE_DOCUMENT_REFERENCE);
        if (referenceString != null) {
            reference = RESOLVER.resolve(referenceString, EntityType.DOCUMENT);
        }
        // Locale
        String localeString = xmlReader.getAttributeValue(null, XarDocumentModel.ATTRIBUTE_DOCUMENT_LOCALE);
        if (localeString != null) {
            if (localeString.isEmpty()) {
                locale = Locale.ROOT;
            } else {
                locale = LocaleUtils.toLocale(localeString);
            }
        }
        // Legacy fallback
        if (reference == null || locale == null) {
            for (xmlReader.nextTag(); xmlReader.isStartElement(); xmlReader.nextTag()) {
                String elementName = xmlReader.getLocalName();
                if (XarDocumentModel.ELEMENT_NAME.equals(elementName)) {
                    if (reference == null) {
                        legacyPage = xmlReader.getElementText();
                        if (legacySpace != null && locale != null) {
                            break;
                        }
                    } else if (locale != null) {
                        break;
                    }
                } else if (XarDocumentModel.ELEMENT_SPACE.equals(elementName)) {
                    if (reference == null) {
                        legacySpace = xmlReader.getElementText();
                        if (legacyPage != null && locale != null) {
                            break;
                        }
                    } else if (locale != null) {
                        break;
                    }
                } else if (XarDocumentModel.ELEMENT_LOCALE.equals(elementName)) {
                    if (locale == null) {
                        String value = xmlReader.getElementText();
                        if (value.length() == 0) {
                            locale = Locale.ROOT;
                        } else {
                            locale = LocaleUtils.toLocale(value);
                        }
                    }
                    if (reference != null || (legacySpace != null && legacyPage != null)) {
                        break;
                    }
                } else {
                    StAXUtils.skipElement(xmlReader);
                }
            }
        }
    } catch (XMLStreamException e) {
        throw new XarException("Failed to parse document", e);
    } finally {
        try {
            xmlReader.close();
        } catch (XMLStreamException e) {
            throw new XarException("Failed to close XML reader", e);
        }
    }
    if (reference == null) {
        if (legacySpace == null) {
            throw new XarException("Missing space element");
        }
        if (legacyPage == null) {
            throw new XarException("Missing page element");
        }
        reference = new LocalDocumentReference(legacySpace, legacyPage);
    }
    if (locale == null) {
        throw new XarException("Missing locale element");
    }
    return new LocalDocumentReference(reference, locale);
}
Also used : Locale(java.util.Locale) LocalDocumentReference(org.xwiki.model.reference.LocalDocumentReference) XMLStreamReader(javax.xml.stream.XMLStreamReader) XMLStreamException(javax.xml.stream.XMLStreamException) XarException(org.xwiki.xar.XarException) EntityReference(org.xwiki.model.reference.EntityReference)

Aggregations

XarException (org.xwiki.xar.XarException)3 IOException (java.io.IOException)2 LocalDocumentReference (org.xwiki.model.reference.LocalDocumentReference)2 XWikiContext (com.xpn.xwiki.XWikiContext)1 XWikiException (com.xpn.xwiki.XWikiException)1 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)1 File (java.io.File)1 Locale (java.util.Locale)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 XMLStreamReader (javax.xml.stream.XMLStreamReader)1 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)1 ExtensionId (org.xwiki.extension.ExtensionId)1 UnsupportedNamespaceException (org.xwiki.extension.xar.internal.handler.UnsupportedNamespaceException)1 XarExtensionPlan (org.xwiki.extension.xar.internal.handler.XarExtensionPlan)1 FilterException (org.xwiki.filter.FilterException)1 DocumentReference (org.xwiki.model.reference.DocumentReference)1 EntityReference (org.xwiki.model.reference.EntityReference)1 WikiReference (org.xwiki.model.reference.WikiReference)1 WikiManagerException (org.xwiki.wiki.manager.WikiManagerException)1 XarFile (org.xwiki.xar.XarFile)1