Листинг 8.7. Метод doShortNews (NewsFormatter.java)
public void doShortNews(PrintWriter out){ NamedNodeMap attrib = docRoot.getAttributes(); Node n = attrib.getNamedItem( "shorttemplate") ; String template = null ; if( n != null ) template = n.getNodeValue(); if( headStr == null && template != null && template.length() > 2 ){ try { setFromTemplate( template ); System.out.println("Template set ok " + headStr + footStr ); }catch(IOException ie ){ System.out.println("Unable to read " + template ); } } out.println( headStr ); for( int i = skipNitems ; i < itemNodes.length ; i++ ){ if( i > = maxNitems ) break ; n = itemNodes[i]; // <Newsitem nodes String id = ((Element)n).getAttribute("id"); findNodes((Element) n ); // locates the parts of <Newsitem doNewsItemShort( out, id ); } out.println( footStr ); }
Как показано в листинге 8.8, метод doLongNews проверяет наличие заданного по умолчанию шаблона форматирования полной версии сообщения, после чего выполняет цикл по всем сообщениям в массиве itemNodes.