CssNestedAtRuleFactory { public static CssNestedAtRule createNestedRule(String ruleDeclaration) { ruleDeclaration = ruleDeclaration.trim(); String ruleName = extractRuleNameFromDeclaration(ruleDeclaration); String ruleParameters = ruleDeclaration.substring(ruleName.length()).trim(); switch (ruleName) { case CssRuleName.MEDIA: return new CssMediaRule(ruleParameters); case CssRuleName.PAGE: return new CssPageRule(ruleParameters); case CssRuleName.TOP_LEFT_CORNER: case CssRuleName.TOP_LEFT: case CssRuleName.TOP_CENTER: case CssRuleName.TOP_RIGHT: case CssRuleName.TOP_RIGHT_CORNER: case CssRuleName.LEFT_TOP: case CssRuleName.LEFT_MIDDLE: case CssRuleName.LEFT_BOTTOM: case CssRuleName.RIGHT_TOP: case CssRuleName.RIGHT_MIDDLE: case CssRuleName.RIGHT_BOTTOM: case CssRuleName.BOTTOM_LEFT_CORNER: case CssRuleName.BOTTOM_LEFT: case CssRuleName.BOTTOM_CENTER: case CssRuleName.BOTTOM_RIGHT: case CssRuleName.BOTTOM_RIGHT_CORNER: return new CssMarginRule(ruleName); case CssRuleName.FONT_FACE: return new CssFontFaceRule(); default: return new CssNestedAtRule(ruleName, ruleParameters); } } private  CssNestedAtRuleFactory(); static CssNestedAtRule createNestedRule(String ruleDeclaration);  }
@Test public void testCreatingNestedRule() { CssNestedAtRule pageRule = CssNestedAtRuleFactory.createNestedRule("page:first"); Assert.assertTrue(pageRule instanceof CssPageRule); Assert.assertEquals(CssRuleName.PAGE, pageRule.getRuleName()); Assert.assertEquals(":first", pageRule.getRuleParameters()); CssNestedAtRule rightBottomMarginRule = CssNestedAtRuleFactory.createNestedRule("bottom-right"); Assert.assertTrue(rightBottomMarginRule instanceof CssMarginRule); Assert.assertEquals(CssRuleName.BOTTOM_RIGHT, rightBottomMarginRule.getRuleName()); CssNestedAtRule fontFaceRule = CssNestedAtRuleFactory.createNestedRule("font-face"); Assert.assertTrue(fontFaceRule instanceof CssFontFaceRule); Assert.assertEquals(CssRuleName.FONT_FACE, fontFaceRule.getRuleName()); }