/** @return */
  public List<PhpCodeSnifferViolation> getViolations(File reportFile) {
    LOG.debug("Report file for PHP_CodeSniffer is " + reportFile);
    if (reportFile == null || !reportFile.exists()) {
      throw new SonarException("The XML report '" + reportFile + "' can't be found");
    }
    String reportPath = reportFile.getAbsolutePath();
    LOG.debug("Getting violations form report file");
    List<PhpCodeSnifferViolation> violations = new ArrayList<PhpCodeSnifferViolation>();
    try { // <checkstyle>
      SMInputFactory inputFactory =
          new SMInputFactory(XMLInputFactory.newInstance()); // <checkstyle>
      SMInputCursor rootNodeCursor = inputFactory.rootElementCursor(reportFile).advance(); // <file>

      SMInputCursor fileNodeCursor = rootNodeCursor.childElementCursor(FILE_NODE_NAME).advance();
      while (fileNodeCursor.asEvent() != null) {
        String fileName = fileNodeCursor.getAttrValue(FILE_NAME_ATTRIBUTE_NAME);
        SMInputCursor violationNodeCursor =
            fileNodeCursor.childElementCursor().advance(); // <error>
        while (violationNodeCursor.asEvent() != null) {
          violations.add(getViolation(fileName, violationNodeCursor));
          violationNodeCursor.advance();
        }
        fileNodeCursor.advance();
      }
      rootNodeCursor.getStreamReader().closeCompletely();
    } catch (XMLStreamException e) {
      throw new XmlParserException("Unable to parse the  XML Report '" + reportPath + "'", e);
    }
    return violations;
  }
예제 #2
0
 public List<Block> parse(
     ComponentDto component, @Nullable String duplicationsData, DbSession session) {
   Map<String, ComponentDto> componentsByKey = newHashMap();
   List<Block> blocks = newArrayList();
   if (duplicationsData != null) {
     try {
       SMInputFactory inputFactory = initStax();
       SMHierarchicCursor root =
           inputFactory.rootElementCursor(new StringReader(duplicationsData));
       root.advance(); // <duplications>
       SMInputCursor cursor = root.childElementCursor("g");
       while (cursor.getNext() != null) {
         List<Duplication> duplications = newArrayList();
         SMInputCursor bCursor = cursor.childElementCursor("b");
         while (bCursor.getNext() != null) {
           String from = bCursor.getAttrValue("s");
           String size = bCursor.getAttrValue("l");
           String componentKey = bCursor.getAttrValue("r");
           if (from != null && size != null && componentKey != null) {
             duplications.add(
                 createDuplication(componentsByKey, from, size, componentKey, session));
           }
         }
         Collections.sort(
             duplications, new DuplicationComparator(component.uuid(), component.projectUuid()));
         blocks.add(new Block(duplications));
       }
       Collections.sort(blocks, new BlockComparator());
     } catch (XMLStreamException e) {
       throw new IllegalStateException("XML is not valid", e);
     }
   }
   return blocks;
 }
예제 #3
0
  public List<Rule> 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 {
      SMHierarchicCursor rootC = inputFactory.rootElementCursor(reader);
      rootC.advance(); // <rules>
      List<Rule> rules = new ArrayList<>();

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

        processRule(rule, rulesC);
      }
      return rules;

    } catch (XMLStreamException e) {
      throw new SonarException("XML is not valid", e);
    }
  }
  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 {
      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);
    }
  }
예제 #5
0
  public RulesProfile parse(Reader reader, ValidationMessages messages) {
    RulesProfile profile = RulesProfile.create();
    SMInputFactory inputFactory = initStax();
    try {
      SMHierarchicCursor rootC = inputFactory.rootElementCursor(reader);
      rootC.advance(); // <profile>
      SMInputCursor cursor = rootC.childElementCursor();
      while (cursor.getNext() != null) {
        String nodeName = cursor.getLocalName();
        if (StringUtils.equals("rules", nodeName)) {
          SMInputCursor rulesCursor = cursor.childElementCursor("rule");
          processRules(rulesCursor, profile, messages);

        } else if (StringUtils.equals("name", nodeName)) {
          profile.setName(StringUtils.trim(cursor.collectDescendantText(false)));

        } else if (StringUtils.equals("language", nodeName)) {
          profile.setLanguage(StringUtils.trim(cursor.collectDescendantText(false)));
        }
      }
    } catch (XMLStreamException e) {
      messages.addErrorText("XML is not valid: " + e.getMessage());
    }
    checkProfile(profile, messages);
    return profile;
  }
  @Override
  public RulesProfile importProfile(Reader reader, ValidationMessages messages) {
    SMInputFactory inputFactory = initStax();
    RulesProfile profile = RulesProfile.create();

    try {
      SMHierarchicCursor rootC = inputFactory.rootElementCursor(reader);
      rootC.advance(); // <ruleset>
      SMInputCursor ruleCursor = rootC.childElementCursor(RULE_NODE);
      while (ruleCursor.getNext() != null) {
        String ruleKey = ruleCursor.getAttrValue(RULE_CLASS_ATTR);
        Rule rule = ruleFinder.findByKey(CodeNarcConstants.REPOSITORY_KEY, ruleKey);
        if (rule == null) {
          messages.addWarningText("CodeNarc rule '" + ruleKey + "' not found");
        } else {
          ActiveRule activeRule = profile.activateRule(rule, null);
          processProperties(ruleCursor, activeRule);
        }
      }

    } catch (XMLStreamException e) {
      messages.addErrorText("XML is not valid: " + e.getMessage());
    }
    return profile;
  }
 /**
  * Parses a processed violation file.
  *
  * @param file the file to parse
  */
 public void parse(File file) {
   SMInputFactory inputFactory = initStax();
   FileInputStream fileInputStream = null;
   try {
     fileInputStream = new FileInputStream(file);
     SMHierarchicCursor cursor =
         inputFactory.rootElementCursor(new InputStreamReader(fileInputStream, getEncoding()));
     SMInputCursor mainCursor = cursor.advance().childElementCursor();
     parseStyleCopViolationsBloc(mainCursor);
     cursor.getStreamReader().closeCompletely();
   } catch (XMLStreamException e) {
     throw new SonarException(
         "Error while reading StyleCop result file: " + file.getAbsolutePath(), e);
   } catch (FileNotFoundException e) {
     throw new SonarException("Cannot find StyleCop result file: " + file.getAbsolutePath(), e);
   } finally {
     IOUtils.closeQuietly(fileInputStream);
   }
 }
  public void load(RulesDefinition.NewExtendedRepository repo, 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 {
      SMHierarchicCursor rootC = inputFactory.rootElementCursor(reader);
      rootC.advance(); // <rules>

      SMInputCursor rulesC = rootC.childElementCursor("rule");
      while (rulesC.getNext() != null) {
        // <rule>
        processRule(repo, rulesC);
      }

    } catch (XMLStreamException e) {
      throw new IllegalStateException("XML is not valid", e);
    }
  }
예제 #9
0
 /**
  * Creates a new instance of Index Of Model Mappings read from the corresponding file (Used for
  * persistence)
  */
 public static void parseIndexFromFile() {
   // Read file and create the Index
   // check if file exists. If it exists open it and parse it.
   // Else return
   //
   File inFile = new File(Model3dIndex.getIndexPath() + "modelIndex.xml");
   // error state check
   if (inFile.exists()) {
     try {
       Model3dIndex.getListofAllMetaEntries().clear();
       FileReader tmpInReader = new FileReader(inFile);
       WstxInputFactory fin = new WstxInputFactory();
       fin.configureForConvenience();
       fin.setProperty(
           XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.FALSE); // <-- NEEDED TO GET ATTRIBUTES!
       // input
       XMLStreamReader2 sr = (XMLStreamReader2) fin.createXMLStreamReader(tmpInReader);
       SMInputCursor inputRootElement = SMInputFactory.rootElementCursor(sr);
       inputRootElement.getNext();
       SMInputCursor childInElement = inputRootElement.childCursor();
       String myText = "";
       while (childInElement.getNext() != null) {
         if (!childInElement.getCurrEvent().hasText()
             && childInElement
                 .getLocalName()
                 .toLowerCase()
                 .equals(Model3dIndex.getMetaEntryTag().toLowerCase())) {
           Model3dIndex.getListofAllMetaEntries().add(new Model3dIndexEntry(childInElement));
         }
       }
       tmpInReader.close();
     } catch (Exception e) {
       return;
     }
   } else return;
 }
예제 #10
0
 public void parse(Reader xmlReader) throws XMLStreamException {
   if (isoControlCharsAwareParser) {
     throw new SonarException("Method call not supported when isoControlCharsAwareParser=true");
   }
   parse(inf.rootElementCursor(xmlReader));
 }
예제 #11
0
 public void parse(InputStream xmlInput) throws XMLStreamException {
   xmlInput = isoControlCharsAwareParser ? new ISOControlCharAwareInputStream(xmlInput) : xmlInput;
   parse(inf.rootElementCursor(xmlInput));
 }