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;
}
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);
}
}
}
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);
}
Aggregations