CommonsParser { public static ArrayList<TextFragment> parseTextFragments( List<? extends Node> elementsAndText) throws ConstraintViolationException { ArrayList<TextFragment> ret = new ArrayList<TextFragment>(); NodeIterator iter = new NodeIterator(elementsAndText, false); while (iter.hasNext()) { if (iter.nextIsElement()) { Element element = iter .getXliffElement(G.NAME, X.NAME, Bx.NAME, Ex.NAME, Bpt.NAME, Ept.NAME, Ph.NAME, It.NAME, Mrk.NAME); String name = element.getLocalName(); if (G.NAME.equals(name)) { ret.add(new GImpl(element)); } else if (X.NAME.equals(name)) { ret.add(new XImpl(element)); } else if (Bx.NAME.equals(name)) { ret.add(new BxImpl(element)); } else if (Ex.NAME.equals(name)) { ret.add(new ExImpl(element)); } else if (Bpt.NAME.equals(name)) { ret.add(new BptImpl(element)); } else if (Ept.NAME.equals(name)) { ret.add(new EptImpl(element)); } else if (Ph.NAME.equals(name)) { ret.add(new PhImpl(element)); } else if (It.NAME.equals(name)) { ret.add(new ItImpl(element)); } else if (Mrk.NAME.equals(name)) { ret.add(new MrkImpl(element)); } } else { ret.add(new TextImpl(iter.getText())); } } return ret; } private  CommonsParser(); static ArrayList<TextFragment> parseTextFragments(
			List<? extends Node> elementsAndText); static ArrayList<CodeFragment> parseCodeFragments(
			List<? extends Node> elementsAndText);  }
@Test public void testParseTextFragments() { Document doc = Utils.createDocument(); ArrayList<Node> list = new ArrayList<Node>(); Element g = doc.createElementNS(XLIFF_12_NAMESPACE, "g"); g.setAttributeNS(null, "id", "1"); g.setTextContent("text1"); list.add(g); Element bpt = doc.createElementNS(XLIFF_12_NAMESPACE, "bpt"); bpt.setAttributeNS(null, "id", "2"); bpt.setTextContent("text2"); list.add(bpt); list.add(doc.createTextNode("text3")); Element ept = doc.createElementNS(XLIFF_12_NAMESPACE, "ept"); ept.setAttributeNS(null, "id", "2"); ept.setTextContent("text4"); list.add(ept); try { ArrayList<TextFragment> fragments = CommonsParser.parseTextFragments(list); assertEquals(new Integer(4), new Integer(fragments.size())); assertTrue(fragments.get(0) instanceof G); assertTrue(fragments.get(1) instanceof Bpt); assertTrue(fragments.get(2) instanceof Text); assertTrue(fragments.get(3) instanceof Ept); } catch (ConstraintViolationException e) { fail("Did not expect ConstraintViolationException"); } list.clear(); Element x = doc.createElementNS(XLIFF_12_NAMESPACE, "x"); x.setAttributeNS(null, "id", "1"); list.add(x); Element bx = doc.createElementNS(XLIFF_12_NAMESPACE, "bx"); bx.setAttributeNS(null, "id", "2"); list.add(bx); Element ex = doc.createElementNS(XLIFF_12_NAMESPACE, "ex"); ex.setAttributeNS(null, "id", "2"); list.add(ex); try { ArrayList<TextFragment> fragments = CommonsParser.parseTextFragments(list); assertEquals(new Integer(3), new Integer(fragments.size())); assertTrue(fragments.get(0) instanceof X); assertTrue(fragments.get(1) instanceof Bx); assertTrue(fragments.get(2) instanceof Ex); } catch (ConstraintViolationException e) { fail("Did not expect ConstraintViolationException"); } list.clear(); Element ph = doc.createElementNS(XLIFF_12_NAMESPACE, "ph"); ph.setAttributeNS(null, "id", "1"); ph.setTextContent("text1"); list.add(ph); Element it = doc.createElementNS(XLIFF_12_NAMESPACE, "it"); it.setAttributeNS(null, "id", "2"); it.setAttributeNS(null, "pos", "open"); list.add(it); Element mrk = doc.createElementNS(XLIFF_12_NAMESPACE, "mrk"); mrk.setAttributeNS(XLIFF_12_NAMESPACE, "mtype", "name"); mrk.setTextContent("text2"); list.add(mrk); try { ArrayList<TextFragment> fragments = CommonsParser.parseTextFragments(list); assertEquals(new Integer(3), new Integer(fragments.size())); assertTrue(fragments.get(0) instanceof Ph); assertTrue(fragments.get(1) instanceof It); assertTrue(fragments.get(2) instanceof Mrk); } catch (ConstraintViolationException e) { fail("Did not expect ConstraintViolationException"); } list.clear(); Element invalid = doc.createElementNS(XLIFF_12_NAMESPACE, "invalid"); list.add(invalid); try { CommonsParser.parseTextFragments(list); fail("Expected ConstraintViolationException"); } catch (ConstraintViolationException e) { } Element invalidParent = doc.createElementNS(XLIFF_12_NAMESPACE, "parent"); invalidParent.appendChild(invalid); try { CommonsParser.parseTextFragments(list); fail("Expected ConstraintViolationException"); } catch (ConstraintViolationException e) { } }