MetadataRecordFactory { public static MetadataRecord create( XMLStreamReader xmlStream ) { if ( !xmlStream.isStartElement() ) { throw new XMLParsingException( xmlStream, "XMLStreamReader does not point to a START_ELEMENT." ); } String ns = xmlStream.getNamespaceURI(); ByteArrayOutputStream out = new ByteArrayOutputStream(); XMLStreamWriter writer = null; XMLStreamReader recordAsXmlStream; InputStream in = null; try { XMLOutputFactory factory = XMLOutputFactory.newInstance(); factory.setProperty( XMLOutputFactory.IS_REPAIRING_NAMESPACES, Boolean.TRUE ); writer = factory.createXMLStreamWriter( out ); writer.writeStartDocument(); XMLAdapter.writeElement( writer, xmlStream ); writer.writeEndDocument(); writer.close(); in = new ByteArrayInputStream( out.toByteArray() ); recordAsXmlStream = XMLInputFactory.newInstance().createXMLStreamReader( in ); } catch ( XMLStreamException e ) { throw new XMLParsingException( xmlStream, e.getMessage() ); } catch ( FactoryConfigurationError e ) { throw new XMLParsingException( xmlStream, e.getMessage() ); } finally { IOUtils.closeQuietly( in ); IOUtils.closeQuietly( out ); } if ( ISO_RECORD_NS.equals( ns ) ) { return new ISORecord( recordAsXmlStream ); } if ( RIM_NS.equals( ns ) ) { throw new UnsupportedOperationException( "Creating ebRIM records from XMLStreamReader is not implemented yet." ); } if ( DC_RECORD_NS.equals( ns ) ) { throw new UnsupportedOperationException( "Creating DC records from XMLStreamReader is not implemented yet." ); } throw new IllegalArgumentException( "Unknown / unsuppported metadata namespace '" + ns + "'." ); }  static MetadataRecord create( XMLStreamReader xmlStream ); static MetadataRecord create( OMElement rootEl ); static MetadataRecord create( File file );  }
@Test public void testCreate() throws Exception { XMLStreamReader xmlStream = createStream( "metadataRecord.xml" ); MetadataRecord record = MetadataRecordFactory.create( xmlStream ); assertThat( record, instanceOf( ISORecord.class ) ); assertThat( record.getIdentifier(), is( "655e5998-a20e-66b5-c888-00005553421" ) ); }
@Test public void testCreate_DuplicatedNamespace() throws Exception { XMLStreamReader xmlStream = createStream( "metadataRecord_namespaceDuplicated.xml" ); MetadataRecord record = MetadataRecordFactory.create( xmlStream ); assertThat( record, instanceOf( ISORecord.class ) ); assertThat( record.getIdentifier(), is( "655e5998-a20e-66b5-c888-00005553499" ) ); }