Begin on test for going from xwpf text to style

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1678179 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2015-05-07 12:37:43 +00:00
parent 9c558ab293
commit 9a755850e9
2 changed files with 55 additions and 4 deletions

View File

@ -43,8 +43,10 @@ import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTLanguage;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFonts; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFonts;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDocDefaults; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDocDefaults;
/** /**
* @author Philipp Epp * Holds details of built-in, default and user styles, which
* * apply to tables / paragraphs / lists etc.
* Text within one of those with custom stylings has the style
* information stored in the {@link XWPFRun}
*/ */
public class XWPFStyles extends POIXMLDocumentPart{ public class XWPFStyles extends POIXMLDocumentPart{
@ -83,8 +85,6 @@ public class XWPFStyles extends POIXMLDocumentPart{
} catch (XmlException e) { } catch (XmlException e) {
throw new POIXMLException("Unable to read styles", e); throw new POIXMLException("Unable to read styles", e);
} }
} }
@Override @Override
@ -154,6 +154,9 @@ public class XWPFStyles extends POIXMLDocumentPart{
} }
return null; return null;
} }
public int getNumberOfStyles() {
return listStyle.size();
}
/** /**
* get the styles which are related to the parameter style and their relatives * get the styles which are related to the parameter style and their relatives

View File

@ -138,4 +138,52 @@ public class TestXWPFStyles extends TestCase {
styles = docIn.getStyles(); styles = docIn.getStyles();
assertTrue(styles.styleExist(strStyleId)); assertTrue(styles.styleExist(strStyleId));
} }
public void testEasyAccessToStyles() throws IOException {
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("SampleDoc.docx");
XWPFStyles styles = doc.getStyles();
assertNotNull(styles);
// Has 3 paragraphs on page one, a break, and 3 on page 2
assertEquals(7, doc.getParagraphs().size());
// Check the first three have no run styles, just default paragraph style
for (int i=0; i<3; i++) {
XWPFParagraph p = doc.getParagraphs().get(i);
assertEquals(null, p.getStyle());
assertEquals(null, p.getStyleID());
assertEquals(1, p.getRuns().size());
XWPFRun r = p.getRuns().get(0);
assertEquals(null, r.getColor());
assertEquals(null, r.getFontFamily());
assertEquals(null, r.getFontName());
assertEquals(-1, r.getFontSize());
}
// On page two, has explicit styles, but on runs not on
// the paragraph itself
for (int i=4; i<7; i++) {
XWPFParagraph p = doc.getParagraphs().get(i);
assertEquals(null, p.getStyle());
assertEquals(null, p.getStyleID());
assertEquals(1, p.getRuns().size());
XWPFRun r = p.getRuns().get(0);
assertEquals("Arial Black", r.getFontFamily());
assertEquals("Arial Black", r.getFontName());
assertEquals(16, r.getFontSize());
assertEquals("548DD4", r.getColor());
}
// Check the document styles
// Should have a style defined for each type
assertEquals(4, styles.getNumberOfStyles());
assertNotNull(styles.getStyle("Normal"));
assertNotNull(styles.getStyle("DefaultParagraphFont"));
assertNotNull(styles.getStyle("TableNormal"));
assertNotNull(styles.getStyle("NoList"));
// TODO Check latent and default
}
} }