Разработка контента курса дистанционного обучения "DB2 универсальная база данных", страница 47

                    } else {

                        currentRow.columnTypes.put(currentColumn, attributes.getValue(i));

                    }

                }

            }

        }

    }

    public void characters(char[] ch, int offset, int length) throws SAXException {

        if(currentCharacters == null){

            currentCharacters = (new String(ch, offset, length).trim());

        } else {

            currentCharacters += (new String(ch, offset, length).trim());

        }

    }

    public void endElement(String uri, String localName, String qname) throws SAXException {

//        fOut.println("/"+localName);

        if(localName.equals("row")){

//      Конец строки

            rows.add(currentRow);

            currentRow = null;

        } else if(localName.equals("key")){

//      Конец ключа

            doKey = false;

        } else if(localName.equals("column-value")){

//      Конец значения ключа поиска или столбца данных

            if(doKey){

                currentRow.keys.put(currentColumn, currentCharacters);

            } else {

                currentRow.columns.put(currentColumn, currentCharacters);

            }

            currentCharacters = null;

            currentColumn = null;

        }

    }

    public void endDocument() throws SAXException {

//        fOut.println("/document");

        goodDTD = false;

        for(int i = 0; i < rows.size(); i++){

            Row.buildSql((Row)rows.get(i));

        }

    }

    //SAX2 ErrorHandler Methods

    public void warning(SAXParseException saxEx) throws SAXException {

        fOut.println("SAX2 предупреждение:");

        fOut.print("Документ ");

        if(goodDTD){

            fOut.println(locator.getSystemId());    //Warning in XML-file

        } else {

            fOut.println(locator.getPublicId());    //Warning in DTD-file

        }

        fOut.println("Строка: "+locator.getLineNumber());

        fOut.println("Столбец: "+locator.getColumnNumber());

        fOut.println(saxEx.getMessage());

    }

    public void error(SAXParseException saxEx) throws SAXException {

        fatalError(saxEx);

    }

    public void fatalError(SAXParseException saxEx) throws SAXException {

        fOut.println("SAX2 критическая ошибка:");

        fOut.print("Документ ");

        if(goodDTD){

            fOut.println(locator.getSystemId());    //Error in XML-file

        } else {

            fOut.println(locator.getPublicId());    //Error in DTD-file

        }

        fOut.println("Строка: "+locator.getLineNumber());

        fOut.println("Столбец: "+locator.getColumnNumber());

        fOut.println(saxEx.getMessage());

        fOut.println("Продолжение работы невозможно.");

        throw saxEx;

    }

    public static void main(String[] args){

        try{

            if(args.length != 1){

                System.out.println("Usage: java fromXML xml_file");

                return;

            }

            RowSet rowSet = new RowSet();

            XMLReader reader = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");

            reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", true);

            reader.setFeature("http://xml.org/sax/features/namespaces", true);

            reader.setFeature("http://xml.org/sax/features/validation", true);

            reader.setDTDHandler(rowSet);

            reader.setErrorHandler(rowSet);

            reader.setContentHandler(rowSet);

            try{

                reader.parse(args[0]);

            } catch(SAXParseException saxPEx){

            }

            Hashtable env = new Hashtable();

            env.put(Context.INITIAL_CONTEXT_FACTORY, "com.ibm.websphere.naming.WsnInitialContextFactory");

            env.put(Context.URL_PKG_PREFIXES, "com.ibm.ws.naming");