org.apache.poi.xwpf.converter.core.openxmlformats.RelashionShipsHandler Maven / Gradle / Ivy
package org.apache.poi.xwpf.converter.core.openxmlformats;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.xmlbeans.XmlException;
import org.openxmlformats.schemas.drawingml.x2006.main.ThemeDocument;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSettings;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTStyles;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.FontsDocument;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.FtrDocument;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.HdrDocument;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.SettingsDocument;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.StylesDocument;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.XMLReaderFactory;
public class RelashionShipsHandler
extends DefaultHandler
{
static class Relationship
{
public final String id;
public final String type;
public final String target;
public Relationship( String id, String type, String target )
{
this.id = id;
this.type = type;
this.target = target;
}
}
private final Map relationships;
private final AbstractOpenXMLFormatsPartProvider provider;
private CTStyles styles;
private final List themeDocuments;
private final List fontsDocuments;
private CTSettings settings;
private RelashionShipsHandler( AbstractOpenXMLFormatsPartProvider provider )
{
this.provider = provider;
this.relationships = new HashMap();
this.themeDocuments = new ArrayList();
this.fontsDocuments = new ArrayList();
}
public static RelashionShipsHandler create( String partName, AbstractOpenXMLFormatsPartProvider provider )
throws SAXException, IOException
{
XMLReader xmlReader = XMLReaderFactory.createXMLReader();
RelashionShipsHandler contentHandler = new RelashionShipsHandler( provider );
xmlReader.setContentHandler( contentHandler );
xmlReader.parse( new InputSource( provider.getEntryInputStream( "word/_rels/" + partName + ".rels" ) ) );
return contentHandler;
}
@Override
public final void startElement( String uri, String localName, String name, Attributes attributes )
throws SAXException
{
if ( "Relationship".equals( localName ) )
{
String id = attributes.getValue( "Id" );
String type = attributes.getValue( "Type" );
String target = attributes.getValue( "Target" );
relationships.put( id, new Relationship( id, type, target ) );
if ( "http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings".equals( type ) )
{
// settings
InputStream in = getInputStream( target );
try
{
settings = SettingsDocument.Factory.parse( in ).getSettings();
}
catch ( Exception e )
{
throw new SAXException( e );
}
}
else if ( "http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles".equals( type ) )
{
// style
InputStream in = getInputStream( target );
try
{
styles = StylesDocument.Factory.parse( in ).getStyles();
}
catch ( Exception e )
{
throw new SAXException( e );
}
}
else if ( "http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme".equals( type ) )
{
// theme
InputStream in = getInputStream( target );
try
{
ThemeDocument theme = ThemeDocument.Factory.parse( in );
themeDocuments.add( theme );
}
catch ( Exception e )
{
throw new SAXException( e );
}
}
else if ( "http://schemas.openxmlformats.org/officeDocument/2006/relationships/fontTable".equals( type ) )
{
// fontTable
InputStream in = getInputStream( target );
try
{
FontsDocument fonts = FontsDocument.Factory.parse( in );
fontsDocuments.add( fonts );
}
catch ( Exception e )
{
throw new SAXException( e );
}
}
}
}
private InputStream getInputStream( String target )
{
return provider.getEntryInputStream( "word/" + target );
}
public InputStream getInputStreamByRelId( String relId )
{
Relationship relation = getRelationship( relId );
return getInputStream( relation.target );
}
public List getThemeDocuments()
{
return themeDocuments;
}
public List getFontsDocument()
{
return fontsDocuments;
}
public CTSettings getSettings()
{
return settings;
}
public CTStyles getStyle()
{
return styles;
}
public FtrDocument getFtrDocument( String relId )
throws XmlException, IOException
{
Relationship relationship = getRelationship( relId );
InputStream in = provider.getEntryInputStream( "word/" + relationship.target );
FtrDocument ftrDoc = FtrDocument.Factory.parse( in );
return ftrDoc;
}
Relationship getRelationship( String relId )
{
return relationships.get( relId );
}
public HdrDocument getHdrDocument( String relId )
throws XmlException, IOException
{
Relationship relationship = getRelationship( relId );
InputStream in = provider.getEntryInputStream( "word/" + relationship.target );
HdrDocument hdrDoc = HdrDocument.Factory.parse( in );
return hdrDoc;
}
}