Example usage for javax.xml.stream XMLInputFactory IS_NAMESPACE_AWARE

List of usage examples for javax.xml.stream XMLInputFactory IS_NAMESPACE_AWARE

Introduction

In this page you can find the example usage for javax.xml.stream XMLInputFactory IS_NAMESPACE_AWARE.

Prototype

String IS_NAMESPACE_AWARE

To view the source code for javax.xml.stream XMLInputFactory IS_NAMESPACE_AWARE.

Click Source Link

Document

The property used to turn on/off namespace support, this is to support XML 1.0 documents, only the true setting must be supported

Usage

From source file:org.sonar.core.technicaldebt.TechnicalDebtXMLImporter.java

private SMInputFactory initStax() {
    XMLInputFactory xmlFactory = XMLInputFactory2.newInstance();
    xmlFactory.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE);
    xmlFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.FALSE);
    xmlFactory.setProperty(XMLInputFactory.SUPPORT_DTD, Boolean.FALSE);
    xmlFactory.setProperty(XMLInputFactory.IS_VALIDATING, Boolean.FALSE);
    return new SMInputFactory(xmlFactory);
}

From source file:org.sonar.cxx.sensors.utils.StaxParser.java

/**
 * StaxParser for a given stream handler and ISO control chars set awareness to on. The ISO control chars in the XML
 * file will be replaced by simple spaces, useful for potentially bogus XML files to parse, this has a small perfs
 * overhead so use it only when necessary
 *
 * @param streamHandler the XML stream handler
 * @param isoControlCharsAwareParser true or false
 *//*www . j  a  v  a  2  s .c  o m*/
public StaxParser(XmlStreamHandler streamHandler, boolean isoControlCharsAwareParser) {
    this.streamHandler = streamHandler;
    XMLInputFactory xmlFactory = XMLInputFactory.newInstance();
    if (xmlFactory instanceof WstxInputFactory) {
        WstxInputFactory wstxInputfactory = (WstxInputFactory) xmlFactory;
        wstxInputfactory.configureForLowMemUsage();
        wstxInputfactory.getConfig().setUndeclaredEntityResolver(new UndeclaredEntitiesXMLResolver());
    }
    xmlFactory.setProperty(XMLInputFactory.IS_VALIDATING, Boolean.FALSE);
    xmlFactory.setProperty(XMLInputFactory.SUPPORT_DTD, Boolean.FALSE);
    xmlFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.FALSE);
    this.isoControlCharsAwareParser = isoControlCharsAwareParser;
    inf = new SMInputFactory(xmlFactory);
}

From source file:org.sonar.plugins.ada.rules.AdaProfileImporter.java

/**
 * @return//from ww w  .  ja  va  2 s.  co m
 */
private SMInputFactory initStax() {
    XMLInputFactory xmlFactory = XMLInputFactory2.newInstance();
    xmlFactory.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE);
    xmlFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.FALSE);
    xmlFactory.setProperty(XMLInputFactory.SUPPORT_DTD, Boolean.FALSE);
    xmlFactory.setProperty(XMLInputFactory.IS_VALIDATING, Boolean.FALSE);
    SMInputFactory inputFactory = new SMInputFactory(xmlFactory);
    return inputFactory;
}

From source file:org.sonar.plugins.android.lint.AndroidLintProfileExporter.java

private void loadRuleKeys() {
    XMLInputFactory xmlFactory = XMLInputFactory.newInstance();
    xmlFactory.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE);
    xmlFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.FALSE);
    // just so it won't try to load DTD in if there's DOCTYPE
    xmlFactory.setProperty(XMLInputFactory.SUPPORT_DTD, Boolean.FALSE);
    xmlFactory.setProperty(XMLInputFactory.IS_VALIDATING, Boolean.FALSE);
    SMInputFactory inputFactory = new SMInputFactory(xmlFactory);
    InputStream inputStream = getClass().getResourceAsStream(AndroidLintRulesDefinition.RULES_XML_PATH);
    InputStreamReader reader = new InputStreamReader(inputStream, Charsets.UTF_8);
    try {//from  w  w  w  . j a  va2  s  . c  o  m
        SMHierarchicCursor rootC = inputFactory.rootElementCursor(reader);
        rootC.advance(); // <rules>

        SMInputCursor rulesC = rootC.childElementCursor("rule");
        while (rulesC.getNext() != null) {
            // <rule>
            SMInputCursor cursor = rulesC.childElementCursor();
            while (cursor.getNext() != null) {
                if (StringUtils.equalsIgnoreCase("key", cursor.getLocalName())) {
                    String key = StringUtils.trim(cursor.collectDescendantText(false));
                    ruleKeys.add(key);
                }
            }
        }

    } catch (XMLStreamException e) {
        throw new IllegalStateException("XML is not valid", e);
    }
}

From source file:org.sonar.plugins.checkstyle.CheckstyleProfileImporter.java

private SMInputFactory initStax() {
    XMLInputFactory xmlFactory = XMLInputFactory2.newInstance();
    xmlFactory.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE);
    xmlFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.FALSE);
    xmlFactory.setProperty(XMLInputFactory.SUPPORT_DTD, Boolean.FALSE);
    xmlFactory.setProperty(XMLInputFactory.IS_VALIDATING, Boolean.FALSE);
    SMInputFactory inputFactory = new SMInputFactory(xmlFactory);
    return inputFactory;
}

From source file:org.sonar.plugins.javascript.jslint.JsLintXmlRuleParser.java

public List<JsLintRule> parse(Reader reader) {
    XMLInputFactory xmlFactory = XMLInputFactory.newInstance();
    xmlFactory.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE);
    xmlFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.FALSE);
    // just so it won't try to load DTD in if there's DOCTYPE
    xmlFactory.setProperty(XMLInputFactory.SUPPORT_DTD, Boolean.FALSE);
    xmlFactory.setProperty(XMLInputFactory.IS_VALIDATING, Boolean.FALSE);
    SMInputFactory inputFactory = new SMInputFactory(xmlFactory);
    try {/* ww w .ja v  a  2  s . c o  m*/
        SMHierarchicCursor rootC = inputFactory.rootElementCursor(reader);
        rootC.advance(); // <rules>
        List<JsLintRule> rules = new ArrayList<JsLintRule>();

        SMInputCursor rulesC = rootC.childElementCursor("rule");
        while (rulesC.getNext() != null) {
            // <rule>
            JsLintRule rule = new JsLintRule();
            rules.add(rule);

            processRule(rule, rulesC);
        }
        return rules;

    } catch (XMLStreamException e) {
        throw new SonarException("XML is not valid", e);
    }
}

From source file:org.sonar.plugins.ndepend.QueryLoader.java

public ImmutableList<NdependQuery> getQueries(Reader reader) {
    XMLInputFactory xmlFactory = XMLInputFactory.newInstance();
    xmlFactory.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE);
    xmlFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.FALSE);
    xmlFactory.setProperty(XMLInputFactory.SUPPORT_DTD, Boolean.FALSE);
    SMInputFactory inputFactory = new SMInputFactory(xmlFactory);
    ImmutableList.Builder<NdependQuery> builder = new Builder<NdependQuery>();
    try {/*from w w w  . jav a 2  s.c o  m*/
        SMHierarchicCursor root = inputFactory.rootElementCursor(reader);
        root.advance();

        SMInputCursor rules = root.childElementCursor("rule");
        while (rules.getNext() != null) {
            builder.add(processRule(rules));
        }
        return builder.build();

    } catch (XMLStreamException e) {
        throw new IllegalStateException("XML is not valid", e);
    }
}

From source file:org.sonar.plugins.xaml.fxcop.XamlFxCopRulesSensor.java

protected SMInputFactory initStax() {
    XMLInputFactory xmlFactory = XMLInputFactory2.newInstance();
    xmlFactory.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE);
    xmlFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.FALSE);
    xmlFactory.setProperty(XMLInputFactory.SUPPORT_DTD, Boolean.FALSE);
    xmlFactory.setProperty(XMLInputFactory.IS_VALIDATING, Boolean.FALSE);
    return new SMInputFactory(xmlFactory);
}

From source file:org.sonar.server.duplication.ws.DuplicationsParser.java

private static SMInputFactory initStax() {
    XMLInputFactory xmlFactory = XMLInputFactory.newInstance();
    xmlFactory.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE);
    xmlFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.FALSE);
    // just so it won't try to load DTD in if there's DOCTYPE
    xmlFactory.setProperty(XMLInputFactory.SUPPORT_DTD, Boolean.FALSE);
    xmlFactory.setProperty(XMLInputFactory.IS_VALIDATING, Boolean.FALSE);
    return new SMInputFactory(xmlFactory);
}

From source file:presentation.webgui.vitroappservlet.uploadService.UploadServlet.java

private void doFileUpload(HttpSession session, HttpServletRequest request, HttpServletResponse response)
        throws IOException {

    String fname = "";
    HashMap<String, String> myFileRequestParamsHM = new HashMap<String, String>();

    try {// w w  w .j a va 2  s . c o m
        FileUploadListener listener = new FileUploadListener(request.getContentLength());
        FileItemFactory factory = new MonitoredDiskFileItemFactory(listener);

        ServletFileUpload upload = new ServletFileUpload(factory);
        //        upload.setSizeMax(83886080); /* the unit is bytes */

        FileItem fileItem = null;
        fileItem = myrequestGetParameter(upload, request, myFileRequestParamsHM);

        String mode = myFileRequestParamsHM.get("mode");

        session.setAttribute("FILE_UPLOAD_STATS" + mode, listener.getFileUploadStats());

        boolean hasError = false;

        if (fileItem != null) {
            /**
             * (for KML only files) ( not prefabs (collada) or icons or images)
             */
            WstxInputFactory f = null;
            XMLStreamReader2 sr = null;
            SMInputCursor iroot = null;
            if (mode.equals("3dFile") || mode.equals("LinePlaceMarksFile")
                    || mode.equals("RoomCenterPointsFile")) {
                f = new WstxInputFactory();
                f.configureForConvenience();
                // Let's configure factory 'optimally'...
                f.setProperty(XMLInputFactory.IS_COALESCING, Boolean.FALSE);
                f.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.FALSE);

                sr = (XMLStreamReader2) f.createXMLStreamReader(fileItem.getInputStream());
                iroot = SMInputFactory.rootElementCursor(sr);
                // If we needed to store some information about preceding siblings,
                // we should enable tracking. (we need it for  mygetElementValueStaxMultiple method)
                iroot.setElementTracking(SMInputCursor.Tracking.PARENTS);

                iroot.getNext();
                if (!"kml".equals(iroot.getLocalName().toLowerCase())) {
                    hasError = true;
                    listener.getFileUploadStats().setCurrentStatus("finito");
                    session.setAttribute("FILE_UPLOAD_STATS" + mode, listener.getFileUploadStats());
                    sendCompleteResponse(myFileRequestParamsHM, response, hasError,
                            "Root element not kml, as expected, but " + iroot.getLocalName());
                    return;
                }
            }

            fname = "";
            if (mode.equals("3dFile")) {
                if ((fileItem.getSize() / 1024) > 25096) { // with woodstox stax, file size should not be a problem. Let's put some limit however!
                    hasError = true;
                    listener.getFileUploadStats().setCurrentStatus("finito");
                    session.setAttribute("FILE_UPLOAD_STATS" + mode, listener.getFileUploadStats());
                    sendCompleteResponse(myFileRequestParamsHM, response, hasError,
                            "File is very large for XML handler to process!");
                    return;
                }

                fname = "";
                String[] elementsToFollow = { "document", "name" };
                Vector<String> resultValues = SMTools.mygetElementValueStax(iroot, elementsToFollow, 0);
                if (resultValues != null && !resultValues.isEmpty()) {
                    fname = resultValues.elementAt(0);
                }

                if (!fname.equals("")) {
                    // check for kml extension and Add it if necessary!!
                    int lastdot = fname.lastIndexOf('.');
                    if (lastdot != -1) {
                        if (lastdot == 0) // if it is the first char then ignore it and add an extension anyway
                        {
                            fname += ".kml";
                        } else if (lastdot < fname.length() - 1) {
                            if (!(fname.substring(lastdot + 1).toLowerCase().equals("kml"))) {
                                fname += ".kml";
                            }
                        } else if (lastdot == fname.length() - 1) {
                            fname += "kml";
                        }
                    } else {
                        fname += ".kml";
                    }

                    String realPath = this.getServletContext().getRealPath("/");
                    int lastslash = realPath.lastIndexOf(File.separator);
                    realPath = realPath.substring(0, lastslash);
                    // too slow
                    //FileWriter out = new FileWriter(realPath+File.separator+"KML"+File.separator+fname);
                    //document.sendToWriter(out);
                    // too slow
                    //StringWriter outString = new StringWriter();
                    //document.sendToWriter(outString);
                    //out.close();

                    // fast - do not process and store xml file, just store it.
                    File outFile = new File(realPath + File.separator + "Models" + File.separator + "Large"
                            + File.separator + fname);
                    outFile.createNewFile();
                    FileWriter tmpoutWriter = new FileWriter(outFile);
                    BufferedWriter buffWriter = new BufferedWriter(tmpoutWriter);
                    buffWriter.write(new String(fileItem.get()));
                    buffWriter.flush();
                    buffWriter.close();
                    tmpoutWriter.close();
                } else {
                    hasError = true;
                    listener.getFileUploadStats().setCurrentStatus("finito");
                    session.setAttribute("FILE_UPLOAD_STATS" + mode, listener.getFileUploadStats());
                    sendCompleteResponse(myFileRequestParamsHM, response, hasError,
                            "No name tag found inside the KML file!");
                    return;
                }
            } else if (mode.equals("LinePlaceMarksFile")) {
                fname = "";
                String[] elementsToFollow = { "document", "folder", "placemark", "point", "coordinates" };
                Vector<String> resultValues = SMTools.mygetElementValueStax(iroot, elementsToFollow, 0);
                if (resultValues != null && resultValues.size() < 2) {
                    hasError = true;
                    listener.getFileUploadStats().setCurrentStatus("finito");
                    session.setAttribute("FILE_UPLOAD_STATS" + mode, listener.getFileUploadStats());
                    sendCompleteResponse(myFileRequestParamsHM, response, hasError,
                            "File does not contain 2 placemarks!");
                    return;
                }

                for (int i = 0; (i < resultValues.size()) && (i < 2); i++) {
                    fname = fname + ":" + resultValues.elementAt(i);
                }
            } else if (mode.equals("RoomCenterPointsFile")) {
                fname = "";
                // here: process PlaceMarks for rooms (centerpoints) in the building
                String[] elementsToFollow0 = { "document", "folder", "placemark", "point", "coordinates" };
                String[] elementsToFollow1 = { "document", "folder", "placemark", "name" };
                // add elements to follow for room names and coordinates        
                Vector<String[]> elementsToFollow = new Vector<String[]>();
                elementsToFollow.add(elementsToFollow0);
                elementsToFollow.add(elementsToFollow1);
                Vector<Vector<String>> resultValues = new Vector<Vector<String>>();
                SMTools.mygetMultipleElementValuesStax(iroot, elementsToFollow, resultValues);

                Vector<String> resultValuesForCoords = resultValues.elementAt(0);
                Vector<String> resultValuesForNames = resultValues.elementAt(1);

                if (resultValuesForCoords == null || resultValuesForCoords.size() == 0
                        || resultValuesForNames == null || resultValuesForCoords.size() == 0
                        || resultValuesForCoords.size() != resultValuesForNames.size()) {
                    hasError = true;
                    listener.getFileUploadStats().setCurrentStatus("finito");
                    session.setAttribute("FILE_UPLOAD_STATS" + mode, listener.getFileUploadStats());
                    sendCompleteResponse(myFileRequestParamsHM, response, hasError,
                            "File does not contain valid data for rooms!");
                    return;
                }

                for (int i = 0; i < resultValuesForNames.size(); i++) {
                    // since we use ;  and ':' to seperate rooms, we replace the comma's in the rooms' names.
                    if (resultValuesForNames.elementAt(i).indexOf(';') >= 0
                            || resultValuesForNames.elementAt(i).indexOf(':') >= 0) {
                        String tmp = new String(resultValuesForNames.elementAt(i));
                        tmp.replace(';', ' ');
                        tmp.replace(':', ' ');
                        resultValuesForNames.set(i, tmp);
                    }
                    fname = fname + ";" + resultValuesForNames.elementAt(i) + ":"
                            + resultValuesForCoords.elementAt(i);
                }

            } else if (mode.equals("DefaultIconfile") || mode.equals("DefaultPrefabfile")
                    || mode.equals("SpecialValueIconfile") || mode.equals("SpecialValuePrefabfile")
                    || mode.equals("NumericRangeIconfile") || mode.equals("NumericRangePrefabfile")) {
                fname = "";
                if ((fileItem.getSize() / 1024) > 10096) { // no more than 10 Mbs of size for small prefabs or icons
                    hasError = true;
                    listener.getFileUploadStats().setCurrentStatus("finito");
                    session.setAttribute("FILE_UPLOAD_STATS" + mode, listener.getFileUploadStats());
                    sendCompleteResponse(myFileRequestParamsHM, response, hasError, "File is very large!");
                    return;
                }
                fname = fileItem.getName();
                if (!fname.equals("")) {
                    String realPath = this.getServletContext().getRealPath("/");
                    int lastslash = realPath.lastIndexOf(File.separator);
                    realPath = realPath.substring(0, lastslash);

                    File outFile = new File(realPath + File.separator + "Models" + File.separator + "Media"
                            + File.separator + fname);
                    outFile.createNewFile();
                    /*
                    FileWriter tmpoutWriter = new FileWriter(outFile);
                    BufferedWriter buffWriter = new BufferedWriter(tmpoutWriter);                      
                    buffWriter.write(new String(fileItem.get()));
                    buffWriter.flush();
                    buffWriter.close();
                    tmpoutWriter.close();
                    */
                    fileItem.write(outFile);
                } else {
                    hasError = true;
                    listener.getFileUploadStats().setCurrentStatus("finito");
                    session.setAttribute("FILE_UPLOAD_STATS" + mode, listener.getFileUploadStats());
                    sendCompleteResponse(myFileRequestParamsHM, response, hasError,
                            "No valid name for uploaded file!");
                    return;
                }
            }

            fileItem.delete();
        }

        if (!hasError) {
            sendCompleteResponse(myFileRequestParamsHM, response, hasError, fname);
        } else {
            hasError = true;
            sendCompleteResponse(myFileRequestParamsHM, response, hasError,
                    "Could not process uploaded file. Please see log for details.");
        }
    } catch (Exception e) {
        boolean hasError = true;
        sendCompleteResponse(myFileRequestParamsHM, response, hasError, "::" + fname + "::" + e.getMessage());
    }
}