String defaultSegment = null;
if (contentProxy != null) {
Object contentModel = contentProxy.getModelObject();
if (contentModel != null &&
contentModel instanceof InternalLayoutContentBuilder) {
Layout layout = ((InternalLayoutContentBuilder) contentModel).
getLayout();
if (layout != null) {
defaultFragment = layout.getDefaultFragmentName();
defaultSegment = layout.getDefaultSegmentName();
}
}
}
XMLOutputter outputter = new XMLOutputter();
String docString = outputter.outputString(document);
byte[] bytes = null;
try {
bytes = docString.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
EclipseCommonPlugin.handleError(ABPlugin.getDefault(), e);
}
final ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
final JiBXReader reader;
try {
reader = new JiBXReader(Class.forName(
"com.volantis.mcs.policies.impl.variants.layout.LayoutContentBuilderImpl"));
} catch (ClassNotFoundException e) {
throw new ExtendedIOException(e);
}
final BinaryContentInput content = new BinaryContentInput(inputStream);
ContentBuilder contentBuilder = null;
try {
contentBuilder =
(ContentBuilder) reader.read(content, file.getName());
} catch (ArrayIndexOutOfBoundsException e) {
// HACK : This can happen if we are in the process of removing
// a grid column. First the grid column count is updated then
// the columns are removed. But a change event and layout
// validation is triggered when the column count is altered. As
// the model is in an invalid state it fails to read in
// correctly.
throw new IOException("Failed to read layout due to ArrayIndexOutOfBoundsException");
}
// If we have a valid content builder with layout (which should always
// be the case) re-insert the data that was edited from the overview
// page.
if (contentBuilder instanceof InternalLayoutContentBuilder) {
Layout layout = ((InternalLayoutContentBuilder) contentBuilder).getLayout();
if (layout != null) {
if (defaultFragment != null) {
layout.setDefaultFragmentName(defaultFragment);
}
if (defaultSegment != null) {
layout.setDefaultSegmentName(defaultSegment);
}
}
}
return contentBuilder;