use of javax.jcr.InvalidSerializedDataException in project jackrabbit-oak by apache.
the class SysViewImportHandler method endElement.
@Override
public void endElement(String namespaceURI, String localName, String qName) throws SAXException {
// check element name
ImportState state = stack.peek();
if (namespaceURI.equals(NamespaceConstants.NAMESPACE_SV) && "node".equals(localName)) {
// sv:node element
if (!state.started) {
// need to start & end current node
processNode(state, true, true);
state.started = true;
} else {
// need to end current node
processNode(state, false, true);
}
// pop current state from stack
stack.pop();
} else if (namespaceURI.equals(NamespaceConstants.NAMESPACE_SV) && "property".equals(localName)) {
// have been collected and create node as necessary primaryType
if (isSystemProperty("primaryType")) {
BufferedStringValue val = currentPropValues.get(0);
String s = null;
try {
s = val.retrieve();
state.nodeTypeName = new NameInfo(s).getRepoQualifiedName();
} catch (IOException e) {
throw new SAXException(new InvalidSerializedDataException("illegal node type name: " + s, e));
} catch (RepositoryException e) {
throw new SAXException(new InvalidSerializedDataException("illegal node type name: " + s, e));
}
} else if (isSystemProperty("mixinTypes")) {
if (state.mixinNames == null) {
state.mixinNames = new ArrayList<String>(currentPropValues.size());
}
for (BufferedStringValue val : currentPropValues) {
String s = null;
try {
s = val.retrieve();
state.mixinNames.add(new NameInfo(s).getRepoQualifiedName());
} catch (IOException ioe) {
throw new SAXException("error while retrieving value", ioe);
} catch (RepositoryException e) {
throw new SAXException(new InvalidSerializedDataException("illegal mixin type name: " + s, e));
}
}
} else if (isSystemProperty("uuid")) {
BufferedStringValue val = currentPropValues.get(0);
try {
state.uuid = val.retrieve();
} catch (IOException ioe) {
throw new SAXException("error while retrieving value", ioe);
}
} else {
if (currentPropMultipleStatus == PropInfo.MultipleStatus.UNKNOWN && currentPropValues.size() != 1) {
currentPropMultipleStatus = PropInfo.MultipleStatus.MULTIPLE;
}
PropInfo prop = new PropInfo(currentPropName == null ? null : currentPropName.getRepoQualifiedName(), currentPropType, currentPropValues, currentPropMultipleStatus);
state.props.add(prop);
}
// reset temp fields
currentPropValues.clear();
} else if (namespaceURI.equals(NamespaceConstants.NAMESPACE_SV) && "value".equals(localName)) {
// sv:value element
currentPropValues.add(currentPropValue);
// reset temp fields
currentPropValue = null;
} else {
throw new SAXException(new InvalidSerializedDataException("invalid element in system view xml document: " + localName));
}
}
use of javax.jcr.InvalidSerializedDataException in project sling by apache.
the class JcrXmlImporter method importJcrXml.
/**
* Import the XML file as JCR system or document view import. If the XML
* file is not a valid system or document view export/import file,
* <code>false</code> is returned.
*
* @param parent The parent node below which to import
* @param name the name of the import resource
* @param contentStream The XML content to import
* @param replace Whether or not to replace the subtree at name if the
* node exists.
* @return <code>true</code> if the import succeeds, <code>false</code> if the import fails due to XML format errors.
* @throws IOException If an IO error occurrs reading the XML file.
*/
protected Node importJcrXml(Node parent, String name, InputStream contentStream, boolean replace) throws IOException {
try {
final String nodeName = (name.endsWith(EXT_JCR_XML)) ? name.substring(0, name.length() - EXT_JCR_XML.length()) : name;
// ensure the name is not empty
if (nodeName.length() == 0) {
throw new IOException("Node name must not be empty (or extension only)");
}
// check for existence/replacement
if (parent.hasNode(nodeName)) {
Node existingNode = parent.getNode(nodeName);
if (replace) {
logger.debug("importJcrXml: Removing existing node at {}", nodeName);
existingNode.remove();
} else {
logger.debug("importJcrXml: Node {} for XML already exists, nothing to to", nodeName);
return existingNode;
}
}
final int uuidBehavior;
if (replace) {
uuidBehavior = IMPORT_UUID_COLLISION_REPLACE_EXISTING;
} else {
uuidBehavior = IMPORT_UUID_CREATE_NEW;
}
Session session = parent.getSession();
session.importXML(parent.getPath(), contentStream, uuidBehavior);
// additionally check whether the expected child node exists
return (parent.hasNode(nodeName)) ? parent.getNode(nodeName) : null;
} catch (InvalidSerializedDataException isde) {
// the xml might not be System or Document View export, fall back to old-style XML reading
logger.info("importJcrXml: XML does not seem to be system or document view; cause: {}", isde.toString());
return null;
} catch (RepositoryException re) {
// any other repository related issue...
logger.info("importJcrXml: Repository issue loading XML; cause: {}", re.toString());
return null;
}
}
use of javax.jcr.InvalidSerializedDataException in project jackrabbit by apache.
the class SysViewImportHandler method endElement.
/**
* {@inheritDoc}
*/
@Override
public void endElement(String namespaceURI, String localName, String qName) throws SAXException {
Name name = NameFactoryImpl.getInstance().create(namespaceURI, localName);
// check element name
ImportState state = stack.peek();
if (name.equals(NameConstants.SV_NODE)) {
// sv:node element
if (!state.started) {
// need to start & end current node
processNode(state, true, true);
state.started = true;
} else {
// need to end current node
processNode(state, false, true);
}
// pop current state from stack
stack.pop();
} else if (name.equals(NameConstants.SV_PROPERTY)) {
// have been collected and create node as necessary
if (currentPropName.equals(NameConstants.JCR_PRIMARYTYPE)) {
BufferedStringValue val = currentPropValues.get(0);
String s = null;
try {
s = val.retrieve();
state.nodeTypeName = resolver.getQName(s);
} catch (IOException ioe) {
throw new SAXException("error while retrieving value", ioe);
} catch (NameException e) {
throw new SAXException(new InvalidSerializedDataException("illegal node type name: " + s, e));
} catch (NamespaceException e) {
throw new SAXException(new InvalidSerializedDataException("illegal node type name: " + s, e));
}
} else if (currentPropName.equals(NameConstants.JCR_MIXINTYPES)) {
if (state.mixinNames == null) {
state.mixinNames = new ArrayList<Name>(currentPropValues.size());
}
for (BufferedStringValue val : currentPropValues) {
String s = null;
try {
s = val.retrieve();
Name mixin = resolver.getQName(s);
state.mixinNames.add(mixin);
} catch (IOException ioe) {
throw new SAXException("error while retrieving value", ioe);
} catch (NameException e) {
throw new SAXException(new InvalidSerializedDataException("illegal mixin type name: " + s, e));
} catch (NamespaceException e) {
throw new SAXException(new InvalidSerializedDataException("illegal mixin type name: " + s, e));
}
}
} else if (currentPropName.equals(NameConstants.JCR_UUID)) {
BufferedStringValue val = currentPropValues.get(0);
try {
state.uuid = val.retrieve();
} catch (IOException ioe) {
throw new SAXException("error while retrieving value", ioe);
}
} else {
if (currentPropMultipleStatus == MultipleStatus.UNKNOWN && currentPropValues.size() != 1) {
currentPropMultipleStatus = MultipleStatus.MULTIPLE;
}
PropInfo prop = new PropInfo(currentPropName, currentPropType, currentPropValues.toArray(new TextValue[currentPropValues.size()]), currentPropMultipleStatus);
state.props.add(prop);
}
// reset temp fields
currentPropValues.clear();
} else if (name.equals(NameConstants.SV_VALUE)) {
// sv:value element
currentPropValues.add(currentPropValue);
// reset temp fields
currentPropValue = null;
} else {
throw new SAXException(new InvalidSerializedDataException("invalid element in system view xml document: " + localName));
}
}
use of javax.jcr.InvalidSerializedDataException in project jackrabbit by apache.
the class AbstractSession method importXML.
/**
* Parses the given input stream as an XML document and processes the
* SAX events using the {@link ContentHandler} returned by
* {@link Session#getImportContentHandler(String, int)}.
*
* @param parentAbsPath passed through
* @param in input stream to be parsed as XML and imported
* @param uuidBehavior passed through
* @throws IOException if an I/O error occurs
* @throws InvalidSerializedDataException if an XML parsing error occurs
* @throws RepositoryException if a repository error occurs
*/
public void importXML(String parentAbsPath, InputStream in, int uuidBehavior) throws IOException, InvalidSerializedDataException, RepositoryException {
try {
ContentHandler handler = getImportContentHandler(parentAbsPath, uuidBehavior);
new ParsingContentHandler(handler).parse(in);
} catch (SAXException e) {
Throwable exception = e.getException();
if (exception instanceof RepositoryException) {
throw (RepositoryException) exception;
} else if (exception instanceof IOException) {
throw (IOException) exception;
} else {
throw new InvalidSerializedDataException("XML parse error", e);
}
} finally {
// JCR-2903
if (in != null) {
try {
in.close();
} catch (IOException ignore) {
}
}
}
}
use of javax.jcr.InvalidSerializedDataException in project jackrabbit by apache.
the class SessionImpl method importXML.
/**
* @see javax.jcr.Session#importXML(String, java.io.InputStream, int)
*/
@Override
public void importXML(String parentAbsPath, InputStream in, int uuidBehavior) throws IOException, PathNotFoundException, ItemExistsException, ConstraintViolationException, VersionException, InvalidSerializedDataException, LockException, RepositoryException {
// NOTE: checks are performed by 'getImportContentHandler'
ImportHandler handler = (ImportHandler) getImportContentHandler(parentAbsPath, uuidBehavior);
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setNamespaceAware(true);
factory.setFeature("http://xml.org/sax/features/namespace-prefixes", false);
SAXParser parser = factory.newSAXParser();
parser.parse(new InputSource(in), handler);
} catch (SAXException se) {
// check for wrapped repository exception
Exception e = se.getException();
if (e != null && e instanceof RepositoryException) {
throw (RepositoryException) e;
} else {
String msg = "failed to parse XML stream";
log.debug(msg);
throw new InvalidSerializedDataException(msg, se);
}
} catch (ParserConfigurationException e) {
throw new RepositoryException("SAX parser configuration error", e);
} finally {
// JCR-2903
in.close();
}
}
Aggregations