public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { String strId = ""; String strBody = ""; // Parse the xml and read data (page id and article body) // Using XOM library Builder builder = new Builder(); try { Document doc = builder.build(value.toString(), null); Nodes nodeId = doc.query("//eecs485_article_id"); strId = nodeId.get(0).getChild(0).getValue(); Nodes nodeBody = doc.query("//eecs485_article_body"); strBody = nodeBody.get(0).getChild(0).getValue(); } catch (ParsingException ex) { System.out.println("Not well-formed."); System.out.println(ex.getMessage()); } catch (IOException ex) { System.out.println("io exception"); } // Tokenize document body Pattern pattern = Pattern.compile("\\w+"); Matcher matcher = pattern.matcher(strBody); while (matcher.find()) { // Write the parsed token // key = term, docid value = 1 context.write(new Text(matcher.group() + "," + strId), one); } }
private Collection<ConnectableDefinition> loadConnectableDefs(File connectableDefs) { Collection<ConnectableDefinition> defs = new HashSet<ConnectableDefinition>(); try { Builder parser = new Builder(); Document doc = parser.build(connectableDefs); Element root = doc.getRootElement(); Elements definitions = root.getChildElements(); for (int i = 0; i < definitions.size(); i++) { Element definition = definitions.get(i); String id = definition.getAttributeValue("id"); String pinCount = definition.getAttributeValue("pins"); String type = definition.getAttributeValue("type"); ConnectableDefinition d = new ConnectableDefinition(id, type, Integer.parseInt(pinCount)); Elements pins = definition.getChildElements(); for (int j = 0; j < pins.size(); j++) { Element pinElement = pins.get(j); String pinNumber = pinElement.getAttributeValue("number"); String pinName = pinElement.getAttributeValue("name"); d.addPin(Integer.parseInt(pinNumber), pinName); } defs.add(d); } } catch (ParsingException ex) { System.err.println("malformed XML file : " + ex.getMessage()); } catch (IOException ex) { System.err.println("io error : " + ex.getMessage()); } return defs; }
private FogBugzCommunicationException parsingError(ParsingException e, String url) { // XOM catches runtime exceptions and wraps them as parsing errors. OperationCanceledException // isn't error though, // so we ignore parsing exception, and rethrow original operation cancelled if (e.getCause() instanceof OperationCanceledException) { throw (OperationCanceledException) e.getCause(); } return new FogBugzCommunicationException("Cannot parse FogBugz response", e, url); }
protected ComponentSet loadSet(File componentsDef, File connectionsDef) { ComponentSet set = null; // for collecting all connectables Map<String, Connectable> connectables = new HashMap<String, Connectable>(); try { // open the components.xml file Builder builder = new Builder(); Document doc = builder.build(componentsDef); Element componentsRoot = doc.getRootElement(); int rows = Integer.parseInt(componentsRoot.getAttributeValue("rows")); int cols = Integer.parseInt(componentsRoot.getAttributeValue("cols")); boolean autoLocate = Boolean.parseBoolean(componentsRoot.getAttributeValue("autoLocate")); // add connectables (components need to be located in the set, // endpoints are not so they are just added to the connectables map Elements componentElements = componentsRoot.getChildElements(); if (autoLocate) { set = addComponentsWithAutoLocate(connectables, rows, cols, componentElements); } else { set = addComponents(connectables, rows, cols, componentElements); } doc = builder.build(connectionsDef); Element connectionsRoot = doc.getRootElement(); // add connections Elements connections = connectionsRoot.getChildElements(); for (int i = 0; i < connections.size(); i++) { Element c = connections.get(i); String from = c.getAttributeValue("from"); String to = c.getAttributeValue("to"); String fromPinLabel = c.getAttributeValue("fromPin"); String toPinLabel = c.getAttributeValue("toPin"); Connectable fromComp = connectables.get(from); Connectable toComp = connectables.get(to); Collection<Pin> fromPins = fromComp.getPins(fromPinLabel); Collection<Pin> toPins = toComp.getPins(toPinLabel); Connection con = new Connection(fromComp, fromPins, toComp, toPins); set.addConnection(con); } } catch (ParsingException ex) { System.err.println("malformed XML file : " + ex.getMessage()); } catch (IOException ex) { System.err.println("io error : " + ex.getMessage()); } return set; }
@Override protected List<AlignmentFile> getAlignmentFiles(int currentIter) { // sort input file alphabetically to look nicer File inputDirectory = new File(this.inputDirectory); File[] listOfFiles = inputDirectory.listFiles(); Arrays.sort(listOfFiles, new NaturalOrderFilenameComparator()); List<AlignmentFile> alignmentDataList = new ArrayList<AlignmentFile>(); List<Feature> allFeatures = new ArrayList<Feature>(); // load feature data for (int i = 0; i < listOfFiles.length; i++) { if (listOfFiles[i].isFile()) { File myFile = listOfFiles[i]; String filename = myFile.getName(); String path = myFile.getPath(); String extension = filename.substring(filename.lastIndexOf('.') + 1); // ignore other crap files in the directory if (!"txt".equals(extension)) { continue; } List<Feature> features = new ArrayList<Feature>(); try { features = this.loadFeatures(path); } catch (ValidityException e) { e.printStackTrace(); } catch (ParsingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } AlignmentFile alignmentData = new AlignmentFile(i, myFile, features); if (verbose) { System.out.println( "Load " + filename + " with " + alignmentData.getFeaturesCount() + " features"); } alignmentDataList.add(alignmentData); allFeatures.addAll(alignmentData.getFeatures()); } } this.alignmentFiles = alignmentDataList; return alignmentDataList; }
public static void main(String[] args) { if (args.length <= 0) { System.out.println("Usage: java nu.xom.samples.PureValidator URL"); return; } try { Builder parser = new Builder(true, new MinimalNodeFactory()); parser.build(args[0]); System.out.println(args[0] + " is valid."); } catch (ValidityException ex) { System.out.println(args[0] + " is not valid."); System.out.println(ex.getMessage()); System.out.println(" at line " + ex.getLineNumber() + ", column " + ex.getColumnNumber()); } catch (ParsingException ex) { System.out.println(args[0] + " is not well-formed."); System.out.println(ex.getMessage()); System.out.println(" at line " + ex.getLineNumber() + ", column " + ex.getColumnNumber()); } catch (IOException ex) { System.out.println("Due to an IOException, the parser could not check " + args[0]); } }
public static SaxonXQueryExpression.Result evaluateXQuery( final SaxonXQueryExpression xquery, Object context, Map<String, Object> parameterValues, final RowProcessor processor, CommandContext commandContext) throws TeiidProcessingException, TeiidComponentException { DynamicQueryContext dynamicContext = new DynamicQueryContext(xquery.config); SaxonXQueryExpression.Result result = new SaxonXQueryExpression.Result(); try { try { for (Map.Entry<String, Object> entry : parameterValues.entrySet()) { Object value = entry.getValue(); if (value instanceof SQLXML) { value = XMLSystemFunctions.convertToSource(value); result.sources.add((Source) value); value = wrapStax((Source) value, xquery.getConfig()); } else if (value instanceof java.util.Date) { value = XMLSystemFunctions.convertToAtomicValue(value); } else if (value instanceof BinaryType) { value = new HexBinaryValue(((BinaryType) value).getBytesDirect()); } dynamicContext.setParameter(entry.getKey(), value); } } catch (TransformerException e) { throw new TeiidProcessingException(QueryPlugin.Event.TEIID30148, e); } if (context != null) { Source source = XMLSystemFunctions.convertToSource(context); result.sources.add(source); source = wrapStax(source, xquery.getConfig()); if (xquery.contextRoot != null) { // create our own filter as this logic is not provided in the free saxon ProxyReceiver filter = new PathMapFilter(xquery.contextRoot); AugmentedSource sourceInput = AugmentedSource.makeAugmentedSource(source); sourceInput.addFilter(filter); source = sourceInput; // use streamable processing instead if (xquery.streamingPath != null && processor != null) { if (LogManager.isMessageToBeRecorded(LogConstants.CTX_DQP, MessageLevel.DETAIL)) { LogManager.logDetail( LogConstants.CTX_DQP, "Using stream processing for evaluation of", xquery.xQueryString); // $NON-NLS-1$ } // set to non-blocking in case default expression evaluation blocks boolean isNonBlocking = commandContext.isNonBlocking(); commandContext.setNonBlocking(true); final StreamingTransform myTransform = new StreamingTransform() { public Nodes transform(Element elem) { processor.processRow(XQueryEvaluator.wrap(elem, xquery.config)); return NONE; } }; Builder builder = new Builder( new SaxonReader(xquery.config, sourceInput), false, new StreamingPathFilter(xquery.streamingPath, xquery.namespaceMap) .createNodeFactory(null, myTransform)); try { // the builder is hard wired to parse the source, but the api will throw an exception // if the stream is null builder.build(FAKE_IS); return result; } catch (ParsingException e) { if (e.getCause() instanceof TeiidRuntimeException) { RelationalNode.unwrapException((TeiidRuntimeException) e.getCause()); } throw new TeiidProcessingException( QueryPlugin.Event.TEIID30151, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30151)); } catch (IOException e) { throw new TeiidProcessingException( QueryPlugin.Event.TEIID30151, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30151)); } finally { if (!isNonBlocking) { commandContext.setNonBlocking(false); } } } } DocumentInfo doc; try { doc = xquery.config.buildDocument(source); } catch (XPathException e) { throw new TeiidProcessingException( QueryPlugin.Event.TEIID30151, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30151)); } dynamicContext.setContextItem(doc); } try { result.iter = xquery.xQuery.iterator(dynamicContext); return result; } catch (TransformerException e) { throw new TeiidProcessingException( QueryPlugin.Event.TEIID30152, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30152)); } } finally { if (result.iter == null) { result.close(); } } }
/** * Removes a model. Removes extension and any files. * * @param model */ public void removeModel(IDSTest model, String pluginID) { // Remove from DSBusinessModel dsBusinessModel.getTests().remove(model); for (Endpoint ep : dsBusinessModel.getEndpoints()) { if (ep.getTests() != null) { ep.getTests().remove(model); logger.debug("Removed model " + model.getName() + " from EP " + ep.getName()); } } // Remove extensions in models.container plugin // ============================================= File pluginXMLfile; try { pluginXMLfile = new File(FileUtil.getFilePath("plugin.xml", pluginID)); Builder parser = new Builder(); Document doc = parser.build(pluginXMLfile); Element root = doc.getRootElement(); Element extension = null; // Find extension in plugin.xml, if exists Elements existingExtensions = root.getChildElements("extension"); if (existingExtensions != null && existingExtensions.size() > 0) { for (int i = 0; i < existingExtensions.size(); i++) { extension = existingExtensions.get(i); // If exists a model with same name, remove it Elements existingTests = extension.getChildElements("test"); if (existingTests != null && existingTests.size() > 0) { for (int j = 0; j < existingTests.size(); j++) { Element test = existingTests.get(j); String testName = test.getAttribute("name").getValue(); // Remove spaces included by XML serialization while (testName.contains(" ")) testName = testName.replace(" ", " "); if (model.getName().equals(testName)) { test.getParent().removeChild(test); logger.debug("Removing existing model extension: " + model.getName()); // Also remove the files Elements resources = test.getChildElements("resource"); for (int rescnt = 0; rescnt < resources.size(); rescnt++) { Element resource = resources.get(rescnt); String path = resource.getAttribute("path").getValue(); try { File reFile = new File(FileUtil.getFilePath(path, pluginID)); if (reFile.exists()) { logger.debug("Removing file: " + reFile); reFile.delete(); } else logger.debug("Unable to locate file to remove: " + path); } catch (Exception e) { logger.error("Problems removing file: " + path); } } } } } } } // Serialize the updated plugin.xml to file Serializer serializer = new Serializer(new FileOutputStream(pluginXMLfile)); serializer.setIndent(4); serializer.setMaxLength(64); serializer.write(doc); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ValidityException e) { e.printStackTrace(); } catch (ParsingException e) { e.printStackTrace(); } }