Friday, March 4, 2011

Retrive browser and OS in PHP

Hi friends,

I need to retrive the browser and OS through php coding.

I had used $_SERVER['HTTP_USER_AGENT'], but its shows the following

Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 1.1.4322; IEMB3)

How can i separate the browser and Os from the above received value..

Please guide me..

Thanks in advance

From stackoverflow
  • Grab a copy of browscap.ini compare $_SERVER['HTTP_USER_AGENT'] with that file.

  • Hi, you can use simple explode();

    <?php
    $ex=explode(' ',$_SERVER['HTTP_USER_AGENT']);
    echo 'OS: '.$ex[4].' '.$ex[5].' '.$ex[6].'/n'; 
    echo 'Browser: '.$ex[0]; 
    ?>
    
  • Just use the built in function for this http://ie.php.net/manual/en/function.get-browser.php

  • You could use the Google code Browscap class. It essentially does the same thing as PHP's get_browser(), but you don't have to worry about keeping your browscap.ini up to date.

    This worked for me:

    require('Browscap.php');
    $browscap = new Browscap('/path/to/cache');
    var_dump($browscap->getBrowser());
    
  • The best way would be using the buil-in get_browser() function as it's a few times faster than the Google-Code-version if you're only running it once. (If you're running it 4+ times the Google-Code-version is faster)

    So unless you need the auto-update and only use one check at a time, you should use the built-in version. :)

    And it shouldn't be that hard to make a cronjob to get the newest version. ;)

Starting project from scratch: how and when to distribute the workload?

Hello,

My friend and I are going to develop a new commercial web project. We have a kind of a document that lists all the things that we want to have and we are wondering what is the best way to actually start coding it. The thing is that we used to develop software either in solo-mode or join some projects that were in the middle of development and responsibilities are easy to distribute between members of a team. Now we're starting from scratch and there're obviously things like database design or some essential functionality development, absence of those would be a showstopper for either of us. Also we have like seven hours time difference between us.

Again, we know how teamwork works, we have all the tools we need and we know how to spread the workload when all the groundwork is done, however how to start the groundwork in a distributed team, when everything depends on the results of this groundwork? If there's no database, how my party can start working on the user dashboard functionality?

So, how you guys would start such a development process? At which point you can easily start distributing workload between team members?

I'm wondering if Joel and Jeff could share their experience on the subject, given that Stack Overflow was developed by distributed team in quite a short time.

Thanks!

From stackoverflow
  • Try listening to the earlier episodes of the Stack Overflow podcast for insight into the SO dev process.

  • Also, look into Extreme Programming. Not all the details fit this, but there is a technique that will do you great good: organize the project into small "user stories", which describe one little bit of end-to-end functionality, define an "aceptance test" that tells you when a user story is done, and then work on one user story at a time, or one each.

  • heed the wisdom of Charlie Martin, but also have at least a gentleman's agreement between the two of you as to responsibilities and commitments for completion and commercialization.

    It's one thing to say "yeah i'll help you write the whizbang.com site", but it's quite another to commit to maintaining it, supporting it financially, marketing it, and making it work as a business

    : Yes, we do have an agreement and will form an LLC eventually, when the thing will take off. All we need now is a way to start the damn thing! :)
  • I have worked with distributed teams before, The most important thing you need to have is clarity of your goals, milestones and responsibilities. There are many tools on the net to help you with this project:

    • Google code < You can use this as your code repository.
    • Checkout the tools at 37signals.com, they have a few good tools for collaboration and the basic versions are free
    • You can use crossloop to share your screen to fix things and discuss them.
  • I clarified my question a little bit.

Haskell Function Application

A bit of a neophyte haskell question, but I came across this example in Haskell's tutorial examples. For "find the last element of a list" there are some obvious versions, like

last' [x] = x
last' (_:xs) = last' xs

But I can't make sense of an alternate version presented:

myLast' = foldr1 (const id)

So, in trying to make sense of what the application of the id function is doing, I tried in ghci:

const id 1 2 -> gives 2

This binds like this:

(const id) 1 2 -> gives 2

And not like this:

 const (id 1) 2 -> gives 1

But I'm not making sense of this. (const id) should translate to something like

`(\x y->x) (\x->x)`

Shouldn't this return a function that simply returns the id of its first element? Or, how is the function order making (const id) behave differently than const?

From stackoverflow
  • The definition of const is

    const x = \_ -> x
    

    Hence, (const id) is a function which takes one argument and always returns id and

    const id 1 2 = (\_ -> id) 1 2
                 = id 2
                 = 2
    

    The definition of foldr1 is

    foldr1 f [x] = x
    foldr1 f (x:xs) = f x (foldr1 f xs)
    

    If we have

    myLast' = foldr1 (const id)
    

    then

    myLast' [x] = foldr1 (const id) [x]
                  {- definition of foldr1 -}
                = x
    

    and

    myLast' (x:xs) = foldr1 (const id) (x:xs)
                     {- definition of foldr1 -}
                   = (const id) x (foldr1 (const id) xs)
                     {- definition of const -}  
                   = (\_ -> id) x (foldr1 (const id) xs)
                     {- function application -}  
                   = id (foldr1 (const id) xs)
                     {- definition of id -}  
                   = foldr1 (const id) xs
                     {- definition of myLast' -}  
                   = myLast' xs
    

    which agrees with the definition of last'.

    Steve B. : ahhh. Didn't make the connection of the const returning the function. Thanks for the explanation.
    J Cooper : This is a great explanation, and stepping through it I can see how it works. But is foldr1 (const id) really the idiomatic way to do a myLast function? The first example given seems way more clear...
    Chris Conway : I wouldn't say it's idiomatic... Haskell geeks enjoy working out how to express things in a "point-free" style, or purely in terms of folds. It becomes a kind of programming puzzle. The first version is much more clear.
  • I rely heavily on :t when trying to understand Haskell. In this case:

    Prelude> :t const id
    const id :: b -> a -> a

    might have helped you see what was going on.

    Jay Conrod : To clarify, ":t" is a command you can use in GHCI to print the type of an expression.

Writing a compiler for a DSL in python

I am writing a game in python and have decided to create a DSL for the map data files. I know I could write my own parser with regex, but I am wondering if there are existing python tools which can do this more easily, like re2c which is used in the PHP engine.

Some extra info:

  • Yes, I do need a DSL, and even if I didn't I still want the experience of building and using one in a project.
  • The DSL contains only data (declarative?), it doesn't get "executed". Most lines look like:

    SOMETHING: !abc @123 #xyz/123

    I just need to read the tree of data.

From stackoverflow
  • There are plenty of Python parsing tools: http://nedbatchelder.com/text/python-parsers.html

  • Yes, there are many -- too many -- parsing tools, but none in the standard library.

    From what what I saw PLY and SPARK are popular. PLY is like yacc, but you do everything in Python because you write your grammar in docstrings.

    Personally, I like the concept of parser combinators (taken from functional programming), and I quite like pyparsing: you write your grammar and actions directly in python and it is easy to start with. I ended up producing my own tree node types with actions though, instead of using their default ParserElement type.

    Otherwise, you can also use existing declarative language like YAML.

  • I've always been impressed by pyparsing. The author, Paul McGuire, is active on the python list/comp.lang.python and has always been very helpful with any queries concerning it.

    Torsten Marek : I would have suggested it if you hadn't done it already! PyParsing is awesome.
  • Here's an approach that works really well.

    abc= ONETHING( ... )
    xyz= ANOTHERTHING( ... )
    pqr= SOMETHING( this=abc, that=123, more=(xyz,123) )
    

    Declarative. Easy-to-parse.

    And...

    It's actually Python. A few class declarations and the work is done. The DSL is actually class declarations.

    What's important is that a DSL merely creates objects. When you define a DSL, first you have to start with an object model. Later, you put some syntax around that object model. You don't start with syntax, you start with the model.

    too much php : I know what you're saying, but writing all those comments, parenthesis, equals, prefixes is obfuscating the actual data. Also, this method doesn't port well to more verbose languages like PHP or Java.
    S.Lott : @Peter. Disagree. You can use positional args and eliminate the labels and ='s. It translates perfectly to Java. Already used it in production applications to define a declarative DSL.
    Mendelt : I've seen what you're suggesting referred to as an internal DSL. I like this method. One problem might be that while the method does port to other languages (i've seen stuff like this implemented in C#) the precise syntax of your DSL will probably change a bit. The map files won't be portable.
    S.Lott : @Mendelt: Didn't see portability as a requirement. You're right, but it doesn't seem to apply in this case.
  • I have written something like this in work to read in SNMP notification definitions and automatically generate Java classes and SNMP MIB files from this. Using this little DSL, I could write 20 lines of my specification and it would generate roughly 80 lines of Java code and a 100 line MIB file.

    To implement this, I actually just used straight Python string handling (split(), slicing etc) to parse the file. I find Pythons string capabilities to be adequate for most of my (simple) parsing needs.

    Besides the libraries mentioned by others, if I were writing something more complex and needed proper parsing capabilities, I would probably use ANTLR, which supports Python (and other languages).

  • Peter,

    DSLs are a good thing, so you don't need to defend yourself :-) However, have you considered an internal DSL ? These have so many pros versus external (parsed) DSLs that they're at least worth consideration. Mixing a DSL with the power of the native language really solves lots of the problems for you, and Python is not really bad at internal DSLs, with the with statement handy.

  • For "small languages" as the one you are describing, I use a simple split, shlex (mind that the # defines a comment) or regular expressions.

    >>> line = 'SOMETHING: !abc @123 #xyz/123'
    
    >>> line.split()
    ['SOMETHING:', '!abc', '@123', '#xyz/123']
    
    >>> import shlex
    >>> list(shlex.shlex(line))
    ['SOMETHING', ':', '!', 'abc', '@', '123']
    

    The following is an example, as I do not know exactly what you are looking for.

    >>> import re
    >>> result = re.match(r'([A-Z]*): !([a-z]*) @([0-9]*) #([a-z0-9/]*)', line)
    >>> result.groups()
    ('SOMETHING', 'abc', '123', 'xyz/123')
    

SQL "SELECT IN (Value1, Value2...)" with passing variable of values into GridView

Hi there, I have a strange encounter when creating a GridView using "SELECT..WHERE.. IN (value1, val2...)".

in the "Configure datasource" tab, if i hard code the values "SELECT ....WHERE field1 in ('AAA', 'BBB', 'CCC'), the system works well.

However, if I define a new parameter and pass in a concatenated string of values using a variable; be it a @session, Control or querystring; e.g. "SELECT .... WHERE field1 in @SESSION" the result is always empty.

I did another experiment by reducing the parameter content to only one single value, it works well.

in short, if I hardcode a string of values, it works, if I pass a variable with single value only, it works, but if i pass a varialbe with two values; it failed.

Pls advise if I have make any mistake or it is a known bug.

BR SDIGI

From stackoverflow
  • Take a look at the answer to this question (which is very similar to yours)

    http://stackoverflow.com/questions/337704/parameterizing-a-sql-in-clause

    Which ultimately links (via a convoluted route) to this definitive answer:

    http://www.sommarskog.se/arrays-in-sql.html

  • If you go to using a stored procedure, you can use this method, which I discussed in regards to how to do it in SQL.

  • Thanks all.

    I tried "CancelSelectOnNullParameter" to False n problem persist.

    The fact is that I created only one parameter and I am not leaving the parameter to NULL value.

    When I pass in the single value "'AAA'", it works; but when I pass in the value "'AAA', 'BBB'", it fails. and if I hardcode the values "'AAA', 'BBB'" in the SELECT statement without passing via a variable, it works well too.

    For Andrew and Mitchel's article, we will try creating a new table to store the values.

    will keep you posted on the outcome.

    BR

    Timothy Khouri : Are you setting a single variable to "'AAA','BBB'" ? because that won't work at all. You would have to be building a dynamic SQL string or use one of the suggested answers below.
  • Thanks everyone who responded to help me. I appreciate :-)

    Timothy is sharp enough to spot my silly mistake! salute to you...

    Yes, once I change to dynamic SQL, it works.

    I also tried with Mitchel's suggestion, thanks.

    BR

Rhino Mocks - Setting up results for non-virtual methods

I'm playing around with Rhino Mocks and am trying to set some dummy results on my mocked objects so when they are called in my factory methods I don't have to worry about the data.

But I've hit a snag, the methods I want to have the dummy results for are causing exceptions because they aren't virtual.

I've got code like this:

using(mock.Record()){
  SetupResult.For(service.SomeMethod()).Return("hello world");
}

Does the SomeMethod method have to be a virtual to be have a mocked result?

Also, what's the difference between SetupResult.For and Expect.Call?

From stackoverflow
  • Rhino Mocks uses DynamicProxy2 to do it's magic, so you will not be able to set up expectations/results on non-virtual methods.

    As for the difference between SetupResult.For, and Expect.Call if you want your test to fail validation if a method is not called, use Expect.Call. If you just want to provide a result from your mock object, and you don't want to fail verification if it is not called, use SetupResult.For

    So the following will fail:

    using(mock.Record()){
        Expect.Call(service.SomeMethod()).Return("you have to run me");
    }
    
    using(mock.Replay()){
        // Some code that never calls service.SomeMethod()
    }
    

    And this test will not:

    using(mock.Record()){
        SetupResult.For(service.SomeMethod()).Return("you don't have to run me");
    }
    
    using(mock.Replay()) {
        // Some code that never calls service.SomeMethod()
    }
    

    Does that make sense?

    Slace : Thanks, that explains it well. I was compairing Typemock and Rhino Mocks when I found this. Typemock can mock non-virtuals so it's a plus in my book so far.
  • typemock isolator can do this: Typemock.com

    Slace : I know, but it's not free ;). But I do own a copy

create java console inside the panel

How can I create an instance of the Java console inside of a panel?

From stackoverflow
  • Here's a functioning class - you'll have to replace my library function EzTimer with Thread.sleep() and try/catch the InterruptedException.

    You can install an instance of this into the system out and err using

    PrintStream con=new PrintStream(new TextAreaOutputStream(...));
    System.setOut(con);
    System.setErr(con);
    

    Here's the class

    import java.io.*;
    import java.util.*;
    import javax.swing.*;
    
    public class TextAreaOutputStream
    extends OutputStream
    {
    
    // *****************************************************************************
    // INSTANCE PROPERTIES
    // *****************************************************************************
    
    private JTextArea                       textArea;                               // target text area
    private int                             maxLines;                               // maximum lines allowed in text area
    private LinkedList                      lineLengths;                            // length of lines within text area
    private int                             curLength;                              // length of current line
    private byte[]                          oneByte;                                // array for write(int val);
    
    // *****************************************************************************
    // INSTANCE CONSTRUCTORS/INIT/CLOSE/FINALIZE
    // *****************************************************************************
    
    public TextAreaOutputStream(JTextArea ta) {
        this(ta,1000);
        }
    
    public TextAreaOutputStream(JTextArea ta, int ml) {
        if(ml<1) { throw new IoEscape(IoEscape.GENERAL,"Maximum lines of "+ml+" in TextAreaOutputStream constructor is not permitted"); }
        textArea=ta;
        maxLines=ml;
        lineLengths=new LinkedList();
        curLength=0;
        oneByte=new byte[1];
        }
    
    // *****************************************************************************
    // INSTANCE METHODS - ACCESSORS
    // *****************************************************************************
    
    public synchronized void clear() {
        lineLengths=new LinkedList();
        curLength=0;
        textArea.setText("");
        }
    
    /** Get the number of lines this TextArea will hold. */
    public synchronized int getMaximumLines() { return maxLines; }
    
    /** Set the number of lines this TextArea will hold. */
    public synchronized void setMaximumLines(int val) { maxLines=val; }
    
    // *****************************************************************************
    // INSTANCE METHODS
    // *****************************************************************************
    
    public void close() {
        if(textArea!=null) {
            textArea=null;
            lineLengths=null;
            oneByte=null;
            }
        }
    
    public void flush() {
        }
    
    public void write(int val) {
        oneByte[0]=(byte)val;
        write(oneByte,0,1);
        }
    
    public void write(byte[] ba) {
        write(ba,0,ba.length);
        }
    
    public synchronized void write(byte[] ba,int str,int len) {
        try {
            curLength+=len;
            if(bytesEndWith(ba,str,len,LINE_SEP)) {
                lineLengths.addLast(new Integer(curLength));
                curLength=0;
                if(lineLengths.size()>maxLines) {
                    textArea.replaceRange(null,0,((Integer)lineLengths.removeFirst()).intValue());
                    }
                }
            for(int xa=0; xa<10; xa++) {
                try { textArea.append(new String(ba,str,len)); break; }
                catch(Throwable thr) {                                                 // sometimes throws a java.lang.Error: Interrupted attempt to aquire write lock
                    if(xa==9) { thr.printStackTrace(); }
                    else      { EzTimer.delay(200);    }
                    }
                }
            }
        catch(Throwable thr) {
            CharArrayWriter caw=new CharArrayWriter();
            thr.printStackTrace(new PrintWriter(caw,true));
            textArea.append(System.getProperty("line.separator","\n"));
            textArea.append(caw.toString());
            }
        }
    
    private boolean bytesEndWith(byte[] ba, int str, int len, byte[] ew) {
        if(len<LINE_SEP.length) { return false; }
        for(int xa=0,xb=(str+len-LINE_SEP.length); xa<LINE_SEP.length; xa++,xb++) {
            if(LINE_SEP[xa]!=ba[xb]) { return false; }
            }
        return true;
        }
    
    // *****************************************************************************
    // STATIC PROPERTIES
    // *****************************************************************************
    
    static private byte[]                   LINE_SEP=System.getProperty("line.separator","\n").getBytes();
    
    } /* END PUBLIC CLASS */
    
    OscarRyz : Look interesting. Do you have an screenshot?
    Software Monkey : Not much to see... it's a console window! But I see someone has already posted their test program with a pic in another answer.
    OscarRyz : It was me! :) , I couldn't resists.
  • @Sofware Monkey:

    It works!! :)

    alt text

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    
    public class Main{
        public static void main( String [] args ) throws InterruptedException  {
            JFrame frame = new JFrame();
            frame.add( new JLabel(" Outout" ), BorderLayout.NORTH );
    
            JTextArea ta = new JTextArea();
            TextAreaOutputStream taos = new TextAreaOutputStream( ta, 60 );
            PrintStream ps = new PrintStream( taos );
            System.setOut( ps );
            System.setErr( ps );
    
    
            frame.add( new JScrollPane( ta )  );
    
            frame.pack();
            frame.setVisible( true );
    
            for( int i = 0 ; i < 100 ; i++ ) {
                System.out.println( i );
                Thread.sleep( 500 );
            }
        }
    }
    
    furtelwart : Please upvote his answer if it works.
    OscarRyz : If helps for anything. I've mark this as community wiki. At least I won't get points for something I didn't program.
  • what is Ioscape

Javascript callback when IFRAME is finished loading?

I need to execute a callback when an IFRAME has finished loading. I have no control over the content in the IFRAME, so I can't fire the callback from there.

This IFRAME is programmaticly created, and I need to pass its data as a variable in the callback, as well as destroy the iframe.

Any ideas?

EDIT:

Here is what I have now:

function xssRequest(url, callback)
{
    var iFrameObj = document.createElement('IFRAME');
    iFrameObj.src = url;   
    document.body.appendChild(iFrameObj); 

    $(iFrameObj).load(function() 
    {
     document.body.removeChild(iFrameObj);
     callback(iFrameObj.innerHTML);
    });
}

This callsback before the iFrame has loaded, so the callback has no data returned.

From stackoverflow
  • Try this:

    iframe.contentWindow.onload = yourcallbackfunction;

    Should work in IE at least.

    FlySwat : That doesn't appear to work in FF or IE.
  • I've had exactly the same problem in the past and the only way I found to fix it was to add the callback into the iframe page. Of course that only works when you have control over the iframe content.

    roryf : I fail to see what was wrong with this suggestion and why it was voted down. Some people eh....
    Elmo Gallen : I didn't vote you down, but I imagine you got voted down because you suggested exactly what he said he can't do -- he did say specifically, "I have no control over the content in the IFRAME, so I can't fire the callback from there."
  • Can you try switching the order of:

        document.body.removeChild(iFrameObj);
        callback(iFrameObj.innerHTML);
    
    FlySwat : No difference, only I get blank content, then it removes it.
  • You can use observe() method of HTMLElement to set the 'load' handler:

    iFrameObj.observe('load', function(e) {....});
    

    Also, I'd suggest to set an inner spanning DIV inside the iframe and set load handler to it.

  • I have a similar code in my projects that works fine. Adapting my code to your function, a solution could be the following:

    function xssRequest(url, callback)
    {
        var iFrameObj = document.createElement('IFRAME');
        iFrameObj.id = 'myUniqueID';
        document.body.appendChild(iFrameObj);       
        iFrameObj.src = url;                        
    
        $(iFrameObj).load(function() 
        {
            callback(window['myUniqueID'].document.body.innerHTML);
            document.body.removeChild(iFrameObj);
        });
    }
    

    Maybe you have an empty innerHTML because (one or both causes): 1. you should use it against the body element 2. you have removed the iframe from the your page DOM

  • First up, going by the function name xssRequest it sounds like you're trying cross site request - which if that's right, you're not going to be able to read the contents of the iframe.

    On the other hand, if the iframe's URL is on your domain you can access the body, but I've found that if I use a timeout to remove the iframe the callback works fine:

    // possibly excessive use of jQuery - but I've got a live working example in production
    $('#myUniqueID').load(function () {
      if (typeof callback == 'function') {
        callback($('body', this.contentWindow.document).html());
      }
      setTimeout(function () {$('#frameId').remove();}, 50);
    });
    
    Sam Soffes : you could replace `$('body', this.contentWindow.document).html()` with `this.contentDocument.body.outerHTML`
  • I have had to do this in cases where documents such as word docs and pdfs were being streamed to the iframe and found a solution that works pretty well. The key is handling the onreadystatechanged event on the iframe.

    Lets say the name of your frame is "myIframe". First somewhere in your code startup (I do it inline any where after the iframe) add something like this to register the event handler:

    document.getElementById('myIframe').onreadystatechange = MyIframeReadyStateChanged;
    

    I was not able to use an onreadystatechage attribute on the iframe, I can't remember why, but the app had to work in IE 7 and Safari 3, so that may of been a factor.

    Here is an example of a how to get the complete state:

    function MyIframeReadyStateChanged()
    {
        if(document.getElementById('myIframe').readyState == 'complete')
        {
            // Do your complete stuff here.
        }
    }
    
  • I think the load event is right. What is not right is the way you use to retreive the content from iframe content dom.

    What you need is the html of the page loaded in the iframe not the html of the iframe object.

    What you have to do is to access the content document with iFrameObj.contentDocument. This returns the dom of the page loaded inside the iframe, if it is on the same domain of the current page.

    I would retreive the content before removing the iframe.

    I've tested in firefox and opera.

    Then i think you can retreive your data with $(childDom).html() or $(childDom).find('some selector') ...

  • The innerHTML of your iframe is blank because your iframe tag doesn't surround any content in the parent document. In order to get the content from the page referred to by the iframe's src attribute, you need to access the iframe's contentDocument property. An exception will be thrown if the src is from a different domain though. This is a security feature that prevents you from executing arbitrary JavaScript on someone else's page, which would create a cross-site scripting vulnerability. Here is some example code the illustrates what I'm talking about:

    <script src="http://prototypejs.org/assets/2009/8/31/prototype.js"
            type="text/javascript"></script>
    
    <h1>Parent</h1>
    
    <script type="text/javascript">
    function on_load(iframe) {
      try {
        // Displays the first 50 chars in the innerHTML of the
        // body of the page that the iframe is showing.
        alert(iframe.contentDocument.body.innerHTML.substring(0, 50));
      } catch (e) {
        // This can happen if the src of the iframe is
        // on another domain
        alert('exception: ' + e);
      }
    }
    </script>
    <iframe id="child"
            src="iframe_content.html"
            onload="on_load(this)"></iframe>
    

    To further the example, try using this as the content of the iframe:

    <h1>Child</h1>
    
    <a href="http://www.google.com/">Google</a>
    
    Use the preceeding link to change the src of the iframe
    to see what happens when the src domain is different from
    that of the parent page
    

How can I capture the output of an Executable in an image?

I want to capture the output of an exe file as an png or jpg image, i mean the display of that exe. Is there any command on windows to capture and save as image of an exe output or any other idea that i can do this.

From stackoverflow
  • The Alt+PrtSc key combination will take a snapshot of the current window and save it into the clipboard. Then you can paste it into your drawing program of choice (MS Paint will do if nothing else is available), and save it to disk.

  • Alt-PRTSC, then open an image editor and paste.

  • Pressing the Alt + PrintScreen combination on your keyboard will capture the active application window and put it as an image on the clipboard. You can then paste it into Paint or any other application.

  • There is always the Robot class in Java. That may be helpful. Check out this slightly related question.

  • Pardon me... I cant have a monitor. so whatever method you are saying will end up with the headless exception in java!

    gotcha?! any help?

What markup language for richly formated content?

When you are developing a web-based application and you want to allow richly formatted text from the user you have to make a choice about how to allow that input. Many different markup languages have been created because it is arguably more difficult to sanitize HTML.

What are the advantages and disadvantages of the various different markup languages like:

Or to put it differently, what factors do you consider when choosing to use a particular markup language.

From stackoverflow
  • Markdown, BBCode, Textile, MediaWiki markup are all basically the same general concept, so I would really just lump this into two categories: HTML, and plain text markup.

    HTML

    The deal with HTML is the content is already in a "presentable" form for web content. That's great, saves processing time, and it's a readily parse-able language. There are dozens of libraries in pretty much any language to handle HTML content, convert to/from HTML to other formats, etc. The main downside is that because of the loose standards of the early web days, HTML can be incredibly variable and you can't always depend on sane input when accepting HTML from users. As pointed out, tidying or santizing HTML is often very difficult, especially because it fails to follow normal markup rules the way XML does (i.e. improperly closed tags are common).

    Plain Text Markup

    This category is frequently used for the following reasons:

    • Easy to parse into multiple forms from one source - PDF, HTML, RTF
    • Content is stored in readable plain text (usually much easier to read than raw HTML) if needed at some later date, rather than needing to extract from the HTML
    • Follows specific defined rules where HTML can be annoying variable and unstructured
    • Allows you to force a subset of content formatting that's more appropriate in many cases than simply allowing full HTML
    • In addition to forcing a subset of HTML makes it easy to sanitize input and prevent cross site scripting problems etc.
    • Keeping the "raw" data in an abstracted format means that at a later date, if you for instance wanted to convert your site from HTML 4 to XHTML, you only need to change the parsing code. With HTML formatted user input, you're stuck now having to convert all the HTML to XHTML individually, which as HTML Tidy shows, is not always a simple task. Similarly if a new markup language comes along at some point or you need to move to an alternative format (RTF, PDF, TeX) an abstracted restricted subset of text formatting options makes that a much simpler task.

    Bottom line is what is the user input being used for. If you're planning to keep the data around and may need to shuffle formats etc. then it makes sense to use a careful abstract format to store the information. If you need to work with the raw data manually for any reason, then bonus points if that format is easily human-readable. If you're only displaying the content in a web page (or HTML doc for a report etc.) and you have no concerns about converting it or future-proofing it, then it's a reasonable practice to store it in HTML.

  • Jeff discussed some pros and cons on codinghorror.com while they were in the initial stages of putting together SO. I thought it was a worthwhile read.

    Zoredache : Is this (http://www.codinghorror.com/blog/archives/001116.html) the article you are referring too?
    Software Monkey : Yes, and he did a follow up a few weeks later.
    Aaron Digulla : Monkey: Please add this link to your answer :)
    Software Monkey : Added link - thanks Zoredache
  • "Many different markup languages have been created because it is arguably more difficult to sanitize HTML."

    Really? How is it difficult? There are functions to remove potentially dangerous attributes or tags and validate the HTML before you enter it in database or file. Can you give me examples of how it is difficult to sanitize HTML?

How do I access database via virtual folder which points at a remote share

I'm having a problem getting access to a database which lives on a remote server.

I have a ASP.NET 2.0 webpage that is trying to connect to a database.
The database is accessed via a virtual folder (which I set up in IIS).
The virtual folder points at a remote share which contains the database.

The virtual folder (in the web apps root directory) is pointing at a share on a remote server via a UNC path:

\\databaseServerName\databaseFolder$\

The virtual folder has 'read' and 'browse' permissions set to 'true'.

I store the connection string in the 'appSettings' section of the web.config:

<add key="conStrVirtual" value="Provider=Microsoft.Jet.OleDb.4.0;Data Source=http://webAppServerName/virtualFolderName/databaseName.MDB;Jet OLEDB:Database Password=dumbPassword;"/>

The connection object is declard on my .aspx page:

Dim objConnVirtual As New OleDbConnection(ConfigurationManager.AppSettings("conStrVirtual"))

Here is the code that tries to use the connection object:

Public Sub Test()
 If objConnVirtual.State <> ConnectionState.Open Then
  objConnVirtual.Open()
 End If
 Dim cmd As OleDbCommand = New OleDbCommand("SELECT * FROM TableName", objConnVirtual)
 objDR = cmd.ExecuteReader()
 If objDR.Read() Then
  response.write("Shazaam! Data shows up here")
 End If
 objDR.Close()
 objConnVirtual.Close()
End Sub

When I run the above code I get the following error (on this line of the code 'objConnVirtual.Open()':
Exception Details: System.Data.OleDb.OleDbException: Not a valid file name.

I have checked the database name and it is correct (even copy/pasted it to make sure)

If I put the 'Data Source' section of the connection string into the address bar of my browser I can successfully see the contents of the share on the remote server.

Not sure if this is a problem with permissions or with the code.
I have googled the crap out of this but have not been able to find a solution.

Any help is much appreciated.

From stackoverflow
  • make sure the two servers have internal access to each other and also specify the ip & port of db server in your connection string .

  • When accessing a remote Access MDB database, you have to specify a UNC path like \\remoteMachine\Share\test.mdb.

    Make sure your application pool identity has the right permissions to connect to the remote share. By default on IIS 6 you are working with the Network Service account, which is by default not allowed to access a remote share.

    The best way is to let the AppPool run with a dedicated service user.

    David HAust : Hi Louis, thanks for the answer. Could you expand on what you mean when you say 'let the AppPool run with a dedicated service user'? I have added a new application pool just for my app but still can't get access to the database via the web page.
    David HAust : Also, I shouldn't have to put the database name in the UNC path, should I? I've currently got the database name in the connection string (Data Source=http://webAppServerName/virtualFolderName/databaseName.mdb)
    Louis Haußknecht : Hi David. When using IIS 6 on Windows 2003 Server a Application Pool runs with a identity. By default this is the Network Service account. When your application executes, it runs with in the context of this user. You can change it on the identiy tab. MDBs cannot be accessed over http directly.
    David HAust : Thanks for that explanation Louis, appreciated.
  • Update

    I should also mention that it works on my machine (but not once loaded up to the production box) if I declare the connection string in the 'appSettings' section of the web.config like this:

    <add key="conStrVirtual" value="Provider=Microsoft.Jet.OleDb.4.0;Data Source=\\databaseServerName\databaseFolder$\databaseName.MDB;Jet OLEDB:Database Password=dumbPassword;"/>
    

    This leads me to think that it could be an issue with needing to use domain credentials other than the local IUSER account.

  • What is the account being used on your server when your web app tries to read the db file? Whatever this user account is, it needs to have permissions to read that folder/file. In IIS6 you can configure the virtual folder to use any user account... on the Directory Security tab there's an Edit button under Authentication and access control.

    It seems likely that your error message is just a generic error message, and the permissions problem is your real issue.

  • UPDATE

    First up, thank you to everyone who submitted answers.

    However, we ended up not using the 'connect to remote database via virtual folder' method because the complexity of the permissions needed to get this to work was causing us more problems than it was worth. We put the UNC path back into the connection string, which may not be the best way to do this, but is working for us.

Skip Lists -- ever used them?

I'm wondering whether anyone here has ever used a skip list. It looks to have roughly the same advantages as a balanced binary tree, but is simpler to implement. If you have, did you write your own, or use a pre-written library (and if so, what was its name)?

From stackoverflow
  • I implemented a variant that I termed a Reverse Skip List for a rules engine a few years ago. Much the same, but the reference links run backwards from the last element.

    This is because it was faster for inserting sorted items that were most likely to towards the back-end of the collection.

    It was written in C# and took a few iterations to get working successfully.

    oɔɯǝɹ : Couldn't you invert the comparison for the type, and use a standard skiplist to achieve the same result?
  • Actually, for one of my projects I am implementing my own full STL. And I used a skiplist to implement my std::map. The reason I went with it is because it is a simple algorithm which is very close to the performance of a balanced tree but has much simpler iteration capabilities.

    Also, QT4's QMap is a skiplist as well which was the original inspiration for my using it in my std::map.

    : Evan, have you made your skiplist code available anywhere? Thanks
    Evan Teran : not quite yet, but things are almost to the point where i may release my STL. But there is plenty of example skip list implementations out there.
    : Yes, there is plenty of skip list code available. I was looking for something that specifically complies with std::map<> interface. QMap looks quite close, so I'll give that a try. Thank-you
  • Years ago I implemented my own for a probabilistic algorithms class. I'm not aware of any library implementations, but it's been a long time. It is pretty simple to implement. As I recall they had some really nice properties for large data sets and avoided some of the problems of rebalancing. I think the implementation is also simpler than binary tries in general. There is a nice discussion and some sample c++ code here:

    http://www.ddj.us/cpp/184403579?pgno=1

    There's also an applet with a running demonstration. Cute 90's Java shininess here:

    http://www.geocities.com/siliconvalley/network/1854/skiplist.html

    Head Geek : I'm accepting this one because of the DDJ article it references. Apologies to Even and Steve, I'd accept all three answers if I could.
  • Java 1.6 (Java SE 6) introduced ConcurrentSkipListSet and ConcurrentSkipListMap to the collections framework. So, I'd speculate that someone out there is really using them.

    Skiplists tend to offer far less contention for locks in a multithreaded situation, and (probabilistically) have performance characteristics similar to trees.

    See the original paper [pdf] by William Pugh

  • My understanding is that they're not so much a useful alternative to binary trees (e.g. red-black trees) as they are to B-trees for database use, so that you can keep the # of levels down to a feasible minimum and deal w/ base-K logs rather than base-2 logs for performance characteristics. The algorithms for probabilistic skip-lists are (IMHO) easier to get right than the corresponding B-tree algorithms. Plus there's some literature on lock-free skip lists. I looked at using them a few months ago but then abandoned the effort on discovering the HDF5 library.

    literature on the subject:

    Papers by Bill Pugh:

    non-academic papers/tutorials:

Maximum boost factor

Hi,

What is the maxiimum boost factor value for a word in Lucene.Net? I believe default value is 1.0f

Thanks!

From stackoverflow
  • I've found evidence on Google that it's at least 4... but I can't find any further information as to what the ceiling is. Some of the document importance descriptions use 4 as an example which is where I drew my conclusion.

    However the user documentation defines that it must be a positive number which may be less than 1 (presumably, given some of the examples it may also not be less than 1).

    Draw from that as you will.

  • It can be any positive number. Of course, if you pick an unreasonable number, like Float.POSITIVE_INFINITY (or the .NET equivalent), you'll end up with some crazy scores. Generally, you want to look at your boosts as a percentage: 1.0F is 100%, meaning no boost up or down. 1.2F is 120%, a little boost up; 0.5 is 50%, a fairly significant boost down.

Shorten URL value as Javascript variable

I'm using the jQuery Treeview plugin for navigation on a site with nested levels of categories. In each category there are then products, each of which have one or more information types.

So my URLs can be either:

  1. foo.com/view/cat_id/
  2. foo.com/view/cat_id/prod_id/
  3. foo.com/view/cat_id/prod_id/info_id/

Trouble is, the navigation for the products is separate to the categories, which means Treeview only 'knows about' (and therefore marks 'active') case 1. Cases 2 & 3 mean that the tree is collapsed, whereas I want the tree to be open to cat_id for all cases.

(I'm open to anything, but) I'm thinking the solution is some Javascript trickery to remove anything after cat_id/ from the URL so that I can then pass the result to Treeview, but not being that au fait with Javascript I can't figure out how to do it.

Note: I'm looking for help with the Javascript regex, not Treeview, though on the off-chance that anyone knows of a ready-rolled solution I am of course all ears.

TIA.

From stackoverflow
  • After help from a friend (thanks Sawks) I am presenting the answer to my own question in case anyone else finds themselves in the same boat.

    I changed the lines starting 206 within jquery.treeview.js from:

    case "location":
        var current = this.find("a").filter(function() { return this.href.toLowerCase() == location.href.toLowerCase(); });
        if ( current.length ) {
         current.addClass("selected").parents("ul, li").add( current.next() ).show();
        }
        break;
    }
    

    to:

    case "location":
        var current_location = location.href.toLowerCase();
        var current_base = current_location.split('/').slice(0,5).join('/')+'/';
        var current = this.find("a").filter(function() { return this.href.toLowerCase() == current_base; });
        if ( current.length ) {
         current.addClass("selected").parents("ul, li").add( current.next() ).show();
        }
        break;
    }
    

Refactoring a massive function into many files.

I've been trying to refactor a "bit" of code that I'd previously developed. Basically, the project was my response to not knowing how to use XSLT effectively, so I developed an XML transformation system in PHP. The program reads through the tags of an XML file and does something along these lines to convert it to HTML:

private function getTemplate(...) {
    switch ($nodeName) {
        case "a" :
            // code here to generate a link tag
            break;
        case "box" :
            // code here to generate the divs and whatnot to create a box
            break;
        case "ref" :
            // look up an external reference file and include a bibliography
            break;
        default :
            // do the default thing
    }
}

That was all working great, except that I ended up with 26 branches to my switch, and that once switch block was over 1000 lines of code. Needless to say, it made maintenance slightly more difficult.

What I've done now is to pull the code of each branch out into its own file (named "a.php", "box.php", "ref.php" ...) and include that file each time:

if (file_exists("templates/$nodeName.php")) {
    include "templates/$nodeName.php";
} else {
    // do the default thing
}

Again, this works, but benchmarking it shows that it has slowed down processing times by 50%. I'm assuming that this is because there's now up to 4000 includes being done now.

What I was considering was to put the code for each template into a function, and if the function hasn't been declared then include the file, and then run the function - the only problem with that is that the existing code has been written in the scope of the original function, using $this, etc.

Given that this code is not run in real time (eg: it merely processes the XML into static HTML files which are stored - it's not done on the fly), do you have any advice for me here?

From stackoverflow
  • caveat: i don't know PHP, but I can google and i like function pointers better than huge switch statements, so if you can't just use XSLT...

    ...one option would be to adopt a naming convention for your 'worker' functions that incorporated the html node's tag name, e.g. <tagname>_converter, include all the functions, and replace your switch statement with something like:

    private function getTemplate(...) 
    {
        $func = $nodeName + "_converter";
        if (function_exists($func))    //see if function is defined, somehow...
        {
            $func();    //call conversion function (pass whatever is needed)
        }
    }
    
  • Try something like this:

    //utils.php
    function handle_box($node)
    {
    //...
    }
    
    function handle_link($node)
    {
    //....
    }
    ?\>
    
    

    then:

    
    require_once 'templates/utils.php';
    
    
    function getTemplate()
    {
      switch($node)
      {
          case "a" :
            handle_link($node,$otherParams);
          break;
      }
    }
    
    }
    

    Basically this refactors all the functionality for each node into its own function. From here you can work on a more generalized solution. It is very similar to what you had original but the switch/case statements will be a lot more manageable without a ton of code inside of them. You can then look towards potentially implementing the Stratgy design pattern if you feel it is necessary.

    Kudos : I think that's actually what his original code was, or if anything worse. Fortunately for you, I'm 9 points off downvoting ;)
    nickf : no, i've actually just done a cut and paste of my original code into external files - no changes made to it at all (eg: wrapping each into a function)
    grepsedawk : If that was his original code, it would be pretty amazing to turn 26 case statements into 1000 lines...
  • Learn XSLT.

    annakata : right tool for the right job
    nickf : least. helpful. answer. ever.
    Robert Rossney : You may not think it's helpful, but it's right. Do you want a maintainable, performant solution? Don't use a tool that's obviously wrong for the job, especially when there's one that's obviously right.
  • Without getting into XSLT itself and focusing on the question of refactoring: You already have considered a fair amount of different strategies. I will offer an optimization tip on one of the approaches you already experimented with, and one other approach which might work for you.

    When you said that there was 50% performance decrease when relying on many files. I take it you don't have PHP Op-code cache enabled. You do that, and that 50% lose should vanish.

    On the other approach, I would suggest creating a base class NodeProcessor or so. And then create a new class for every node ANodeProcessor, RefNodeProcessor, ...etc.

    Alternatively, you could only create one class NodeProcessor which contains methods in the form of: processTag. Then:

    1. You can call $this->processNode($tag);
    2. An in that method: $name = 'process'. $tag;
    3. Then: return $this->nodeProcessor->{$name};
    4. NodeProcessor should have a __call() method to do the "default thing" for tags with no processTag method defined.

    Again, don't forget to add op-code caching.

Which .net Web Application Framework to use ?

I am looking for a solid and easy to pick up .NET Web Application Framework for company-wide development use. I am considering DotNetNuke. Is it good ? What else can you recommend ?

From stackoverflow
  • DotNetNuke is a Content Management System. There are quite a few out there, and more vendor solutions than you can shake a stick at. It all depends on what you need/want, and whether cost is an issue. Some more information would help. Is this more of a wiki-oriented project? An intranet site? A bit of both? Is 24/7 support an issue?

    Are you looking for a framework? Or a Content Management System?

    List of Content Management Systems. There are also commercial ones, like SharePoint, as well as a whole host of custom vendor solutions.

  • If you're wanting a framework for "Web Applications" in .NET, then your choices boil down to ASP.NET or ASP.NET MVC (both from Microsoft), or an open source alternative like Castle Monorail.

    ASP.NET would be the way to go if you want to have maximum use of vendor controls, and you're putting together small, form based applications.

    For anything larger or more complex, and if you can do without vendor controls, ASP.NET MVC or Castle Monorail are going to give you a better result because they forces you into better practices.

    However, if you're looking for an existing web application to use as a basis for customization, then your starting point needs to be to tell us more detail about what you want to achieve.

    Depending on your requirements, Dotnetnuke may be a great fit for your needs, or it might be a spectaularly bad choice. Without more information we can't tell.

    mmiika : and Castle Monorail,,
  • I am looking for a framework with common services already being provided, and that we will be able to develop business applications on top of it (including a simple CMS). A framework which provides services like user access/security control, portal/pluggable UI, built-in internationalizations, etc, are important to us. Advice please.

  • SharePoint is an option but I don't recommend it. I've always wanted to try DotNetNuke. It has a strong community and a lot of growth.

    Our team worked on SharePoint features for 5 months before we ditched it. Deployment and debugging are awful experiences. We had to resort to lengthy build scripts within Visual Studio and we used a few open source tools (WspBuilder, SharePoint Installer, etc.) just to make the process half-way manageable.

    Maybe SharePoint will grow up and get real Visual Studio integration some day (I hear that is the plan with VS 2010). Until then, it is just too much trouble. The learning curve is steep and you will have to spend a lot of time searching XML config files to track down problems. SharePoint will probably make you despise XML configurations!

    UPDATE: Sharepoint is getting some much needed attention in VS2010. See: http://www.cmswire.com/cms/enterpr...

  • In the end you will need multiple frameworks. It's difficult to support any complex business environment with just one. Sounds like your dedicated to .NET so I would stick with ASP.NET or ASP.NET MVC. Use ASP.NET MVC for larger applications requiring domain driven development with testing. Use ASP.NET for data driven applications that closely match your SQL schema (quick and dirty applications without much complexity). Assuming you are using MS SQL for persistence. I would also consider the latest version of SharePoint. It's now based on ASP.NET 2.0 and bit easier to deal with than it was in the past. If a lot of your business requirements are simple you can provide a great deal of functionality with SharePoint out of the box. Of course with SharePoint you will need a good support staff for backup, maintenance, and recovery.