Search in sources :

Example 36 with SAXException

use of org.xml.sax.SAXException in project j2objc by google.

the class ToStream method attributeDecl.

/**
     * Report an attribute type declaration.
     *
     * <p>Only the effective (first) declaration for an attribute will
     * be reported.  The type will be one of the strings "CDATA",
     * "ID", "IDREF", "IDREFS", "NMTOKEN", "NMTOKENS", "ENTITY",
     * "ENTITIES", or "NOTATION", or a parenthesized token group with
     * the separator "|" and all whitespace removed.</p>
     *
     * @param eName The name of the associated element.
     * @param aName The name of the attribute.
     * @param type A string representing the attribute type.
     * @param valueDefault A string representing the attribute default
     *        ("#IMPLIED", "#REQUIRED", or "#FIXED") or null if
     *        none of these applies.
     * @param value A string representing the attribute's default value,
     *        or null if there is none.
     * @exception SAXException The application may raise an exception.
     */
public void attributeDecl(String eName, String aName, String type, String valueDefault, String value) throws SAXException {
    // Do not inline external DTD
    if (m_inExternalDTD)
        return;
    try {
        final java.io.Writer writer = m_writer;
        DTDprolog();
        writer.write("<!ATTLIST ");
        writer.write(eName);
        writer.write(' ');
        writer.write(aName);
        writer.write(' ');
        writer.write(type);
        if (valueDefault != null) {
            writer.write(' ');
            writer.write(valueDefault);
        }
        //writer.write(" ");
        //writer.write(value);
        writer.write('>');
        writer.write(m_lineSep, 0, m_lineSepLen);
    } catch (IOException e) {
        throw new SAXException(e);
    }
}
Also used : Writer(java.io.Writer) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException)

Example 37 with SAXException

use of org.xml.sax.SAXException in project j2objc by google.

the class ToXMLStream method startDocumentInternal.

/**
     * Receive notification of the beginning of a document.
     *
     * @throws org.xml.sax.SAXException Any SAX exception, possibly
     *            wrapping another exception.
     *
     * @throws org.xml.sax.SAXException
     */
public void startDocumentInternal() throws org.xml.sax.SAXException {
    if (m_needToCallStartDocument) {
        super.startDocumentInternal();
        m_needToCallStartDocument = false;
        if (m_inEntityRef)
            return;
        m_needToOutputDocTypeDecl = true;
        m_startNewLine = false;
        /* The call to getXMLVersion() might emit an error message
             * and we should emit this message regardless of if we are 
             * writing out an XML header or not.
             */
        final String version = getXMLVersion();
        if (getOmitXMLDeclaration() == false) {
            String encoding = Encodings.getMimeEncoding(getEncoding());
            String standalone;
            if (m_standaloneWasSpecified) {
                standalone = " standalone=\"" + getStandalone() + "\"";
            } else {
                standalone = "";
            }
            try {
                final java.io.Writer writer = m_writer;
                writer.write("<?xml version=\"");
                writer.write(version);
                writer.write("\" encoding=\"");
                writer.write(encoding);
                writer.write('\"');
                writer.write(standalone);
                writer.write("?>");
                if (m_doIndent) {
                    if (m_standaloneWasSpecified || getDoctypePublic() != null || getDoctypeSystem() != null) {
                        // We almost never put a newline after the XML
                        // header because this XML could be used as
                        // an extenal general parsed entity
                        // and we don't know the context into which it
                        // will be used in the future.  Only when
                        // standalone, or a doctype system or public is
                        // specified are we free to insert a new line
                        // after the header.  Is it even worth bothering
                        // in these rare cases?                           
                        writer.write(m_lineSep, 0, m_lineSepLen);
                    }
                }
            } catch (IOException e) {
                throw new SAXException(e);
            }
        }
    }
}
Also used : IOException(java.io.IOException) SAXException(org.xml.sax.SAXException)

Example 38 with SAXException

use of org.xml.sax.SAXException in project j2objc by google.

the class ToXMLStream method addAttribute.

/**
     * Add an attribute to the current element.
     * @param uri the URI associated with the element name
     * @param localName local part of the attribute name
     * @param rawName   prefix:localName
     * @param type
     * @param value the value of the attribute
     * @param xslAttribute true if this attribute is from an xsl:attribute,
     * false if declared within the elements opening tag.
     * @throws SAXException
     */
public void addAttribute(String uri, String localName, String rawName, String type, String value, boolean xslAttribute) throws SAXException {
    if (m_elemContext.m_startTagOpen) {
        boolean was_added = addAttributeAlways(uri, localName, rawName, type, value, xslAttribute);
        /*
             * We don't run this block of code if:
             * 1. The attribute value was only replaced (was_added is false).
             * 2. The attribute is from an xsl:attribute element (that is handled
             *    in the addAttributeAlways() call just above.
             * 3. The name starts with "xmlns", i.e. it is a namespace declaration.
             */
        if (was_added && !xslAttribute && !rawName.startsWith("xmlns")) {
            String prefixUsed = ensureAttributesNamespaceIsDeclared(uri, localName, rawName);
            if (prefixUsed != null && rawName != null && !rawName.startsWith(prefixUsed)) {
                // use a different raw name, with the prefix used in the
                // generated namespace declaration
                rawName = prefixUsed + ":" + localName;
            }
        }
        addAttributeAlways(uri, localName, rawName, type, value, xslAttribute);
    } else {
        /*
             * The startTag is closed, yet we are adding an attribute?
             *
             * Section: 7.1.3 Creating Attributes Adding an attribute to an
             * element after a PI (for example) has been added to it is an
             * error. The attributes can be ignored. The spec doesn't explicitly
             * say this is disallowed, as it does for child elements, but it
             * makes sense to have the same treatment.
             *
             * We choose to ignore the attribute which is added too late.
             */
        // Generate a warning of the ignored attributes
        // Create the warning message
        String msg = Utils.messages.createMessage(MsgKey.ER_ILLEGAL_ATTRIBUTE_POSITION, new Object[] { localName });
        try {
            // Prepare to issue the warning message
            Transformer tran = super.getTransformer();
            ErrorListener errHandler = tran.getErrorListener();
            // Issue the warning message
            if (null != errHandler && m_sourceLocator != null)
                errHandler.warning(new TransformerException(msg, m_sourceLocator));
            else
                System.out.println(msg);
        } catch (TransformerException e) {
            // A user defined error handler, errHandler, may throw
            // a TransformerException if it chooses to, and if it does
            // we will wrap it with a SAXException and re-throw.
            // Of course if the handler throws another type of
            // exception, like a RuntimeException, then that is OK too.
            SAXException se = new SAXException(e);
            throw se;
        }
    }
}
Also used : ErrorListener(javax.xml.transform.ErrorListener) Transformer(javax.xml.transform.Transformer) TransformerException(javax.xml.transform.TransformerException) SAXException(org.xml.sax.SAXException)

Example 39 with SAXException

use of org.xml.sax.SAXException in project j2objc by google.

the class ParserAdapter method startElement.

/**
     * Adapter implementation method; do not call.
     * Adapt a SAX1 startElement event.
     *
     * <p>If necessary, perform Namespace processing.</p>
     *
     * @param qName The qualified (prefixed) name.
     * @param qAtts The XML attribute list (with qnames).
     * @exception SAXException The client may raise a
     *            processing exception.
     */
public void startElement(String qName, AttributeList qAtts) throws SAXException {
    // These are exceptions from the
    // first pass; they should be
    // ignored if there's a second pass,
    // but reported otherwise.
    ArrayList<SAXParseException> exceptions = null;
    // processing, dispatch this quickly.
    if (!namespaces) {
        if (contentHandler != null) {
            attAdapter.setAttributeList(qAtts);
            contentHandler.startElement("", "", qName.intern(), attAdapter);
        }
        return;
    }
    // OK, we're doing Namespace processing.
    nsSupport.pushContext();
    int length = qAtts.getLength();
    // First pass:  handle NS decls
    for (int i = 0; i < length; i++) {
        String attQName = qAtts.getName(i);
        if (!attQName.startsWith("xmlns"))
            continue;
        // Could be a declaration...
        String prefix;
        int n = attQName.indexOf(':');
        // xmlns=...
        if (n == -1 && attQName.length() == 5) {
            prefix = "";
        } else if (n != 5) {
            // (and similarly named) attributes ... at most, warn
            continue;
        } else
            // xmlns:foo=...
            prefix = attQName.substring(n + 1);
        String value = qAtts.getValue(i);
        if (!nsSupport.declarePrefix(prefix, value)) {
            reportError("Illegal Namespace prefix: " + prefix);
            continue;
        }
        if (contentHandler != null)
            contentHandler.startPrefixMapping(prefix, value);
    }
    // Second pass: copy all relevant
    // attributes into the SAX2 AttributeList
    // using updated prefix bindings
    atts.clear();
    for (int i = 0; i < length; i++) {
        String attQName = qAtts.getName(i);
        String type = qAtts.getType(i);
        String value = qAtts.getValue(i);
        // Declaration?
        if (attQName.startsWith("xmlns")) {
            String prefix;
            int n = attQName.indexOf(':');
            if (n == -1 && attQName.length() == 5) {
                prefix = "";
            } else if (n != 5) {
                // XML namespaces spec doesn't discuss "xmlnsf:oo"
                // (and similarly named) attributes ... ignore
                prefix = null;
            } else {
                prefix = attQName.substring(6);
            }
            // Yes, decl:  report or prune
            if (prefix != null) {
                if (prefixes) {
                    if (uris)
                        // note funky case:  localname can be null
                        // when declaring the default prefix, and
                        // yet the uri isn't null.
                        atts.addAttribute(NamespaceSupport.XMLNS, prefix, attQName.intern(), type, value);
                    else
                        atts.addAttribute("", "", attQName.intern(), type, value);
                }
                continue;
            }
        }
        // Not a declaration -- report
        try {
            String[] attName = processName(attQName, true, true);
            atts.addAttribute(attName[0], attName[1], attName[2], type, value);
        } catch (SAXException e) {
            if (exceptions == null) {
                exceptions = new ArrayList<SAXParseException>();
            }
            exceptions.add((SAXParseException) e);
            atts.addAttribute("", attQName, attQName, type, value);
        }
    }
    // now handle the deferred exception reports
    if (exceptions != null && errorHandler != null) {
        for (SAXParseException ex : exceptions) {
            errorHandler.error(ex);
        }
    }
    // OK, finally report the event.
    if (contentHandler != null) {
        String[] name = processName(qName, false, false);
        contentHandler.startElement(name[0], name[1], name[2], atts);
    }
}
Also used : SAXParseException(org.xml.sax.SAXParseException) ArrayList(java.util.ArrayList) SAXException(org.xml.sax.SAXException)

Example 40 with SAXException

use of org.xml.sax.SAXException in project j2objc by google.

the class ProcessorExsltFunction method startElement.

/**
   * Start an ElemExsltFunction. Verify that it is top level and that it has a name attribute with a
   * namespace.
   */
public void startElement(StylesheetHandler handler, String uri, String localName, String rawName, Attributes attributes) throws SAXException {
    //System.out.println("ProcessorFunction.startElement()");
    String msg = "";
    if (!(handler.getElemTemplateElement() instanceof Stylesheet)) {
        msg = "func:function element must be top level.";
        handler.error(msg, new SAXException(msg));
    }
    super.startElement(handler, uri, localName, rawName, attributes);
    String val = attributes.getValue("name");
    int indexOfColon = val.indexOf(":");
    if (indexOfColon > 0) {
    //String prefix = val.substring(0, indexOfColon);
    //String localVal = val.substring(indexOfColon + 1);
    //String ns = handler.getNamespaceSupport().getURI(prefix);
    //if (ns.length() > 0)
    //  System.out.println("fullfuncname " + ns + localVal);
    } else {
        msg = "func:function name must have namespace";
        handler.error(msg, new SAXException(msg));
    }
}
Also used : Stylesheet(org.apache.xalan.templates.Stylesheet) SAXException(org.xml.sax.SAXException)

Aggregations

SAXException (org.xml.sax.SAXException)1248 IOException (java.io.IOException)754 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)466 Document (org.w3c.dom.Document)316 DocumentBuilder (javax.xml.parsers.DocumentBuilder)265 InputSource (org.xml.sax.InputSource)249 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)203 InputStream (java.io.InputStream)160 Element (org.w3c.dom.Element)143 NodeList (org.w3c.dom.NodeList)131 File (java.io.File)129 SAXParseException (org.xml.sax.SAXParseException)119 Node (org.w3c.dom.Node)106 ByteArrayInputStream (java.io.ByteArrayInputStream)99 StringReader (java.io.StringReader)92 TransformerException (javax.xml.transform.TransformerException)91 SAXParser (javax.xml.parsers.SAXParser)83 FileInputStream (java.io.FileInputStream)78 ArrayList (java.util.ArrayList)78 XMLReader (org.xml.sax.XMLReader)70