XML Literals in VB.NET Code don't seem work with #if statements - vb.net

I have a vb.net project that uses an XML literal to configure the user interface navigation bar. The code looks something like this and has been working for many years.
Private ReadOnly _actionTreeXml As XElement =
<nodes>
<node key="any" name="Top">
<node key="log" name="***LOGIN***" type="everybody"></node>
<node key="op" name="Home" ctrl="uiHomePage" type="mfg"></node>
<node key="barcode" name="Barcode Entry" ctrl="EditMfgEntry" type="mfg"></node>
<node key="wip" name="Work in Progress" ctrl="QueryWIP" type="mfg"></node>
<node key="readme" name="Version Info" type="everybody"></node>
</node>
</nodes>
I recently needed to have two builds of the project that differ slightly (don't want to pay for library code for many users). So I have one build where I #if out all the ui tools related to the barcode. This works great everwhere except in XML literals like this
#if USE_BAR_CODE=1
<node key="barcode" name="Barcode Entry" ctrl="EditMfgEntry" type="mfg"> </node>
#end if
If I set USE_BAR_CODE to 0 I still get the xml literal inside the #if block, but everywhere in my code where I #if'ed regular VB source the code was not compiled.
This leads me to believe that the compliation process handles xml literals BEFORE #if statements. Am I missing something?

Unfortunately no, you can't use directives like this in literals. Under the hood, the behavior is the same as the If, Then and Else statements. You can't put those directly in line the way you are, you have to wrap the code block to tell the compiler what to compile versus run time compile like the standard If, Then and Else statements. The statements within a conditional compilation block must be complete logical statements. For example, you cannot conditionally compile only the attributes of literals, functions etc...
So a quick solution is below to either include the node or not include it. This was tested as well and works just fine.
#If USE_BAR_CODE = 1 Then
Private ReadOnly _actionTreeXml As XElement = <nodes>
<node key="any" name="Top">
<node key="log" name="***LOGIN***" type="everybody"></node>
<node key="op" name="Home" ctrl="uiHomePage" type="mfg"></node>
<node key="barcode" name="Barcode Entry" ctrl="EditMfgEntry" type="mfg"></node>
<node key="wip" name="Work in Progress" ctrl="QueryWIP" type="mfg"></node>
<node key="readme" name="Version Info" type="everybody"></node>
</node>
</nodes>
#Else
Private ReadOnly _actionTreeXml As XElement = <nodes>
<node key="any" name="Top">
<node key="log" name="***LOGIN***" type="everybody"></node>
<node key="op" name="Home" ctrl="uiHomePage" type="mfg"></node>
<node key="wip" name="Work in Progress" ctrl="QueryWIP" type="mfg"></node>
<node key="readme" name="Version Info" type="everybody"></node>
</node>
</nodes>
#End If

May be not the solution at all, but as proverb says "A bad bush is better than the open field". 😉
The main idea is to use processing instruction node. If element has processing instruction above it, then skip it, otherwise include it. This way you can control the "inclusiveness" of that element.
1) First, create extension method:
Module XmlExtentions
<System.Runtime.CompilerServices.Extension>
Function Filter(root_node As IEnumerable(Of XElement)) As IEnumerable(Of XElement)
Return root_node.First().Elements().Where(
Function(e)
If e.PreviousNode Is Nothing Then Return True
Return e.PreviousNode.NodeType <> Xml.XmlNodeType.ProcessingInstruction
End Function)
End Function
End Module
2. Query our XML:
Dim _actionTreeXml As XElement =
<nodes>
<node key="any" name="Top">
<node key="log" name="***LOGIN***" type="everybody"></node>
<node key="op" name="Home" ctrl="uiHomePage" type="mfg"></node>
<?Skip-Element?>
<node key="barcode" name="Barcode Entry" ctrl="EditMfgEntry" type="mfg"></node>
<node key="wip" name="Work in Progress" ctrl="QueryWIP" type="mfg"></node>
<node key="readme" name="Version Info" type="everybody"></node>
</node>
</nodes>
Dim elements = _actionTreeXml.<node>.Filter()
'// Do something...
elements.ToList().ForEach(Sub(e) Console.WriteLine(e))

Related

"The macro cannot be found or has been disabled because of your security settings." PPAM addin causing this error from Ribbon but not from Code

I have some VBA PPT macros along with custom ribbon interface. Whose macros work fine from code but when made into ppam addin and added to PPT addins, some commands throw this error msg, however the macro works well, just want to stop this annoying msgs. The subs and functions are all Public and it still shows the same error. Please help
Public Sub frmFeedNotes_show()
frmFeedNotes.Show
End Sub
<!--RibbonX Visual Designer 1.94 for Microsoft PowerPoint 16.0. XML Code produced on 2017-10-06-->
<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" >
<ribbon >
<tabs >
<tab
id="zenTools"
label="Zen Tools"
visible="true">
<group
id="FileProp"
label="File Properties"
visible="true">
<menu id="mnuExport"
label="Export Slides"
showImage="true"
imageMso="ExportToVCardFile" size="large">
<button id="btnFeedNotes"
label="Feed Notes*"
imageMso="FootnotesEndnotesShow"
onAction="frmFeedNotes_show" />
<button id="btnNotesCSV"
label="Notes as CSV"
imageMso="CommaSign"
onAction="ExportNotes" />
</menu>
</group >
</tab >
</tabs >
</ribbon >
</customUI >
Need to insert module name for onAction e.g. Module1
<button id="btnNotesCSV"
label="Notes as CSV"
imageMso="CommaSign"
onAction="Module1.ExportNotes" />
The ribbon Button also will pass a parameter to the sub, so expect it to receive a parameter. Add a parameter for the macro as follow
Public Sub frmFeedNotes_show(ByVal control As IRibbonControl)
frmFeedNotes.Show
End Sub

Graph tool GraphML support for coloured vertices?

I am trying to use graph-tool to graph a network graph with coloured vertices. I am trying to graph the following graphML file from here shown below.
However, the colour are not showing with the following code:
g = Graph()
g= load_graph("filename.graphml", fmt="graphml")
graph_draw(g)
The graph renders but there are no colours on vertices, only for default red. I thought that graphML was fully supported?
Graph-tool docs states: "The only file formats which are capable of perfectly preserving the internal property maps are “gt” and “graphml”. Because of this, they should be preferred over the other formats whenever possible."
Is colour not an internal property?
Oringinally I was graphing in DOT. I have an array of colours the index of which depends on name of the node - the nodes are integers in increasing order. However, when I was using:
for v in g.vertices():
v_prop[v] = colourarray[vertex]
The colours did not correspond to the correct node. This is due to the fact the load_graph seems to have its own idea of which nodes are which index. Does any one have an idea of what I can do here?
<graphml xmlns="http://graphml.graphdrawing.org/xmlns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd">
<key id="d0" for="node" attr.name="color" attr.type="string">
<default>yellow</default>
</key>
<key id="d1" for="edge" attr.name="weight" attr.type="double"/>
<graph id="G" edgedefault="undirected">
<node id="n0">
<data key="d0">green</data>
</node>
<node id="n1"/>
<node id="n2">
<data key="d0">blue</data>
</node>
<node id="n3">
<data key="d0">red</data>
</node>
<node id="n4"/>
<node id="n5">
<data key="d0">turquoise</data>
</node>
<edge id="e0" source="n0" target="n2">
<data key="d1">1.0</data>
</edge>
<edge id="e1" source="n0" target="n1">
<data key="d1">1.0</data>
</edge>
<edge id="e2" source="n1" target="n3">
<data key="d1">2.0</data>
</edge>
<edge id="e3" source="n3" target="n2"/>
<edge id="e4" source="n2" target="n4"/>
<edge id="e5" source="n3" target="n5"/>
<edge id="e6" source="n5" target="n4">
<data key="d1">1.1</data>
</edge>
</graph>
</graphml>
You are conflating the existence of a property map with it actually being used for drawing. If you want to use a property map, you have to do so explicitly:
graph_draw(g, vertex_fill_color=g.vp.color)
Please take a look at the documentation, which contains many examples of this kind.
The inherent vertex "names" that exist in dot and graphml files are loaded as internal property maps. In the case of dot, the property map is named "vertex_name":
g.vp.vertex_name
in the case of graphml, if the vertex label is not canonical (i.e. numbered from 0 to N-1), it is stored as "_graphml_vertex_id":
g.vp._graphml_vertex_id

How can I configure a live template that generates a builder method in IntelliJ IDEA?

I oftentimes need to create builder methods in my code. These methods are similar to getters, but they return this and they use with instead of get.
To be faster with that task I'd like to create a live-template in IDEA.
This how far I got:
(in ~/.IntelliJIdea14/config/templates/user.xml this looks like this:)
<template name="builderMethod" value="public $CLASS_NAME$ with$VAR_GET$(final $TYPE$ $PARAM_NAME$) {
this.$VAR$ = $PARAM_NAME$;
return this;
}" description="create a builder method" toReformat="true" toShortenFQNames="true">
<variable name="CLASS_NAME" expression="className()" defaultValue="" alwaysStopAt="true" />
<variable name="VAR" expression="complete()" defaultValue="" alwaysStopAt="true" />
<variable name="PARAM_NAME" expression="VAR" defaultValue="" alwaysStopAt="true" />
<variable name="TYPE" expression="typeOfVariable("this." + VAR)" defaultValue="" alwaysStopAt="true" />
<variable name="VAR_GET" expression="capitalize(VAR)" defaultValue="" alwaysStopAt="true" />
<context>
<option name="JAVA_EXPRESSION" value="false" />
<option name="JAVA_DECLARATION" value="true" />
</context>
</template>
This nearly works, except for typeOfVariable("this." + VAR) which does not. I just guessed how to call this method, because I could not find any documentation on the syntax used in the expressions except for this page which does not even mention a script language name or something that would make googling for easier.
How do I fix the call to typeOfVariable?
Bonus question: How can I get complete() for VAR to only show fields?
Similar question but not does not even have a start: Live Template for Fluent-API Builder in IntelliJ
Replace typeOfVariable("this." + VAR) with typeOfVariable(VAR).
Edit:
Another way to generate builder methods is to use an appropriate setter template (instead of live template).
https://www.jetbrains.com/help/idea/2016.1/generate-setter-dialog.html
There is already a built-in setter template named "Builder" which generates setters such as:
public Foo setBar(int bar) {
this.bar = bar;
return this;
}
You can create your own template (e.g by copying it) and change it so that the method prefix is with.
And to make the generated method parameter final go to settings:
Editor | Code Style | Java
Select the Code Generation tab
Tick Make generated parameters final
IntelliJ IDEA add final to auto-generated setters

RelaxNG choice and regular expression

I have a RelaxNG based XML instance which should allow (choice) align="right", align="left" and align="{ anything here }". The right/left is easy:
attribute name="align">
<a:documentation>doc for attrib</a:documentation>
<choice>
<value>left</value>
<a:documentation>doc for left</a:documentation>
<value>right</value>
<a:documentation>doc for right</a:documentation>
</choice>
</attribute>
But how can I allow the user to enter { ... } as an alternative to the two values above?
You need to use the XML Schema string type and set a pattern on it. Here's an example:
<grammar xmlns="http://relaxng.org/ns/structure/1.0"
datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes">
<start>
<element name="top">
<oneOrMore>
<element name="foo">
<attribute name="align">
<choice>
<value>left</value>
<value>right</value>
<data type="string">
<param name="pattern">\{.+\}</param>
</data>
</choice>
</attribute>
</element>
</oneOrMore>
</element>
</start>
</grammar>
And a test file:
<top>
<!-- valid cases -->
<foo align="{fasf}"></foo>
<foo align="left"></foo>
<foo align="right"></foo>
<!-- invalid cases -->
<foo align="fasf"></foo>
<foo align="{}"></foo>
<foo align="a{c}b"></foo>
</top>
Note the comments that delimit valid and invalid cases.
The regular expression I used allows things like align="{ab{c}d}". Curly braces are valid inside the curly braces that start and end the attribute value. I've not see any reason to disallow it.

Trouble passing XML text to RIA Services from my client

I am trying to pass the following XML text to RIA services in a query operation that returns a queryable sequence of entities in response:
<?xml version="1.0" encoding="utf-8"?>
<project>
<item type="Item" filetype="cabinet" category="EZ Workshop" name="EZW Panel Edge-Banded" height="24.000000" width="36.000000" depth="0.725000" quantity="1">
</item>
<item type="Item" filetype="cabinet" category="EZ Furniture" name="Entry Bench" height="19.000000" width="48.000000" depth="17.999999" quantity="1">
</item>
<item type="Item" filetype="cabinet" category="EZ Closet Euro Style" name="CSEKD Tall H3R 28-72W 12-24D" height="84.000000" width="54.000000" depth="19.999999" quantity="1">
</item>
<item type="Item" filetype="assembly" category="EZ Pro ManCave" name="EZ Corn Hole Game-Set" height="0" width="0" depth="0" quantity="1">
</item>
<item type="Item" filetype="assembly" category="EZ Office" name="EZ 30 Printer Stand" height="0" width="0" depth="0" quantity="1">
</item>
<item type="Item" filetype="assembly" category="Corporate Culture Pro" name="C-Table" height="0" width="0" depth="0" quantity="1">
</item>
</project>
This is the query operation:
[Query]
public IQueryable<ProjectItem> GetItemsFromImport(String a_strImportXml)
{
// Return empty sequence for now as a test.
return new ProjectItem[0].AsQueryable();
}
When I pass the full XML, I get that annoying "Not Found" exception, and the break-point in my operation is never hit. I'm using Visual Studio 2010's ASP.NET Development Server. When I get "Not Found" with that it portends bad stuff. The kicker is, when I pass an empty string, I get no exceptions at all, and the break-point is hit.
As you can see its not a tremendously long XML document. Is there some limit to the amount of data sent? Do I have to escape the document?
Thanks.
Edits:
I discovered that I only had to escape the structural characters ('<', '>', and '&') out of the document before I sent it. I'm using String.Replace to do it. Does anyone know if there is a better way to accomplish this. Something similar to Uri.EscapeDataString perhaps?
var strImportXml = a_xImport.ToString();
strImportXml = strImportXml.Replace("&", "&");
strImportXml = strImportXml.Replace("<", "<");
strImportXml = strImportXml.Replace(">", ">");
Ok, so I guess this is standing as the answer to my own question. I discovered that I only had to escape the structural characters ('<', '>', and '&') out of the document before I sent it. I'm using String.Replace to do it. Does anyone know if there is a better way to accomplish this. Something similar to Uri.EscapeDataString perhaps?
var strImportXml = a_xImport.ToString();
strImportXml = strImportXml.Replace("&", "&");
strImportXml = strImportXml.Replace("<", "<");
strImportXml = strImportXml.Replace(">", ">");
NOTE: I'd still like to know if there is a better way to do this.